package com.yn.bftl.thirdparty.modules.chinaums.controller.member;

import com.yn.bftl.common.common.exception.YnceError;
import com.yn.bftl.common.modules.auth.vo.ChinaumsAuthInfoVO;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.modules.chinaums.dto.MchCreateDTO;
import com.yn.bftl.thirdparty.modules.chinaums.dto.PasswordResetDTO;
import com.yn.bftl.thirdparty.modules.chinaums.dto.PasswordUpdateDTO;
import com.yn.bftl.thirdparty.modules.chinaums.service.AuthService;
import com.yn.bftl.thirdparty.modules.chinaums.vo.ChinaumsApplicationStatusVO;
import com.yn.bftl.thirdparty.modules.chinaums.vo.byx.ByxMchCreateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @author huabiao
 * @create 2022/12/19  11:05
 **/
@Api(value = "AuthMemberController", tags = "用户端-银联认证")
@RestController
@RequestMapping("/member/chinaums/auth")
public class AuthMemberController {

    @Resource
    private AuthService authService;

    @Resource
    HttpServletRequest request;

    @ApiOperation(value = "获取上传文件URL")
    @RequestMapping(value = "/mch/upload/url", method = RequestMethod.GET)
    public ByxMchCreateVO getFileUrl() {
//        request.setAttribute("Member-Type", OperatorType.MEMBER_USER);
        return authService.getFileUrl();
    }

    @ApiOperation(value = "二级商户-进件URL(修改)")
    @RequestMapping(value = "/mch/create/url", method = RequestMethod.GET)
    public ByxMchCreateVO getMchCreateUrl() {
//        request.setAttribute("Member-Type", OperatorType.MEMBER_USER);
        return authService.getMchCreateUrl();
    }

//    @ApiOperation(value = "二级商户-进件URL(修改)")
//    @RequestMapping(value = "/mch/create/callback", method = RequestMethod.GET)
//    public ByxMchCreateVO getMchCallBack(String registerNo) {
////        request.setAttribute("Member-Type", OperatorType.MEMBER_USER);
//        return authService.getMchCreateUrl(registerNo);
//    }

//    @ApiOperation(value = "二级商户-进件(修改)")
//    @RequestMapping(value = "/mch/update", method = RequestMethod.POST)
//    public void mchUpdate(@Valid @RequestBody MchCreateDTO mchCreateDTO) {
//        this.checkMchInfo(mchCreateDTO);
//        authService.mchUpdate(mchCreateDTO);
//    }

//    @ApiOperation(value = "二级商户-进件(创建)")
//    @RequestMapping(value = "/mch/create", method = RequestMethod.POST)
//    public void mchCreate(@Valid @RequestBody MchCreateDTO mchCreateDTO) {
//        this.checkMchInfo(mchCreateDTO);
//        authService.mchCreate(mchCreateDTO);
//    }

    @ApiOperation(value = "认证-信息")
    @RequestMapping(value = "/auth_info", method = RequestMethod.GET)
    public ChinaumsAuthInfoVO getAuthInfo() {
        return authService.getAuthInfo();
    }

    @ApiOperation(value = "认证-进件状态")
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    public ChinaumsApplicationStatusVO status() {
        return authService.status();
    }

    // TODO: 等银行确认
//    @ApiOperation(value = "支付密码校验")
//    @RequestMapping(value = "/validate-password", method = RequestMethod.GET)
//    public boolean validatePassword() {
//        return authService.status();
//    }

//    @ApiOperation(value = "个人用户-进件(创建)")
//    @RequestMapping(value = "/cus/create", method = RequestMethod.POST)
//    public void cusCreate(@Valid @RequestBody CusCreateDTO cusCreateDTO) {
//        // 判断证件有效期
//        if (cusCreateDTO.getIdCardEndTime() != null && LocalDateTime.now().until(cusCreateDTO.getIdCardEndTime(), ChronoUnit.DAYS) < 60) {
//            throw new YnceErrorException(YnceError.YNCE_218015);
//        }
//        authService.cusCreate(cusCreateDTO);
//    }

//    @ApiOperation(value = "个人用户-进件(修改)")
//    @RequestMapping(value = "/cus/update", method = RequestMethod.POST)
//    public void cusUpdate(@Valid @RequestBody CusCreateDTO cusCreateDTO){
//        // 判断证件有效期
//        if (cusCreateDTO.getIdCardEndTime() != null && LocalDateTime.now().until(cusCreateDTO.getIdCardEndTime(), ChronoUnit.DAYS) < 60) {
//            throw new YnceErrorException(YnceError.YNCE_218015);
//        }
//        authService.cusUpdate(cusCreateDTO);
//    }

