package cokr.xit.ens.modules.common.ctgy.sys.util.presentation;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.core.utils.crypto.AES256;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "CryptoController")
@Slf4j
@RequiredArgsConstructor
@RestController
public class CryptoController {


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "AES256 암호화"
                            , value = "{\"key\":\"0e1n2s3e4n5c6r7y8p9t256secretkey\",\"raws\":[\"8611281234567\",\"abcdefg\"]}")
            })
    })
    @Operation(summary = "AES256 암호화(일괄)")
    @PostMapping(value = "/sys/util/crypto/aes/enc/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> aesEncBulk(@RequestBody Map<String, Object> mParam) {
        EnsResponseVO responseVO = null;
        try {
            AES256 aes256 = new AES256((String) mParam.get("key"));
            List<String> raws = (List<String>) mParam.get("raws");
            List<Map<String, String>> resultInfo = raws.stream()
                    .map(s -> {
                        HashMap<String, String> m = new HashMap<>();
                        m.put(s, aes256.encrypt(s));
                        return m;
                    })
                    .collect(Collectors.toList());
            responseVO = EnsResponseVO.okBuilder()
                    .resultInfo(resultInfo)
                    .build();

        } catch (StringIndexOutOfBoundsException e) {
            responseVO = EnsResponseVO.errBuilder()
                    .errCode(EnsErrCd.ERR403)
                    .errMsg("key 값이 유효하지 않습니다.:::" + e.getMessage())
                    .build();
        }

        return new ResponseEntity<EnsResponseVO>(responseVO, HttpStatus.OK);
    }

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "AES256 복호화"
                            , value = "{\"key\":\"0e1n2s3e4n5c6r7y8p9t256secretkey\",\"raws\":[\"BtdvjQMMvHC0Qz8gQhZm7Q==\",\"KhzYssTY2yWJhOhg801nFQ==\"]}")
            })
    })
    @Operation(summary = "AES256 복호화(일괄)")
    @PostMapping(value = "/sys/util/crypto/aes/dec/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> aesDecBulk(@RequestBody Map<String, Object> mParam) {
        EnsResponseVO responseVO = null;
        try {

            AES256 aes256 = new AES256((String) mParam.get("key"));
            List<String> raws = (List<String>) mParam.get("raws");
            List<Map<String, String>> resultInfo = raws.stream()
                    .map(s -> {
                        HashMap<String, String> m = new HashMap<>();
                        m.put(s, aes256.decrypt(s));
                        return m;
                    })
                    .collect(Collectors.toList());
            responseVO = EnsResponseVO.okBuilder()
                    .resultInfo(resultInfo)
                    .build();

        } catch (StringIndexOutOfBoundsException e) {
            responseVO = EnsResponseVO.errBuilder()
                    .errCode(EnsErrCd.ERR403)
                    .errMsg("key 값이 유효하지 않습니다.:::" + e.getMessage())
                    .build();
        }

        return new ResponseEntity<EnsResponseVO>(responseVO, HttpStatus.OK);
    }


}
