package com.cool.store.controller;

import com.cool.store.constants.RedisConstant;
import com.cool.store.context.PartnerUserHolder;
import com.cool.store.entity.HyPartnerBaseInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.IDCardSideEnum;
import com.cool.store.enums.WorkflowStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.*;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.*;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.vo.*;
import com.cool.store.vo.cuser.IdentityCardInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.List;

/**
 * @Author suzhuhong
 * @Date 2023/5/29 20:04
 * @Version 1.0
 */
@RestController
@RequestMapping({"/v1/partnerManage/partner" })
@Slf4j
@Api(tags = "加盟商信息")
public class PartnerController {

    @Resource
    private PartnerUserInfoService partnerUserInfoService;
    @Resource
    private HyPartnerIntentInfoService hyPartnerIntentInfoService;
    @Resource
    private AliyunService aliyunService;
    @Resource
    private HyPartnerBaseInfoService hyPartnerBaseInfoService;
    @Resource
    private HyPartnerClerkService hyPartnerClerkService;
    @Resource
    private HyPartnerLineInfoService hyPartnerLineInfoService;
    @Resource
    private RedisUtilPool redisUtilPool;

    @PostMapping(path = "/applyBaseInfo")
    @ApiOperation("提交加盟商基本信息")
    public ResponseResult<ApplyBaseInfoVO> applyBaseInfo(@RequestBody PartnerUserInfoRequest partnerUserInfoRequest){
        return ResponseResult.success(partnerUserInfoService.updatePartnerUserInfo(partnerUserInfoRequest));
    }

    @GetMapping(path = "/getSubmitIntentionApplyFlag")
    @ApiOperation("获取提交意向申请标识 true 已提交  false 未提交")
    public ResponseResult<Boolean> getSubmitIntentionApplyFlag(){
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        PartnerBaseInfoVO partnerBaseInfoVO = hyPartnerBaseInfoService.queryPartnerBaseInfo(userInfoVO.getPartnerId(), userInfoVO.getPartnerLineId());
        Boolean submitFlag = false;
        if(partnerBaseInfoVO.getStatus() != null){
            submitFlag = partnerBaseInfoVO.getStatus() > Integer.valueOf(WorkflowStatusEnum.INTENT_0.getCode());
        }
        return ResponseResult.success(submitFlag);
    }


