package com.sunyard.chsm.controller;

import com.sunyard.chsm.auth.AuthCode;
import com.sunyard.chsm.constant.AuthCodeConst;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.param.*;
import com.sunyard.chsm.service.AsymKeyService;
import org.springframework.beans.factory.annotation.Autowired;
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;

import javax.validation.Valid;

/**
 * 非对称运算类接口
 *
 * @author liulu
 * @since 2024/12/20
 */
@RestController
@RequestMapping("/asym")
public class AsymKeyController {

    @Autowired
    private AsymKeyService asymKeyService;

    /**
     * 导出非对称公钥
     *
     * @param req
     * @return
     */
    @PostMapping("/exportPubKey")
    public R<ExportPubKeyResp> exportPubKey(@Valid @RequestBody ExportPubKeyReq req) {
        ExportPubKeyResp resp = asymKeyService.exportPubKey(req);
        return R.data(resp);
    }

    /**
     * 非对称加密
     *
     * @param req
     * @return
     */
    @PostMapping("/encrypt")
    @AuthCode(AuthCodeConst.asym_enc)
    public R<AsymEncryptResp> encrypt(@Valid @RequestBody AsymEncryptReq req) {
        AsymEncryptResp resp = asymKeyService.encrypt(req);
        return R.data(resp);
    }

    /**
     * 非对称解密
     *
     * @param req
     * @return
     */
    @PostMapping("/decrypt")
    @AuthCode(AuthCodeConst.asym_enc)
    public R<AsymDecryptResp> decrypt(@Valid @RequestBody AsymDecryptReq req) {
        AsymDecryptResp resp = asymKeyService.decrypt(req);
        return R.data(resp);
    }

    /**
     * RAW签名
     *
     * @param req
     * @return
     */
    @PostMapping("/sign/raw")
    @AuthCode(AuthCodeConst.sign_raw)
    public R<AsymSignRawResp> signRaw(@Valid @RequestBody AsymSignRawReq req) {
        AsymSignRawResp resp = asymKeyService.signRaw(req);
        return R.data(resp);
    }

    /**
     * RAW验签
     *
     * @param req
     * @return
     */
    @PostMapping("/verify/raw")
    @AuthCode(AuthCodeConst.verify_raw)
    public R<VerifyResp> verifyRaw(@Valid @RequestBody AsymVerifyRawReq req) {
        VerifyResp resp = asymKeyService.verifyRaw(req);
        return R.data(resp);
    }

    /**
     * P7 Attach签名
     *
     * @param req
     * @return
     */
    @PostMapping("/sign/P7Attach")
    @AuthCode(AuthCodeConst.sign_P7Attach)
    public R<AsymSignP7Resp> signP7Attach(@Valid @RequestBody AsymSignP7Req req) {
        AsymSignP7Resp resp = asymKeyService.signP7Attach(req);
        return R.data(resp);
    }

    /**
     * P7 Attach验签
     *
     * @param req
     * @return
     */
    @PostMapping("/verify/P7Attach")
    @AuthCode(AuthCodeConst.verify_P7Attach)
    public R<VerifyResp> verifyP7Attach(@Valid @RequestBody AsymVerifyP7Req req) {
        VerifyResp resp = asymKeyService.verifyP7Attach(req);
        return R.data(resp);
    }

    /**
     * P7 Detach签名
     *
     * @param req
     * @return
     */
    @PostMapping("/sign/P7Detach")
    @AuthCode(AuthCodeConst.sign_P7Detach)
    public R<AsymSignP7Resp> signP7Detach(@Valid @RequestBody AsymSignP7Req req) {
        AsymSignP7Resp resp = asymKeyService.signP7Detach(req);
        return R.data(resp);
    }

    /**
     * P7 Detach验签
     *
     * @param req
     * @return
     */
    @PostMapping("/verify/P7Detach")
    @AuthCode(AuthCodeConst.verify_P7Detach)
    public R<VerifyResp> verifyP7Detach(@Valid @RequestBody AsymVerifyP7Req req) {
        VerifyResp resp = asymKeyService.verifyP7Detach(req);
        return R.data(resp);
    }

    /**
     * P7数字信封加封
     *
     * @param req
     * @return
     */
    @PostMapping("/envelope/seal")
    @AuthCode(AuthCodeConst.envelope_seal)
    public R<AsymEnvelopeSealResp> envelopeSeal(@Valid @RequestBody AsymEnvelopeSealReq req) {
        AsymEnvelopeSealResp resp = asymKeyService.envelopeSeal(req);
        return R.data(resp);
    }

    /**
     * P7数字信封解封
     *
     * @param req
     * @return
     */
    @PostMapping("/envelope/unseal")
    @AuthCode(AuthCodeConst.envelope_unseal)
    public R<AsymEnvelopeUnsealResp> envelopeUnseal(@Valid @RequestBody AsymEnvelopeUnsealReq req) {
        AsymEnvelopeUnsealResp resp = asymKeyService.envelopeUnseal(req);
        return R.data(resp);
    }

    /**
     * P7带签名的数字信封加封
     *
     * @param req
     * @return
     */
    @PostMapping("/signedEnvelope/seal")
    @AuthCode(AuthCodeConst.signed_envelope_seal)
    public R<AsymEnvelopeSealResp> signedEnvelopeSeal(@Valid @RequestBody AsymEnvelopeSealReq req) {
        AsymEnvelopeSealResp resp = asymKeyService.signedEnvelopeSeal(req);
        return R.data(resp);
    }

    /**
     * P7带签名的数字信封解封
     *
     * @param req
     * @return
     */
    @PostMapping("/signedEnvelope/unseal")
    @AuthCode(AuthCodeConst.signed_envelope_unseal)
    public R<AsymEnvelopeUnsealResp> signedEnvelopeUnseal(@Valid @RequestBody AsymEnvelopeUnsealReq req) {
        AsymEnvelopeUnsealResp resp = asymKeyService.signedEnvelopeUnseal(req);
        return R.data(resp);
    }


}
