package cn.estsh.i3plus.core.apiservice.controller;

import cn.estsh.i3plus.core.api.iservice.base.ISystemLoginService;
import cn.estsh.i3plus.platform.common.tool.EncryptTool;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description :
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-10-13 18:17
 * @Modify:
 **/
@RestController
@Api(description="登陆授权demo")
@RequestMapping(PlatformConstWords.BASE_URL + "/demo")
public class DemoAuthController extends CoreBaseController {
    public static final Logger LOGGER = LoggerFactory.getLogger(DemoAuthController.class);

    @Autowired
    private ISystemLoginService systemLoginService;

    @GetMapping(value="/login")
    @ApiOperation(value="登陆",notes="登陆")
    public ResultBean login(String loginName,String loginPwd,String languageCode){
        LOGGER.info("用户登陆：{},{},{}",loginName,loginPwd,languageCode);
        try {
            SessionUser sUser = systemLoginService.queryUserLogin(loginName,loginPwd,languageCode);
            LOGGER.info("会员登陆：{}",sUser);
            AuthUtil.setSessionObject("languageCode",languageCode);
            return new ResultBean(true,"");
        } catch (ImppBusiException e) {
            return new ResultBean(false,e.getErrorDetail() + "-[解决]" + e.getErrorSolution());
        }
    }

    /**
     *
     * @return
     */
    @GetMapping(value="/logout")
    @ApiOperation(value="登出",notes="登出")
    public ResultBean logout(){
        LOGGER.info("用户退出...");
        try {
            AuthUtil.logout();
            return new ResultBean(true,"");
        } catch (ImppBusiException e) {
            return new ResultBean(false,e.getErrorDetail() + "-[解决]" + e.getErrorSolution());
        }
    }

    /**
     *
     * @return
     */
    @GetMapping(value="/get-user-info")
    @ApiOperation(value="登出",notes="登出")
    public ResultBean getUserInfo(HttpServletRequest request){
        LOGGER.info("获取用户信息...lang:{}",AuthUtil.getSessionAttribute("languageCode"));
        try {
            SessionUser sUser = AuthUtil.getSessionUser();
            LOGGER.info("用户：{}",sUser);
            Enumeration headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                String value = request.getHeader(key);
                System.out.println("k : "+key + "  ,  v : " + value);
            }

            return new ResultBean(true,"",sUser);
        } catch (ImppBusiException e) {
            return new ResultBean(false,e.getErrorDetail() + "-[解决]" + e.getErrorSolution());
        }
    }

    @GetMapping(value="/adlogin")
    @ApiOperation(value="管理员登陆",notes="管理员登陆")
    public ResultBean adlogin(String loginName,String loginPwd,String languageCode){
        LOGGER.info("管理员登陆：{},{},{}",loginName,loginPwd,languageCode);

        try {
            systemLoginService.queryAdminLogin(loginName,EncryptTool.hexMD5(loginPwd),languageCode);

            return new ResultBean(true,"");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBean(true,"");
        }
    }

    @GetMapping(value="/operate/index")
    @ApiOperation(value="用户方法",notes="")
    public ResultBean userIndex(){
        LOGGER.info("用户方法");

        return new ResultBean(true,"");
    }

    @GetMapping(value="/manager/index")
    @ApiOperation(value="管理员方法",notes="")
    public ResultBean adminIndex(){
        LOGGER.info("管理员方法");

        return new ResultBean(true,"");
    }

    @GetMapping(value="/samanager/index")
    @ApiOperation(value="超级管理员方法",notes="")
    public ResultBean saIndex(){
        LOGGER.info("超级管理员方法");

        return new ResultBean(true,"");
    }
}
