package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.MemberRequest;
import com.ssm.manage.response.MemberResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmMemberLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value="会员登录controller",tags={"会员登录相关接口"})
@RequestMapping("/demay/ssm/memberLogin")
@RestController
@Slf4j
public class SsmMemberLoginController {

    @Autowired
    public SsmMemberLoginService ssmMemberLoginService;



    @ApiOperation(value = "手机端会员手机验证码登陆获取token接口", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phoneLogin", method= RequestMethod.POST)
    public GenericResponse phoneLogin(@RequestBody MemberRequest memberRequest, HttpSession session) {
        GenericResponse<MemberResponse> genericResponse = new GenericResponse<>();
        MemberResponse memberResponse = null;
        try{
            memberResponse = ssmMemberLoginService.phoneLogin(memberRequest);
        }catch (SmsException e){
            log.error("SsmMemberLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (memberResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(memberResponse);
        SessionHelp.setToken(session,memberResponse.getToken());
        SessionHelp.setLoginAccount(session,memberResponse.getMobilePhone());
        SessionHelp.setStoreId(session,memberResponse.getStoreId());
        SessionHelp.setStoreName(session,memberResponse.getStoreName());
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "手机端会员获取手机短信验证码接口", notes = "会员获取手机短信验证码接口", response = GenericResponse.class)
    @RequestMapping(path="/getSmsCode", method= RequestMethod.POST)
    public GenericResponse getSmsCode(@RequestBody MemberRequest memberRequest) {
        GenericResponse<MemberResponse> genericResponse = new GenericResponse<>();
        MemberResponse memberResponse = null;
        try{
            memberResponse = ssmMemberLoginService.getSmsCode(memberRequest);
        }catch (SmsException e){
            log.error("SsmMemberLoginController.getSmsCode:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberLoginController.getSmsCode:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (memberResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(memberResponse);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "手机端会员修改密码接口", notes = "会员修改密码接口", response = String.class)
    @RequestMapping(path="/changePassword", method= RequestMethod.POST)
    public GenericResponse changePassword(@RequestBody MemberRequest memberRequest,HttpSession session) {
        GenericResponse<MemberResponse> genericResponse = new GenericResponse<>();
        String phone = SessionHelp.getLoginAccount(session);
        memberRequest.setMobilePhone(phone);
        int i = 0;
        try{
            i = ssmMemberLoginService.changePassword(memberRequest);
        }catch (SmsException e){
            log.error("SsmMemberLoginController.changePassword:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberLoginController.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 phonePassLogin(@RequestBody MemberRequest memberRequest, HttpSession session) {
        GenericResponse<MemberResponse> genericResponse = new GenericResponse<>();
        MemberResponse memberResponse = null;
        try{
            memberResponse = ssmMemberLoginService.phonePassLogin(memberRequest);
        }catch (SmsException e){
            log.error("SsmMemberLoginController.phonePassLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberLoginController.phonePassLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (memberResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(memberResponse);
        SessionHelp.setToken(session,memberResponse.getToken());
        SessionHelp.setLoginAccount(session,memberResponse.getMobilePhone());
        SessionHelp.setStoreId(session,memberResponse.getStoreId());
        SessionHelp.setStoreName(session,memberResponse.getStoreName());
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "退出登录",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/logout")
    public GenericResponse logout (HttpSession session){
        GenericResponse genericResponse = new GenericResponse();
        MemberRequest memberRequest = new MemberRequest();
        memberRequest.setMobilePhone(SessionHelp.getLoginAccount(session));
        try {
            ssmMemberLoginService.logout(memberRequest,session);
        }catch (Exception e){
            log.error("SsmMemberLoginController.logout",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
}
