package com.dxhy.admin.sys.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.xxl.sso.core.login.SsoWebLoginHelper;
import com.xxl.sso.core.util.UserUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dxhy.admin.sys.config.AdminConfig;
import com.dxhy.admin.sys.service.SsoLoginService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.R;
import com.xxl.sso.core.entity.ReturnT;
import com.xxl.sso.core.user.SsoUser;
import com.xxl.sso.core.util.CookieUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@Controller
@RequestMapping("/api")
@Slf4j
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
public class SsoLoginController extends AbstractController {

    @Resource
    private SsoLoginService ssoLoginService;

    @RequestMapping("/checkLogin")
    public ResponseEntity<?> checkLogin() {
        return ResponseEntity.ok(R.ok().put("data", AdminConfig.loginUrl));
    }

    @RequestMapping({"/sso/setcookie"})
    @ResponseBody
    @SysLog("进项平台ITAX用户登录，存储用户信息")
    public ResponseEntity<?> setcookie(String dxhy_sso_sessionid, HttpServletRequest request,
        HttpServletResponse response) {
        log.info("sessionid 是" + dxhy_sso_sessionid);
//        SsoUser user = SsoWebLoginHelper.loginCheck(request, response);
        SsoUser user = UserUtil.getUser();
        log.info("用户信息：" + JSON.toJSONString(user));
        try {
            if (user != null) {
                boolean saveUser = ssoLoginService.saveUser(user);
                log.info("保存机构信息返回标志：" + saveUser + " 返回用户信息为：" + user.getUsername());
                if (saveUser) {
                    return ResponseEntity.ok(R.ok().put("data", user.getUsername()));
                } else {
                    return ResponseEntity.ok(R.error());
                }
            } else {
                return ResponseEntity.ok(R.error());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error());
        }
    }

    @RequestMapping({"/sso/getUserInfo"})
    @ResponseBody
    public ResponseEntity<?> getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        log.info("已登录用户获取用户信息");
//        SsoUser xxlUser = SsoWebLoginHelper.loginCheck(request, response);
        SsoUser xxlUser = UserUtil.getUser();
        return ResponseEntity.ok(R.ok().put("data", xxlUser));
    }

    @RequestMapping({"/sso/updateUserInfo"})
    @ResponseBody
    public ReturnT<SsoUser> updateUserInfo(HttpServletRequest request, HttpServletResponse response, Long deptId) {
        log.info("更新登录用户信息");
//        SsoUser xxlUser = SsoWebLoginHelper.loginCheck(request, response);
        SsoUser xxlUser = UserUtil.getUser();
        xxlUser.setCurrentDeptId(deptId);
        String cookieSessionId = CookieUtil.getValue(request, "dxhy_sso_sessionid");
//        SsoWebLoginHelper.updateUserInfo(xxlUser, cookieSessionId);
        UserUtil.updateUserCurrentDeptId(deptId);
        return new ReturnT<>(xxlUser);
    }

    @RequestMapping(value = "/sso/saveMenu", method = RequestMethod.POST)
    @ResponseBody
    public void saveMenu() {
        log.info("定时任务菜单信息开始");
        ssoLoginService.saveMenu();
    }

    @RequestMapping({"/sso/setTokenUser"})
    @ResponseBody
    @SysLog("进项平台统一用户中心存储用户信息")

    public ResponseEntity<?> setTokenUser(String token) {
        log.info("统一用户中心,根据TOKEN获取用户信息：{}", token);
        try {
            Map<String, String> userToken = ssoLoginService.setTokenUser(token);
            log.info("统一用户中心，获取到的用户信息为：{}", JSON.toJSONString(userToken));
            return ResponseEntity.ok(R.ok().put("data", userToken));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error());
        }

    }
}
