package com.copy.trade.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bizzan.bitrade.entity.transform.AuthMember;
import com.copy.trade.constant.DogeApi;
import com.copy.trade.entity.base.BaseQueryForm;
import com.copy.trade.entity.form.FollowForm;
import com.copy.trade.entity.po.Customer;
import com.copy.trade.entity.po.FollowConfig;
import com.copy.trade.entity.vo.FollowVo;
import com.copy.trade.entity.vo.FollowerVo;
import com.copy.trade.entity.vo.TraderVo;
import com.copy.trade.enums.FollowConfigEnum;
import com.copy.trade.enums.FollowStatusEnum;
import com.copy.trade.enums.TraderEnum;
import com.copy.trade.service.ICustomerService;
import com.copy.trade.service.IFollowConfigService;
import com.copy.trade.service.IFollowService;
import com.copy.trade.util.DogeContractUtils;
import com.copy.trade.util.WebAssert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 跟随表 前端控制器
 * </p>
 *
 * @author wz
 * @since 2022-11-08
 */
@Api(value = "跟随表", tags = "跟随表")
@RestController
@RequestMapping("/follow")
public class FollowController {
	
    @Value("${doge.host}")
    private String dogeHost;

	IFollowService followServiceImpl;

	@Autowired
	private ICustomerService customerServiceImpl;

    @Autowired
    private IFollowConfigService followConfigServiceImpl;

	/**
	 * 推荐使用构造器注入
	 * @param followServiceImpl
	 */	
	@Autowired
	FollowController(IFollowService followServiceImpl) {
		this.followServiceImpl = followServiceImpl;
	}
	

	@ApiOperation(value = "跟随交易员")
	@ApiImplicitParam(name = "followForm",value = "跟随交易员表单",required = true,dataType = "FollowForm")
	@PostMapping("/")
	public Boolean following(@RequestHeader(DogeApi.TOKEN_NAME)String token,@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember,@Valid @RequestBody FollowForm followForm){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		WebAssert.isTrue(customer.getTrader() == TraderEnum.NO_TRADER.getStatus(),"chk.not.notrader");
		return followServiceImpl.following(customer.getId(),followForm,authMember.getId(),token);
	}

	@ApiOperation(value = "跟随交易员详情")
	@PutMapping("/info/{traderId}")
	public FollowVo info(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember,@PathVariable Long traderId){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		return followServiceImpl.info(customer.getId(), traderId);
	}

	@ApiOperation(value = "更新跟随设置")
	@ApiImplicitParam(name = "followForm",value = "更新跟随设置表单",required = true,dataType = "FollowForm")
	@PostMapping("/update")
	public Boolean update(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember,@Valid @RequestBody FollowForm followForm){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		return followServiceImpl.update(customer.getId(),followForm);
	}
	
	@ApiOperation(value = "获取当前跟随设置")
	@PutMapping("/config/{traderId}")
	public FollowForm getConfig(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember,@PathVariable Long traderId){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		return followServiceImpl.getConfig(customer.getId(), traderId);
	}


	@ApiOperation(value = "取消跟随")
	@PutMapping("/unfollow/{traderId}")
	public Boolean unfollow(@RequestHeader(DogeApi.TOKEN_NAME)String token,@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember,@PathVariable Long traderId){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		Customer trader = customerServiceImpl.getById(traderId);
		
		FollowConfig followConfig = followConfigServiceImpl.getConfig(FollowConfigEnum.SPECIAL_TRADER_CODE.getNameEn());
        boolean isSpecialTrader = trader.getTraderLevelId() >= Integer.parseInt(followConfig.getConfigContent());
    	if(isSpecialTrader) {
    		Boolean isSuccess = DogeContractUtils.vailSpecialTrader(dogeHost + DogeApi.UNFOLLOW_WALLET_SPECIAL,token,null);
            WebAssert.isTrue(isSuccess,"chk.unfollow.special.trader.fail");
    	}
		return followServiceImpl.unfollow(customer.getId(),traderId, FollowStatusEnum.UNFOLLOW);
	}

	@ApiOperation(value = "移除跟随")
	@PutMapping("/remove-follow/{customerId}")
	public Boolean removeFollow(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember,@PathVariable Long customerId){
		Customer trader = customerServiceImpl.getByMemberId(authMember.getId());
		return followServiceImpl.unfollow(customerId,trader.getId(),FollowStatusEnum.REMOVE);
	}

	@ApiOperation(value = "我的交易员")
	@ApiImplicitParam(name = "baseQueryForm",value = "分页条件",dataType = "BaseQueryForm")
	@PostMapping("/following")
	public Page<TraderVo> followingList(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember, @RequestBody BaseQueryForm baseQueryForm){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		return followServiceImpl.followingList(baseQueryForm.newFormPage(),customer.getId());
	}

	@ApiOperation(value = "我的学生")
	@ApiImplicitParam(name = "baseQueryForm",value = "分页条件",dataType = "BaseQueryForm")
	@PostMapping("/followers")
	public Page<FollowerVo> followers(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember, @RequestBody BaseQueryForm baseQueryForm){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		WebAssert.isTrue(customer.getTrader() == TraderEnum.TRADER.getStatus(),"chk.not.trader");
		return followServiceImpl.followers(baseQueryForm.newFormPage(),customer.getId());
	}

	@ApiOperation(value = "跟单用户数量")
	@GetMapping("/follower-count")
	public Integer followerCount(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		WebAssert.isTrue(customer.getTrader() == TraderEnum.TRADER.getStatus(),"chk.not.trader");
		return followServiceImpl.followerCount(customer.getId());
	}
}
