package com.kiisoo.ic.login.controller;

import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.login.bean.LoginBean;
import com.kiisoo.ic.login.enums.LoginEnum;
import com.kiisoo.ic.login.service.LoginService;
import com.kiisoo.ic.wx.service.QWMailListManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

import static com.kiisoo.ic.constants.Constants.ERROR_ACCOUNT_NOT_BIND;
import static com.kiisoo.ic.constants.Constants.ERROR_SYSTEM;

/**
 * @Description: 用户登入
 * @Author: wangyinjia
 * @Date: 2020/2/18
 * @Company: kiisoo
 * @Version: 1.0
 */
@Controller
@Slf4j
public class LoginController extends BaseController {

    /**
     * 登录 service
     */
    private final LoginService loginService;

    @Autowired
    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }

    /**
     * 登录账号
     * @param login 用户名
     * @param password 密码
     * @return 成功/失败
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> loginAccount(@RequestParam(value = "login")String login,
                                            @RequestParam(value = "password")String password,
                                            @RequestParam(value = "isMD5",required = false,defaultValue = "false")Boolean isMD5){
        try{
            Map<String, Object> resultMap = new HashMap<>();
            LoginBean loginBean = loginService.login(login, password,isMD5);
            resultMap.put("code", loginBean.getLoginInfo().get("code"));
            resultMap.put("results", loginBean);
            resultMap.put("success", Boolean.TRUE);
            return resultMap;
        }catch (Exception e){
            log.error("登录失败", e);
            return fail();
        }
    }

    /**
     * 小程序登录账号
     * @param loginParams 登录参数
     * @return 成功/失败
     */
    @RequestMapping(value = "/mini/login",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> miniLoginAccount(@RequestBody LoginBean loginParams){
        try{
            Map<String, Object> resultMap = new HashMap<>();
            LoginBean loginBean = loginService.miniLogin(loginParams.getLogin(), loginParams.getPassword(),false);
            resultMap.put("code", loginBean.getLoginInfo().get("code"));
            resultMap.put("results", loginBean);
            resultMap.put("success", Boolean.TRUE);
            return resultMap;
        }catch (Exception e){
            log.error("登录失败", e);
            return fail();
        }
    }

    /**
     * 登录账号
     * @param code
     * @return 成功/失败
     */
    @RequestMapping(value = "/qy/mini/login",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> qyMiniAutoLogin(@RequestParam(value = "code")String code){
        try{
            Map<String, String> accountInfo = loginService.qyMiniAutoLogin(code);
            LoginBean loginBean = null;
            if (accountInfo!=null){
                if (StringUtils.isNotBlank(accountInfo.get("login")) && StringUtils.isNotBlank(accountInfo.get("password"))){
                    loginBean = loginService.miniLogin(accountInfo.get("login"), accountInfo.get("password"), true);
                    if (accountInfo.get("cpUserId").equals(loginBean.getCpUserId())){
                        loginBean.getLoginInfo().put("code",LoginEnum.LOGIN_SUCCEED.getCode());
                    }
                }else{
                    if (StringUtils.isNotBlank(accountInfo.get("cpUserId"))){
                        String cpUserId = accountInfo.get("cpUserId");
                        loginBean = new LoginBean(ERROR_ACCOUNT_NOT_BIND);
                        loginBean.setLogin(cpUserId);
                    }
                }
            }else{
                loginBean = new LoginBean(ERROR_SYSTEM);
            }
            return data(loginBean);
        }catch (Exception e){
            log.error("登录失败", e);
            return fail();
        }
    }

    /**
     * 登录账号
     * @param loginParams 登录参数
     * @return 成功/失败
     */
    @RequestMapping(value = "/login/bind",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> loginAccountBind(@RequestBody LoginBean loginParams){
        try{
            Map<String, Object> resultMap = new HashMap<>();
            LoginBean loginBean = loginService.miniLogin(loginParams.getLogin(), loginParams.getPassword(),false);
            resultMap.put("code", loginBean.getLoginInfo().get("code"));
            if (LoginEnum.LOGIN_SUCCEED.getCode().equals(loginBean.getLoginInfo().get("code"))){
                loginService.bindQywxUserIdByUserId(loginBean.getUserId(),loginParams.getCpUserId());
            }
            resultMap.put("results", loginBean);
            resultMap.put("success", Boolean.TRUE);
            return resultMap;
        }catch (Exception e){
            log.error("登录失败", e);
            return fail();
        }
    }

    /**
     * 登出
     * @param session 用户会话
     * @return string
     */
    @RequestMapping("/logout")
    public String logOut(HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }
}
