package com.eitc.patient.controller;

import com.aliyuncs.exceptions.ClientException;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.patient.enums.AppUserLoginTypeEnum;
import com.eitc.patient.service.IAppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 登录相关接口Controller
 *
 * @author eitc
 * @date 2023-08-04
 */
@Api(tags = "APP登录相关接口")
@RestController
@RequestMapping("/app")
public class AppLoginController extends BaseController {

    @Resource
    private IAppUserService appUserService;


    /**
     * 登录
     *
     * @param map 参数
     * @return 登录信息
     */
    @ApiOperation("APP端账号密码登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "登录手机号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "类型（1患者，2医生）", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/password/login")
    public AjaxResult passwordLogin(@RequestBody Map<String, String> map) {
        map.put("loginType", String.valueOf(AppUserLoginTypeEnum.ACCOUNT.getCode()));
        return appUserService.login(map);
    }

    @ApiOperation("APP端短信登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "登录手机号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "captchaCode", value = "短信验证码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "类型（1患者，2医生）", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/captcha/login")
    public AjaxResult captchaLogin(@RequestBody Map<String, String> map) {
        map.put("loginType", String.valueOf(AppUserLoginTypeEnum.CAPTCHA.getCode()));
        return appUserService.login(map);
    }

    /**
     * 注册
     *
     * @param map 参数
     * @return 返回结果
     * @author mtx
     * @date: 2024/6/19 18:39
     */
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "登录手机号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "identificationCard", value = "身份证号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "captchaCode", value = "短信验证码", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/register")
    public AjaxResult register(@RequestBody Map<String, String> map) {
        return toAjax(appUserService.register(map));
    }

    /**
     * 获取短信验证码
     *
     * @return 成功
     */
    @PostMapping("/sendCaptchaCode")
    @ApiOperation("获取短信验证码")
    public AjaxResult sendCaptchaCode(String phoneNumber) throws ClientException {
        return appUserService.sendCaptchaCode(phoneNumber);
    }

    @PostMapping("/checkCaptchaCode")
    @ApiOperation("验证短信验证码")
    public AjaxResult checkCaptchaCode(String phoneNumber, String captchaCode) {
        appUserService.checkCaptchaCode(phoneNumber, captchaCode);
        return success();
    }

    /**
     * 退出登录
     *
     * @return 成功
     */
    @GetMapping("/logout")
    @ApiOperation("退出登录")
    public AjaxResult logout() {
        appUserService.logout();
        return success();
    }

}
