package com.lightinit.hsdatagateway.modules.api.gateway;

import com.lightinit.hsdatagateway.common.exceptions.BaseException;
import com.lightinit.hsdatagateway.common.utils.StringUtils;
import com.lightinit.hsdatagateway.modules.foreign.model.ReturnResult;
import com.lightinit.hsdatagateway.modules.foreign.util.ApiEncryptUtil;
import com.lightinit.hsdatagateway.modules.gateway.entity.BizSign;
import com.lightinit.hsdatagateway.modules.gateway.service.BizSignService;
import com.lightinit.hsdatagateway.modules.foreign.model.ReturnResult;
import com.lightinit.hsdatagateway.modules.foreign.util.ApiEncryptUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import java.security.KeyPair;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("${adminPath}/api/sign")
public class SignApiController {
    @Autowired
    private BizSignService bizSignService;

    /**
     * 查询
     *
     * @param bizSign
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ReturnResult list(BizSign bizSign) {
        List<BizSign> bizSignList = bizSignService.findListByApi(bizSign);
        return new ReturnResult(bizSignList);
    }


    /**
     * 新增
     *
     * @param bizSign
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResult add(@RequestBody BizSign bizSign) throws Exception {
        if(StringUtils.isBlank(bizSign.getAppId())){
            return new ReturnResult("500","应用ID不能为空");
        }
        if(StringUtils.isBlank(bizSign.getUserId())){
            return new ReturnResult("500","用户ID不能为空");
        }
        if(StringUtils.isBlank(bizSign.getSignStatus())){
            return new ReturnResult("500","签名状态不能为空");
        }
        KeyPair keyPair = ApiEncryptUtil.getKeyPair();
        String publicKey = "";
        if (StringUtils.isBlank(bizSign.getPrivateKey())) {
            String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
            bizSign.setPrivateKey(privateKey);
        }
        if (StringUtils.isBlank(bizSign.getPublicKey())) {
            publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
            bizSign.setPublicKey(publicKey);
        }
        bizSign.setCreatedAt(new Date());
        if (bizSignService.isRepetition(bizSign)) {
            bizSignService.save(bizSign);
            return new ReturnResult(publicKey, "success", "code", "签名管理保存成功");
        }
        return new ReturnResult("500","应用ID、用户ID重复，操作失败");

    }

    /**
     * 根据id查询签名信息
     *
     * @param bizSign
     * @return
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ResponseBody
    public ReturnResult selectById(BizSign bizSign) {
        if (Objects.isNull(bizSign)) {
            throw new BaseException("500", "请求信息不能为空");
        }
        BizSign sign = bizSignService.get(bizSign);
        return new ReturnResult(sign);
    }


    /**
     * 修改
     *
     * @param bizSign
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResult update(@RequestBody BizSign bizSign) {
        if(bizSign.getId() == null){
            return new ReturnResult("500","主键ID不能为空");
        }
        if(StringUtils.isBlank(bizSign.getAppId())){
            return new ReturnResult("500","应用ID不能为空");
        }
        if(StringUtils.isBlank(bizSign.getUserId())){
            return new ReturnResult("500","用户ID不能为空");
        }
        if(StringUtils.isBlank(bizSign.getSignStatus())){
            return new ReturnResult("500","签名状态不能为空");
        }
        if(StringUtils.isBlank(bizSign.getPublicKey())){
            return new ReturnResult("500","公钥不能为空");
        }
        if(StringUtils.isBlank(bizSign.getPrivateKey())){
            return new ReturnResult("500","私钥不能为空");
        }
        if (bizSignService.isRepetition(bizSign)) {
            bizSignService.updateById(bizSign);
        }
        return new ReturnResult();
    }


    /**
     * 删除
     *
     * @param bizSign
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ReturnResult delete(BizSign bizSign) {
        if (bizSign.getId() == null) {
            throw new BaseException("500", "主键ID不能为空");
        }
        bizSign.setDeletedAt(new Date());
        bizSignService.delete(bizSign);
        return new ReturnResult();
    }


}