    @ApiOperation(value = "支付密码-修改")
    @RequestMapping(value = "/password/update", method = RequestMethod.POST)
    public void passwordUpdate(@Valid @RequestBody PasswordUpdateDTO passwordUpdateDTO){
        authService.passwordUpdate(passwordUpdateDTO);
    }

    @ApiOperation(value = "支付密码-重置")
    @RequestMapping(value = "/password/reset", method = RequestMethod.POST)
    public void passwordReset(@Valid @RequestBody PasswordResetDTO passwordResetDTO){
        authService.passwordReset(passwordResetDTO);
    }

    private void checkMchInfo(MchCreateDTO mchCreateDTO){
        //  当营业执照证件号码不是18位时，组织机构信息和税务登记信息必填
        if (mchCreateDTO.getBusinessLicenseNumber().length() != 18
                && (StringUtils.isBlank(mchCreateDTO.getOrganizationCertCopy())
                || mchCreateDTO.getOrganizationCertCopyId() == null
                || StringUtils.isBlank(mchCreateDTO.getOrganizationCertNumber())
                || mchCreateDTO.getOrganizationCertStartTime() == null
                || mchCreateDTO.getOrganizationCertEndTime() == null
                || StringUtils.isBlank(mchCreateDTO.getTaxRegistrationCertCopy())
                || mchCreateDTO.getTaxRegistrationCertCopyId() == null
                || StringUtils.isBlank(mchCreateDTO.getTaxRegistrationCertNumber()))) {
            throw new YnceErrorException(YnceError.YNCE_218011);
        }
        // 当主体为个体工商户时，法人手机号必填
        if (mchCreateDTO.getOrganizationType().equals("2")) {
            if (StringUtils.isBlank(mchCreateDTO.getLegalPersonMobileNumber())) {
                // 法人手机号必填
                throw new YnceErrorException(YnceError.YNCE_218007);
            }
            if (mchCreateDTO.getSettleAcctType().equals("2") && StringUtils.isBlank(mchCreateDTO.getSmsCode())) {
                // 验证码必填
                throw new YnceErrorException(YnceError.YNCE_218008);
            }
        }
        // 判断营业执照有效期
        if (mchCreateDTO.getBusinessLicenseEndTime() != null && LocalDateTime.now().until(mchCreateDTO.getBusinessLicenseEndTime(), ChronoUnit.DAYS) < 60) {
            throw new YnceErrorException(YnceError.YNCE_218016);
        }
        // 判断组织机构代码证有效期
        if (mchCreateDTO.getOrganizationCertEndTime() != null && LocalDateTime.now().until(mchCreateDTO.getOrganizationCertEndTime(), ChronoUnit.DAYS) < 60) {
            throw new YnceErrorException(YnceError.YNCE_218017);
        }
        // 判断法人证件有效期
        if (mchCreateDTO.getLegalPersonIdCardEndTime() != null && LocalDateTime.now().until(mchCreateDTO.getLegalPersonIdCardEndTime(), ChronoUnit.DAYS) < 60) {
            throw new YnceErrorException(YnceError.YNCE_218018);
        }
        // 判断联系人证件有效期
        if (mchCreateDTO.getContactIdCardEndTime() != null && LocalDateTime.now().until(mchCreateDTO.getLegalPersonIdCardEndTime(), ChronoUnit.DAYS) < 60) {
            throw new YnceErrorException(YnceError.YNCE_218019);
        }
    }
}
