package com.fzm.mall.server.admin.auxiliary.controller;

import com.fzm.mall.server.admin.chain.FzmChain;
import com.fzm.mall.server.admin.config.Redis;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.enums.response.MallResponseEnum;
import com.fzm.mall.server.admin.enums.sms.CodeTypeEnum;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.util.AliSmsUtils;
import com.fzm.mall.server.admin.util.CodeUtil;
import com.fzm.mall.server.admin.util.PhoneFormatCheckUtils;
import com.fzm.mall.server.admin.util.SmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author wangp
 * @date 2021/4/6 17:52
 * @description 短信
 * @since JDK 1.8
 */
@Api(tags = "辅助接口-短信")
@RestController
@RequestMapping("/sms")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SmsController {

    private final ResponseFactory resFac;
    private final Redis redis;
    private final FzmChain fzmChain;

    @ApiOperation(value = "发送验证码")
    @GetMapping("/common/sendVerifyCode")
    public ResponseVO<String> sendAuthSms2(@ApiParam(name = "phone", value = "手机") @RequestParam String phone,
                                           @ApiParam(name = "type", value = "验证码类型") @RequestParam Integer type) {
        //TODO 短信模板
        if (!PhoneFormatCheckUtils.isChinaPhoneLegal(phone)) {
            return resFac.getResponse(MallResponseError.PHONE_FORMAT__ERROR);
        }

        String templateId = "";
        String redisKey = "";

        switch (Objects.requireNonNull(CodeTypeEnum.getByType(type))) {
            case REGISTER:
                templateId = "674a92e2616544d3a3dc7ea4bc60b6d3";
                redisKey = MALLGlobalConfig.PHONE_CODE_MERCHANT_REGISTER_REDIS_KEY_SUFFIX;
                break;
            case SEARCH:
                templateId = "674a92e2616544d3a3dc7ea4bc60b6d3";
                redisKey = MALLGlobalConfig.PHONE_CODE_REGISTER_STATUS_REDIS_KEY_SUFFIX;
                break;
            case REPAIR_PWD:
                templateId = "674a92e2616544d3a3dc7ea4bc60b6d3";
                redisKey = MALLGlobalConfig.PHONE_CODE_REPAIR_PWD_REDIS_KEY_SUFFIX;
                break;
            case AUTHENTICATION:
                templateId = "674a92e2616544d3a3dc7ea4bc60b6d3";
                redisKey = MALLGlobalConfig.PHONE_CODE_IDENTITY_REDIS_KEY_SUFFIX;
                break;
        }

        String code = CodeUtil.genValidateCode();
        redis.setKey(phone + redisKey, code, 5 * 60);

        boolean result = SmsUtil.sendSms(phone, true, templateId, code);
        String resCode = result ? MallResponseEnum.SUCCESS.code() : MallResponseEnum.FAIL.code();

        return resFac.getResponse(resCode);
    }

    @ApiOperation(value = "发送验证码")
    @GetMapping("/sendVerifyCode")
    public ResponseVO<String> sendAuthSms(@ApiParam(name = "phone", value = "手机") @RequestParam String phone,
                                          @ApiParam(name = "type", value = "验证码类型") @RequestParam Integer type) {
        //TODO 短信模板
        if (!PhoneFormatCheckUtils.isChinaPhoneLegal(phone)) {
            return resFac.getResponse(MallResponseError.PHONE_FORMAT__ERROR);
        }

        String templateId = "";
        String redisKey = "";

        switch (Objects.requireNonNull(CodeTypeEnum.getByType(type))) {
            case REGISTER:
//                templateId = "SMS_226745226";
                templateId = AliSmsUtils.tempId;
                redisKey = MALLGlobalConfig.PHONE_CODE_MERCHANT_REGISTER_REDIS_KEY_SUFFIX;
                break;
            case SEARCH:
                templateId = "674a92e2616544d3a3dc7ea4bc60b6d3";
                redisKey = MALLGlobalConfig.PHONE_CODE_REGISTER_STATUS_REDIS_KEY_SUFFIX;
                break;
            case REPAIR_PWD:
//                templateId = "SMS_226745226";
                templateId = AliSmsUtils.tempId;
                redisKey = MALLGlobalConfig.PHONE_CODE_REPAIR_PWD_REDIS_KEY_SUFFIX;
                break;
            case AUTHENTICATION:
//                templateId = "SMS_226745226";
                templateId = AliSmsUtils.tempId;
                redisKey = MALLGlobalConfig.PHONE_CODE_IDENTITY_REDIS_KEY_SUFFIX;
                break;
        }

        String code = CodeUtil.genValidateCode();
        redis.setKey(phone + redisKey, code, 5 * 60);

//        boolean result = SmsUtil.sendSms(phone, true, templateId, code);
        boolean result = AliSmsUtils.sendCode(phone, code, templateId, false);
        String resCode = result ? MallResponseEnum.SUCCESS.code() : MallResponseEnum.FAIL.code();

        return resFac.getResponse(resCode);
    }


    @ApiOperation(value = "备用接口")
    @GetMapping("/bate")
    public ResponseVO bate() {
        String ziyin = fzmChain.accountCreate("30dfdc0977e343a5919b527448a7123f_extern");
        String wangyueyuan = fzmChain.accountCreate("7ea9ef05102b4164859349b5b2c873bc_extern");
        String jiangleibo = fzmChain.accountCreate("54dc9b99b4984b579edc8ea75aead1b1_extern");
        String zhangsan = fzmChain.accountCreate("7adbb644c5da491192e8d6ca4e47008e_extern");
        String ceshi = fzmChain.accountCreate("1978a03c3b2946ad8188d3d1a7148dde_extern");
        Map<String, String> map = new HashMap<>();
        map.put("ziyin", ziyin);
        map.put("wangyueyuan", wangyueyuan);
        map.put("jiangleibo", jiangleibo);
        map.put("zhangsan", zhangsan);
        map.put("ceshi", ceshi);
        return resFac.getSuccessResponseWithData(map);
    }
}