package com.zhongshu.card.server.core.controller.user;

import com.github.microservice.auth.client.model.LoginTokenModel;
import com.github.microservice.auth.client.model.UserTokenModel;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultState;
import com.zhongshu.card.client.model.base.ReTokenParam;
import com.zhongshu.card.client.model.login.LoginCommonModel;
import com.zhongshu.card.client.model.org.GetPhoneModel;
import com.zhongshu.card.client.model.org.LoginParam;
import com.zhongshu.card.client.model.org.LoginPlatformTokenModel;
import com.zhongshu.card.client.type.LoginFromType;
import com.zhongshu.card.server.core.service.org.IndexService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
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;

/**
 * 用户登录等相关
 *
 * @author TRX
 * @date 2024/6/4
 */
@RestController
@RequestMapping("index")
@Tag(name = "登录注册")
public class IndexController {

    @Autowired
    private IndexService indexService;

    @Operation(summary = "物联网管理登录", description = "物联网管理登录")
    @RequestMapping(value = "iotLogin", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent iotLogin(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        param.setLoginFromType(LoginFromType.Platform);
        ResultContent result = indexService.iotLogin(param);
        if (result.isSuccess()) {
        }
        return result;
    }

    @Operation(summary = "商户小程序登录", description = "商户小程序登录")
    @RequestMapping(value = "busLogin", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent busLogin(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        param.setLoginFromType(LoginFromType.Shop);
        ResultContent result = indexService.busLogin(param);
        if (result.isSuccess()) {
        }
        return result;
    }

    @Operation(summary = "web通用登录", description = "web通用登录")
    @RequestMapping(value = "commonLogin", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent commonLogin(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        param.setLoginFromType(LoginFromType.Platform);
        ResultContent result = indexService.commonAllLogin(param);
        if (result.isSuccess()) {
            LoginCommonModel tokenModel = (LoginCommonModel) result.getContent();
            response.setHeader("accessToken", tokenModel.getAccess_token());
            Cookie cookie = new Cookie("accessToken", tokenModel.getAccess_token());
            cookie.setMaxAge(60 * 60 * 24 * 3);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return result;
    }


    /**
     * 登录
     *
     * @param param
     * @return
     */
    @Operation(summary = "登录-用户名密码登录", description = "登录--用户名密码登录")
    @RequestMapping(value = "login", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent login(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        if (param.getLoginFromType() == null) {
            return ResultContent.buildFail("loginFromType 不能为空");
        }
        ResultContent result = indexService.login(param);
        if (result.isSuccess()) {
            LoginTokenModel tokenModel = (LoginTokenModel) result.getContent();
            response.setHeader("accessToken", tokenModel.getAccess_token());
            Cookie cookie = new Cookie("accessToken", tokenModel.getAccess_token());
            cookie.setMaxAge(60 * 60 * 24 * 3);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return result;
    }

    @Operation(summary = "登录-用户名密码登录平台端", description = "登录--用户名密码登录平台端")
    @RequestMapping(value = "loginPlatform", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent loginPlatform(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        if (param.getLoginFromType() == null) {
            return ResultContent.buildFail("loginFromType 不能为空");
        }
        ResultContent result = indexService.loginPlatform(param);
        if (result.isSuccess()) {
            LoginPlatformTokenModel tokenModel = (LoginPlatformTokenModel) result.getContent();
            response.setHeader("accessToken", tokenModel.getAccess_token());
            Cookie cookie = new Cookie("accessToken", tokenModel.getAccess_token());
            cookie.setMaxAge(60 * 60 * 24 * 3);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return result;
    }

    /**
     * 普通用户端 (如云芯客)
     *
     * @param param
     * @return
     */
    @Operation(summary = "登录-微信普通用户端", description = "登录--微信普通用户端")
    @RequestMapping(value = "wxUserLogin", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent wxUserLogin(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        param.setLoginFromType(LoginFromType.Student);
        return indexService.wxUserLogin(param);
    }

    /**
     * 微信商户端 (如云芯商)
     *
     * @param param
     * @return
     */
    @Operation(summary = "登录-微信商户端", description = "登录--微信商户端")
    @RequestMapping(value = "wxBusLogin", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent wxBusLogin(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        if (param.getLoginFromType() == null) {
            return ResultContent.buildFail("loginFromType 不能为空");
        }
        ResultContent result = indexService.login(param);
        if (result.isSuccess()) {
            LoginTokenModel tokenModel = (LoginTokenModel) result.getContent();
            response.setHeader("accessToken", tokenModel.getAccess_token());
            Cookie cookie = new Cookie("accessToken", tokenModel.getAccess_token());
            cookie.setMaxAge(60 * 60 * 24 * 3);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return result;
    }

    @Operation(summary = "登录某个机构 + 用户名密码登录", description = "登录某个机构 + 用户名密码登录")
    @RequestMapping(value = "loginToOid", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent loginToOid(@RequestBody LoginParam param, HttpServletRequest request, HttpServletResponse response) {
        if (param.getLoginFromType() == null) {
            return ResultContent.buildFail("loginFromType 不能为空");
        }
        Assert.hasText(param.getOid(), "oid不能为空");
        ResultContent result = indexService.loginToOid(param);
        if (result.isSuccess()) {
            LoginPlatformTokenModel tokenModel = (LoginPlatformTokenModel) result.getContent();
            response.setHeader("accessToken", tokenModel.getAccess_token());
            Cookie cookie = new Cookie("accessToken", tokenModel.getAccess_token());
            cookie.setMaxAge(60 * 60 * 24 * 3);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return result;
    }

    @Operation(summary = "刷新token", description = "刷新token")
    @RequestMapping(value = "refreshToken", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent refreshToken(@RequestBody ReTokenParam param, HttpServletRequest request, HttpServletResponse response) {
        ResultContent<UserTokenModel> result = indexService.refreshToken(param.getRefreshToken());
        if (result != null && result.getState() == ResultState.Success) {
            UserTokenModel tokenModel = (UserTokenModel) result.getContent();
            response.setHeader("accessToken", tokenModel.getAccessToken());
            Cookie cookie = new Cookie("accessToken", String.valueOf(tokenModel.getExpireTime()));
            cookie.setMaxAge(60 * 60 * 24 * 3);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return result;
    }

    @Operation(summary = "退出登录", description = "退出登录")
    @RequestMapping(value = "loginOut", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultContent loginOut() {
        return indexService.loginOut();
    }

    @Operation(summary = "微信小程序得到电话号码", description = "微信小程序得到电话号码")
    @RequestMapping(value = "/getPhoneNumber", method = {RequestMethod.POST})
    public ResultContent getPhoneNumber(@RequestBody GetPhoneModel param) {
        return indexService.getPhoneNum(param);
    }

    @Operation(summary = "验证令牌是否有效", description = "验证令牌是否有效")
    @RequestMapping(value = "verificationToken", method = RequestMethod.GET)
    public ResultContent verificationToken() {
        return indexService.verificationToken();
    }
}
