package com.casic.missiles.controller;

import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.base.response.SuccessResponseData;
import com.casic.missiles.core.log.LogManager;
import com.casic.missiles.core.log.factory.LogTaskFactory;
import com.casic.missiles.core.shiro.ShiroKit;
import com.casic.missiles.core.shiro.ShiroUser;
import com.casic.missiles.core.util.RSAUtils;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.constant.PermissionContant;
import com.casic.missiles.modular.system.dto.LoginResult;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import static com.casic.missiles.core.support.HttpKit.getIp;

/**
 * 仅为mock登录使用
 */
@Controller
@RequestMapping("/route")
public class MockController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MockController.class);

    /**
     * 获取mockToken
     */
    @GetMapping("/sync")
    @ResponseBody
    public Object sync(String batchId) {
        System.out.println("成功调用" + batchId);
        return ResponseData.success();
    }

    /**
     * 获取mockToken
     */
    @GetMapping("/mockToken")
    @ResponseBody
    public Object mockToken(HttpServletRequest request, String username, String password) {
        super.getSession().setAttribute(PermissionContant.IS_APP, false);
        SuccessResponseData resultData = new SuccessResponseData();
        if (ToolUtil.isOneEmpty(username, password)) {
            username = "admin";
            password = "111111";
        }
        Subject currentUser = ShiroKit.getSubject();

        try {
            Map<String, String> key = RSAUtils.genKeyPair();
            ShiroKit.getSession().setAttribute(PermissionContant.PRIVATE_KEY, key.get(RSAUtils.RSAPrivateKey));
            logger.info("秘钥:".concat(key.get(RSAUtils.RSAPublicKey)));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        UsernamePasswordToken token = new UsernamePasswordToken(username, password.toCharArray());
        token.setRememberMe(false);
        try {
            currentUser.login(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ShiroUser shiroUser = ShiroKit.getUser();
        super.getSession().setAttribute("shiroUser", shiroUser);
        super.getSession().setAttribute("username", shiroUser.getAccount());

        super.getSession().setAttribute(PermissionContant.SESSION_KEY, shiroUser.getId());

        LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));

        ShiroKit.getSession().setAttribute("sessionFlag", true);
        resultData.setData(new LoginResult(ShiroKit.getSession().getId().toString(), null));
        resultData.setMessage("登录成功");
        return resultData;
    }

}
