package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.UserRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Api(value = "平台登录controller" ,tags = {"平台登录相关接口"})
@RequestMapping("/demay/ssm/login")
@RestController
@Slf4j
public class SsmLoginController extends SsmSysBaseController{

    @Autowired
    public SsmLoginService ssmLoginService;

    @ApiOperation(value = "平台端用户账户登陆获取token接口", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/accountLogin", method= RequestMethod.POST)
    public GenericResponse<UserResponse> accountLogin(@RequestBody UserRequest userRequest, HttpSession session) {
        GenericResponse<UserResponse> genericResponse = new GenericResponse<>();
        UserResponse userResponse = null;
        if (StringUtils.isBlank(userRequest.getUserNum())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"账号");
            return genericResponse;
        }
        if (StringUtils.isBlank(userRequest.getPassword())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"密码");
            return genericResponse;
        }
        try{
            userResponse = ssmLoginService.accountLogin(userRequest);
        }catch (SmsException e){
            log.error("SsmLoginController.accountLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmLoginController.accountLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (userResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(userResponse);
        SessionHelp.setToken(session,userResponse.getToken());
        SessionHelp.setLoginAccount(session,userResponse.getUserNum());
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "平台端用户手机验证码登陆获取token接口", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phoneLogin", method= RequestMethod.POST)
    public GenericResponse<UserResponse> phoneLogin(@RequestBody UserRequest userRequest, HttpSession session) {
        GenericResponse<UserResponse> genericResponse = new GenericResponse<>();
        UserResponse userResponse = null;
        if (StringUtils.isBlank(userRequest.getPhone())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"手机号");
            return genericResponse;
        }
        if (StringUtils.isBlank(userRequest.getPassCode())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"验证码");
            return genericResponse;
        }
        try{
            userResponse = ssmLoginService.phoneLogin(userRequest);
        }catch (SmsException e){
            log.error("SsmLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (userResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(userResponse);
        SessionHelp.setToken(session,userResponse.getToken());
        SessionHelp.setLoginAccount(session,userResponse.getUserNum());
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "平台端用户获取手机短信验证码接口", notes = "用户获取手机短信验证码接口", response = GenericResponse.class)
    @RequestMapping(path="/getSmsCode", method= RequestMethod.POST)
    public GenericResponse<UserResponse> getSmsCode(@RequestBody UserRequest userRequest,HttpSession session) {
        GenericResponse<UserResponse> genericResponse = new GenericResponse<>();
        UserResponse userResponse = null;
        userRequest.setStoreId(SessionHelp.getStoreId(session));
        try{
            userResponse = ssmLoginService.getSmsCode(userRequest);
        }catch (SmsException e){
            log.error("SsmLoginController.getSmsCode:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmLoginController.getSmsCode:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (userResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        //genericResponse.setData(userResponse);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "平台端用户修改密码接口", notes = "用户修改密码接口", response = String.class)
    @RequestMapping(path="/changePassword", method= RequestMethod.POST)
    public GenericResponse<UserResponse> changePassword(@RequestBody UserRequest userRequest,HttpSession session) {
        GenericResponse<UserResponse> genericResponse = new GenericResponse<>();
        String userNum = SessionHelp.getLoginAccount(session);
        userRequest.setUserNum(userNum);
        int i = 0;
        try{
            i = ssmLoginService.changePassword(userRequest);
        }catch (SmsException e){
            log.error("SsmLoginController.changePassword:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmLoginController.changePassword:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "平台端用户手机密码登陆获取token接口", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phonePassLogin", method= RequestMethod.POST)
    public GenericResponse<UserResponse> phonePassLogin(@RequestBody UserRequest userRequest, HttpSession session) {
        GenericResponse<UserResponse> genericResponse = new GenericResponse<>();
        UserResponse userResponse = null;
        if (StringUtils.isBlank(userRequest.getPhone())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"手机号");
            return genericResponse;
        }
        if (StringUtils.isBlank(userRequest.getPassword())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"密码");
            return genericResponse;
        }
        try{
            userResponse = ssmLoginService.phonePassLogin(userRequest);
        }catch (SmsException e){
            log.error("SsmLoginController.phonePassLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmLoginController.phonePassLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (userResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(userResponse);
        SessionHelp.setToken(session,userResponse.getToken());
        SessionHelp.setLoginAccount(session,userResponse.getUserNum());
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "平台端退出登录",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/logout")
    public GenericResponse logout( HttpSession session){
        GenericResponse genericResponse = new GenericResponse();
        UserRequest userRequest = new UserRequest();
        userRequest.setUserNum(SessionHelp.getLoginAccount(session));
        try {
            ssmLoginService.logout(userRequest,session);
        }catch (Exception e){
            log.error("SsmLoginController.logout",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
}
