package com.tax.token.web.controller;

import com.tax.token.constants.TokenConstants;
import com.tax.token.integration.user.UserClient;
import com.tax.token.model.SmsSendReq;
import com.tax.token.service.api.SmsRecordService;
import com.yxcheng.token.common.annotation.LogT;
import com.yxcheng.token.common.controller.BaseClientController;
import com.yxcheng.token.common.enumutil.ResultEnum;
import com.yxcheng.token.common.model.CrRpcResult;
import com.yxcheng.token.common.model.JsonResult;
import com.yxcheng.token.utils.Base64Util;
import com.yxcheng.token.utils.StringUtil;
import com.yxcheng.token.utils.UUIDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
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;

/**
 * @ProjectName: tax-sys
 * @Package: com.tax.token.web.controller
 * @ClassName: SmsController
 * @Author: ddx
 * @Description: ${description}
 * @Date: 2022-11-24 11:53
 * @Version: 1.0
 */
@RestController
@RequestMapping("/client")
@ComponentScan(basePackages = {"com.tax.token.service"})
public class SmsController extends BaseClientController {

    private static final Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    private SmsRecordService smsRecordService;

    @Autowired
    private UserClient userClient;

    @RequestMapping(value ="/smsReceive" , method = RequestMethod.POST)
    @LogT
    public JsonResult<String> smsReceive(@RequestBody SmsSendReq smsSendReq){
        CrRpcResult crRpcResult = userClient.isSafe(smsSendReq);
        if(!checkUserSafe(crRpcResult)){
            return JsonResult.errorResult(crRpcResult.getCode(),crRpcResult.getMessage());
        }
        String uuId = UUIDGenerator.generate();

        String cellphone = smsSendReq.getCellphone();
        String requestUid = smsSendReq.getSignatureNonce();
        String smsCode = smsSendReq.getSmsCode();
        String smsDate = smsSendReq.getSmsDate();
        String smsFrom = "2";
        String sendCellphone = smsSendReq.getSendCellphone();
        String zoneCode = smsSendReq.getZoneCode();
        String validCode = smsSendReq.getValidCode();

        boolean checkFlag = false;
        JsonResult<String> jsonResult = new JsonResult();
        if(StringUtil.isNotEmpty(zoneCode) && StringUtil.isNotEmpty(validCode)
                && (!(StringUtil.isNumericString(zoneCode) && StringUtil.isNumericString(validCode)))) {
            checkFlag = true;
            jsonResult.errorResponse(ResultEnum.param_error);
        } else if(StringUtil.isEmpty(cellphone) || StringUtil.isEmpty(requestUid) || checkFlag && StringUtil.isEmpty(smsCode)) {
            jsonResult.errorResponse(ResultEnum.param_error);
        } else {
            try {
            if(StringUtil.isNotEmpty(smsCode)) {
                smsCode = new String(Base64Util.decode(smsCode), "UTF-8");
            }
            jsonResult = smsRecordService.saveSmsCode(cellphone, requestUid, smsCode, smsDate, smsFrom,
                    sendCellphone, zoneCode, validCode, smsSendReq.getAccessKeyID(), TokenConstants.FUNCTION_NO_FPY, smsSendReq);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.errorResponse(ResultEnum.system_error);
                logger.error(uuId + " cellphone is " + cellphone + " send sms is error!" + e.getMessage() );
            }
        }
        return jsonResult;
    }

}