    @GetMapping(path = "/getLineByIdCard")
    @ApiOperation("通过身份证号码查询线索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idCard", value = "身份证号码", required = false),
    })
    public ResponseResult<Boolean> getByIdCard(@RequestParam(value = "idCard",required = false)String idCard){

        HyPartnerBaseInfoDO baseInfoDO = hyPartnerBaseInfoService.getByIdCard(idCard);
        return ResponseResult.success(baseInfoDO != null && baseInfoDO.getPartnerLineId() > 0L);
    }


    @GetMapping(path = "/changeBinding")
    @ApiOperation("换绑微信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idCard", value = "身份证号码", required = false),
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<Boolean> changeBinding(@RequestParam(value = "idCard",required = false)String idCard,
                                                 @RequestParam(value = "lineId",required = false)Long lineId){
        PartnerUserInfoVO currentUser = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerBaseInfoService.changeBinding(idCard, lineId, currentUser));
    }


    @PostMapping(path = "/submitPartnerBaseInfo")
    @ApiOperation("提交意向书基本信息")
    public ResponseResult<Boolean> submitPartnerBaseInfo(@RequestBody PartnerBaseInfoRequest baseInfoRequest){
        //前提 未提交加盟申请
        //成功 意向区域变更成功  失败 您已进入意向申请流程，当前不可变更意向区域
        PartnerUserInfoVO currentUser = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerBaseInfoService.submitPartnerBaseInfo(baseInfoRequest, currentUser));
    }

    @PostMapping(path = "/submitPartnerClerkInfo")
    @ApiOperation("提交意向书店员信息")
    public ResponseResult<Boolean> submitPartnerClerkInfo(@RequestBody PartnerClerkInfoRequest partnerClerkInfoRequest){

        return ResponseResult.success(hyPartnerClerkService.submitPartnerClerkInfo(partnerClerkInfoRequest));
    }

    @PostMapping(path = "/submitPartnerIntentInfo")
    @ApiOperation("提意向书意向信息/行业认知")
    public ResponseResult<Integer> submitPartnerIntentInfo(@RequestBody PartnerIntentInfoRequest partnerIntentInfoRequest){
        return ResponseResult.success(hyPartnerIntentInfoService.submitPartnerIntentInfo(partnerIntentInfoRequest));
    }

    @PostMapping(path = "/submitWantShopInfo")
    @ApiOperation("提意向书意向信息")
    public ResponseResult<String> submitWantShopInfo(@RequestBody PartnerWantShopInfoRequest request){
        return ResponseResult.success(hyPartnerIntentInfoService.submitWantShopInfo(request));
    }

    @PostMapping(path = "/submitIndustryCognitionInfo")
    @ApiOperation("提意向书行业认知")
    public ResponseResult<Boolean> submitIndustryCognitionInfo(@RequestBody IndustryCognitionInfoRequest request){
        return ResponseResult.success(hyPartnerIntentInfoService.submitIndustryCognitionInfo(request));
    }

    @PostMapping(path = "/changePartnerClerkInfo")
    @ApiOperation("修改意向书店员信息")
    public ResponseResult<Boolean> changePartnerClerkInfo(@RequestBody PartnerClerkInfoRequest partnerClerkInfoRequest){
        return ResponseResult.success(hyPartnerClerkService.submitPartnerClerkInfo(partnerClerkInfoRequest));
    }

    @PostMapping(path = "/changePartnerIntentInfo")
    @ApiOperation("修改意向书意向信息/行业认知")
    public ResponseResult<Integer> changePartnerIntentInfo(@RequestBody PartnerIntentInfoRequest partnerIntentInfoRequest){

        return ResponseResult.success(hyPartnerIntentInfoService.submitPartnerIntentInfo(partnerIntentInfoRequest));
    }

    @GetMapping(path = "/getPartnerLineBaseInfo")
    @ApiOperation("查询加盟商线索详情(适用全部流程) 包括冷静期是否首次登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerId", value = "C端用户基本信息ID", required = false),
    })
    public ResponseResult<PartnerLineBaseInfoVO> getPartnerLinBaseInfo(@RequestParam(value = "partnerId",required = false)String partnerId){
        return ResponseResult.success(hyPartnerLineInfoService.getPartnerLinBaseInfo(partnerId));
    }

    @PostMapping(path = "/delCoolDownFirstLoginFlag")
    @ApiOperation("删除冷静期是否首次登录缓存")
    public ResponseResult<Boolean> delCoolDownFirstLoginFlag(@RequestBody CoolDownFirstLoginFlagDelRequest coolDownFirstLoginFlagDelRequest){
        String coolingPeriodFirstLoginCacheKey = MessageFormat.format(RedisConstant.COOLINGPERIOD_FIRSTLOGIN_KEY, coolDownFirstLoginFlagDelRequest.getLineId());
        redisUtilPool.delKey(coolingPeriodFirstLoginCacheKey);
        return ResponseResult.success(true);
    }



    @GetMapping(path = "/queryPartnerBaseInfo")
    @ApiOperation("查看意向申请书——基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<PartnerBaseInfoVO> queryPartnerBaseInfo(@RequestParam(value = "lineId",required = false)Long lineId){
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerBaseInfoService.queryPartnerBaseInfo(userInfoVO.getPartnerId(), lineId));
    }


    @GetMapping(path = "/queryPartnerClerkInfo")
    @ApiOperation("查看意向申请书-店员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<List<PartnerClerkVO>> queryPartnerClerkInfo(@RequestParam(value = "lineId",required = false)Long lineId){

        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerClerkService.queryPartnerClerkInfo(userInfoVO.getPartnerId(), lineId));
    }

    @GetMapping(path = "/queryPartnerIntentInfo")
    @ApiOperation("查看意向申请书-意向信息/行业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<PartnerIntentInfoVO> queryPartnerIntentInfo(@RequestParam(value = "lineId",required = false)Long lineId){
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerIntentInfoService.queryPartnerIntentInfo(userInfoVO, lineId));
    }

    @GetMapping(path = "/queryWantShopInfo")
    @ApiOperation("查看意向申请书-意向信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<PartnerIntentInfoVO> queryWantShopInfo(@RequestParam(value = "lineId",required = false)Long lineId){
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerIntentInfoService.queryWantShopInfo(userInfoVO, lineId));
    }

    @GetMapping(path = "/queryIndustryCognitionInfo")
    @ApiOperation("查看意向申请书-行业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<PartnerIntentInfoVO> queryIndustryCognitionInfo(@RequestParam(value = "lineId",required = false)Long lineId){
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerIntentInfoService.queryIndustryCognitionInfo(userInfoVO, lineId));
    }

    @GetMapping(path = "/completeJoinNotice")
    @ApiOperation("提交加盟须知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerId", value = "加盟商ID", required = false),
    })
    public ResponseResult<Boolean> completeJoinNotice(@RequestParam(value = "partnerId",required = false)String partnerId){
        return ResponseResult.success(partnerUserInfoService.completeJoinNotice(partnerId));
    }


    @GetMapping(path = "/queryJoinNotice")
    @ApiOperation("查询是否填写加盟须知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerId", value = "加盟商ID", required = false),
    })
    public ResponseResult<Boolean> queryJoinNotice(@RequestParam(value = "partnerId",required = false)String partnerId){
        return ResponseResult.success(partnerUserInfoService.queryJoinNotice(partnerId));
    }

    @GetMapping(path = "/checkGenerateNewLineId")
    @ApiOperation("提交意向申请书之前——判断是否生成新线索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "线索ID", required = false),
    })
    public ResponseResult<Long> checkGenerateNewLineId(@RequestParam(value = "lineId",required = false)Long lineId){
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(hyPartnerLineInfoService.checkGenerateNewLineId(lineId));
    }


    @GetMapping(path = "/getIdentityCardInfo")
    @ApiOperation("根据身份证正面解析获取数据")
    public ResponseResult<IdentityCardInfoVO> getIdentityCardInfo(@RequestParam(value = "faceImageUrl")String faceImageUrl, @RequestParam("side")IDCardSideEnum sideEnum){
        try {
            IdentityCardInfoVO identityCardInfo = aliyunService.getIdentityCardInfo(faceImageUrl, sideEnum);
            return ResponseResult.success(identityCardInfo);
        } catch (Exception e) {
            throw new ServiceException(ErrorCodeEnum.IDENTITY_CARD_PARSE_FAIL);
        }
    }

}
