package com.jiayue.insu.incloud.permisson;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.jiayue.insu.common.core.util.R;
import com.jiayue.insu.incloud.entity.User;
import com.jiayue.insu.incloud.service.UserService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.KeyPair;

/**
 * 系统权限业务类
 *
 * @author yh
 * @version 1.0
 * @since 2022/6/1 10:56
 */
@RestController
@RequestMapping("/login")
public class Login {

    @Autowired
    private UserService userService;

    private final static String privateKey ="MIICdAIBADANBgkqhkiG9w0BAQEFAASCAl4wggJaAgEAAoGBALBU3090h9fZg1Y5ix2SPBx/iITBeLA+7s43V2F16fQO3IQ0QjgedRifD+vY+uo5UIHVfnsUlH3d5+d4vgQU5Dl8j+aSaFt+c2Dk7yJpsAm3PjjeZSEu+XI7qjSbkQpZ/Y+6/ER2A4sAWigBV9hOySIcxDUdzuz6JT/9jklHAAXZAgMBAAECgYAfY1xE9odf4vZAGQosf2V35TPV/eKP7QPxbWR+ktDMgwC5/9txxeouMdJRt1Qi8YTlN63GFwm/xgqL57BbZaSiUuT5FadkAVuLj2FIp+kiHNhsuJJP+2OSPa01uGhef8uNYX0l57lzepsBeCYGoT2aq0iBm84r+POYQatgXPQJiQJBAO6mziOpNAO+DHwn03vGla+QUJyVCOZEGWI1yL09sZs8NUrDrM5OkD+SjCgBIIwRhQKlmxHoOcA4fUaGVe3CGJsCQQC9JlHplFOoiZjPi9bH9AegZgch7WZpXvuw9T8cYa2fER3DEgcn6yexfvjd3sxQ/2qy6ZvtRaKco5KKtkYdU2CbAj9G2T5ZV+IZOlJ6iVg/ZrUJac6Erbn2okDm5xCxHZ+cdkNb7rteg/RO6zKwraT78DeMkZ13s3lxl1OZnEfmTbkCQF4hvuwWpBeg+op3VllGKbF6dRw+UBQqb+md812FgulBlGUrCKKmFafwv5tzMI0yv3/wYGgRmp6HjPs5QOWBQ/cCQCMe73HBAYp1JnhMSvJ4irmRlfI9wsoC7FaEjIdAZCupYma8ST23deBpBOKUSoVY4Off5PZIuBYuKyeUUPBz0Ls=";
    private final static String publicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwVN9PdIfX2YNWOYsdkjwcf4iEwXiwPu7ON1dhden0DtyENEI4HnUYnw/r2PrqOVCB1X57FJR93efneL4EFOQ5fI/mkmhbfnNg5O8iabAJtz443mUhLvlyO6o0m5EKWf2PuvxEdgOLAFooAVfYTskiHMQ1Hc7s+iU//Y5JRwAF2QIDAQAB";
    @PostMapping("/doLogin")
    public R doLogin(@RequestBody User user ){

     /*   // 公钥加密，私钥解密
        String encryptByPublic = rsa.encryptBase64(text, KeyType.PublicKey);
        System.out.println("公钥加密\t" + encryptByPublic);
        String decryptByPrivate = rsa.decryptStr(encryptByPublic, KeyType.PrivateKey);
        System.out.println("私钥解密\t" + decryptByPrivate);

        // 私钥加密，公钥解密
        String encryptByPrivate = rsa.encryptBase64(text, KeyType.PrivateKey);
        System.out.println("私钥加密\t" + encryptByPrivate);
        String decryptByPublic = rsa.decryptStr(encryptByPrivate, KeyType.PublicKey);
        System.out.println("公钥解密\t" + decryptByPublic);*/


        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), privateKey, publicKey);
        // 私钥解密公钥加密后的密文
        String inputPwd = rsa.decryptStr(user.getPassword(), KeyType.PrivateKey);

        //User userTar = userService.findByNameAndPwd(user.getUsername(), decrypt);
        User userTar = userService.findByName(user.getUsername());

        if(userTar!=null){
            String dbPwd = rsa.decryptStr(userTar.getPassword(), KeyType.PrivateKey);
            if(dbPwd.equals(inputPwd)){
                StpUtil.login(userTar.getUsername());
                return R.ok(StpUtil.getTokenInfo());
            }else{
                return R.ok(null);
            }

        }else{
            return R.ok(null);
        }
    }
    //加密解密工具
    public static void main(String[] args) {
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), privateKey, publicKey);
        //加密
        String pwd ="";
        String encryptByPublic = rsa.encryptBase64(pwd, KeyType.PublicKey);
        System.out.println(encryptByPublic);
        //解密
        String pwden ="";
        String decryptByPrivate = rsa.decryptStr(pwden, KeyType.PrivateKey);
        System.out.println(decryptByPrivate);
    }

    @PostMapping("/logout")
    public R logout() {
        try {
            StpUtil.logout();
        }catch (Exception e){
            e.printStackTrace();
        }

        return R.ok();
    }
    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("/isLogin")
    public R isLogin() {
        return R.ok( StpUtil.isLogin());

    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("/getLoginId")
    public R getLoginId() {
        return R.ok( StpUtil.getLoginId());

    }



}
