package com.njcn.gather.tool.active.controller;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.tool.active.service.ActivateService;
import com.njcn.gather.tool.active.vo.ActivationCodePlaintext;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.*;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "设备激活管理")
@RestController
@RequestMapping("/activate")
@RequiredArgsConstructor
public class ActivateController extends BaseController {

    private final ActivateService activateService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("生成设备申请码")
    @PostMapping("/generateApplicationCode")
    public HttpResult generateApplicationCode() {
        String methodDescribe = getMethodDescribe("generateApplicationCode");
        LogUtil.njcnDebug(log, "{}，生成设备申请码", methodDescribe);
        String applicationCode = activateService.generateApplicationCode();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, applicationCode, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("验证设备激活码")
    @ApiImplicitParam(name = "params", value = "验证设备激活码参数", required = true)
    @PostMapping("/verifyActivationCode")
    public HttpResult verifyActivationCode(@RequestBody VerifyActivationCodeParams params) {
        String methodDescribe = getMethodDescribe("verifyActivationCode");
        LogUtil.njcnDebug(log, "{}，验证设备激活码\"：{}", methodDescribe, JSONUtil.toJsonStr(params));
        ActivationCodePlaintext activationCodePlaintext = activateService.verifyActivationCode(params.getActivationCode());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, activationCodePlaintext, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获取许可信息")
    @PostMapping("/getLicense")
    public HttpResult getLicense() {
        String methodDescribe = getMethodDescribe("checkLicense");
        LogUtil.njcnDebug(log, "{}，获取许可信息", methodDescribe);
        ActivationCodePlaintext activationCodePlaintext = null;
        try {
            activationCodePlaintext = activateService.readLicenseFile();
        } catch (BusinessException e) {
            methodDescribe = "无效的许可信息";
        }
        String macAddress = NetUtil.getLocalMacAddress();
        if (activationCodePlaintext == null) {
            activationCodePlaintext = new ActivationCodePlaintext();
            activationCodePlaintext.setMacAddress(macAddress);
            activationCodePlaintext.init();
        } else {
            // 校验mac地址
            String licenseMacAddress = activationCodePlaintext.getMacAddress();
            if (StrUtil.isNotEmpty(licenseMacAddress)) {
                if (!StrUtil.equals(licenseMacAddress, macAddress)) {
                    log.error("mac地址不匹配，无效的许可文件，本机mac:{},许可mac:{}", macAddress, licenseMacAddress);
                    methodDescribe = "mac地址不匹配，无效的许可文件";
                    activationCodePlaintext = new ActivationCodePlaintext();
                    activationCodePlaintext.setMacAddress(macAddress);
                    activationCodePlaintext.init();
                }
            }
        }


        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, activationCodePlaintext, methodDescribe);
    }

    @Data
    public static class VerifyActivationCodeParams {
        @ApiModelProperty(value = "激活码")
        private String activationCode;
    }


    @ApiOperation("生成设备激活码")
    @ApiImplicitParam(name = "params", value = "参数", required = true, dataType = "ApplicationCodeParams")
    @PostMapping("/generateActivationCode")
    public HttpResult generateActivationCode(@RequestBody ApplicationCodeParams params) {
        String activationCode = activateService.generateActivationCode(params.getApplicationCode(), params.getActivationModule());
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), activationCode, "");
    }

    @ApiModel("生成设备激活码参数")
    @Data
    public static class ApplicationCodeParams {
        @ApiModelProperty(value = "设备申请码", required = true)
        private String applicationCode;
        @ApiModelProperty(value = "激活模块", required = true)
        private ActivationCodePlaintext activationModule;
    }
}