package com.m2pool.auth.controller;

import com.m2pool.auth.entity.*;
import com.m2pool.auth.service.SysLoginService;
import com.m2pool.auth.service.impl.MaliServiceImpl;
import com.m2pool.common.core.Result.R;
import com.m2pool.common.core.utils.JwtUtils;
import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.common.security.annotation.RequiresLogin;
import com.m2pool.common.security.auth.AuthUtil;
import com.m2pool.common.security.service.TokenService;
import com.m2pool.common.security.utils.SecurityUtils;
import com.m2pool.system.api.entity.EmailTemplateEntity;
import com.m2pool.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;


/**
 * @Description token 控制
 * @Date 2024/6/12 14:59
 * @Author dy
 */
@RestController
@Api(tags = "token令牌")
public class TokenController {

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private MaliServiceImpl maliService;

    @PostMapping("login")
    public R<?> login(@RequestBody @Valid LoginBody loginBody,@RequestHeader("User-Agent") String userAgent)
    {
        return sysLoginService.login(loginBody);
    }


    /**
     * 后台管理系统登录
     * @param loginManageBody
     * @return
     */
    @PostMapping("managerLogin")
    @ApiOperation(value = "后台管理系统登录")
    public R<Map<String,Object>> managerLogin(@RequestBody @Valid LoginManageBody loginManageBody)
    {
        return sysLoginService.managerLogin(loginManageBody);
    }


    @PostMapping("registerCode")
    public R<?> emailCode(@Validated @RequestBody GetEmailCodeEntity entity)
    {
        return maliService.emailCode(entity);
    }

    @PostMapping("loginCode")
    public R<?> loginCode(@Valid @RequestBody GetLoginEmailCodeEntity entity)
    {
        return maliService.loginCode(entity);
    }

    @PostMapping("addCreditEmailCode")
    public R<?> addCreditEmailCode(@RequestBody GetEmailCodeEntity entity)
    {
        return maliService.AddCreditCode(entity);
    }

    @PostMapping("resetPwdCode")
    public R<?> resetPwdCode(@Valid @RequestBody GetEmailCodeEntity entity)
    {
        return maliService.resetPwdCode(entity);
    }

    @PostMapping("updatePwdCode")
    public R<?> updatePwdCode()
    {
        return maliService.updatePwdCode();
    }


    @GetMapping("hello")
    public R<?> hello()
    {
        return R.success("浏览器安全认证已确认，请重新返回原页面进行登陆");
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            //删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.success("用户登出");
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)){
            //刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.success();
        }
        return R.success();
    }

    @PostMapping("register")
    public R<?> register(@Valid() @RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody);
        return R.success("用户"+registerBody.getEmail()+"注册成功");
    }

    @PostMapping("resetPwd")
    public R<?> resetPwd(@Valid @RequestBody ResetPwdBody resetPwdBody)
    {
        // 重置密码
        sysLoginService.resetPwd(resetPwdBody,true);
        return R.success("账号"+ resetPwdBody.getEmail()+"密码重置成功");
    }

    @PostMapping("updatePwd")
    @RequiresLogin
    public R<?> updatePwd(@Valid @RequestBody ResetPwdBody resetPwdBody)
    {
        String email = SecurityUtils.getUsername();
        resetPwdBody.setEmail(email);

        // 修改密码
        sysLoginService.resetPwd(resetPwdBody,false);
        return R.success("账号"+ resetPwdBody.getEmail()+"密码修改成功");
    }

    @PostMapping("sendTextMail")
    public R<?> sendTextMail(@Valid @RequestBody EmailEntity entity)
    {
        maliService.sendTextMailMessage(entity.getEmail(), entity.getSubject(),entity.getText());
        return R.success("邮件已发送");
    }


    @PostMapping("sendHtmlMailMessage")
    public R<?> sendHtmlMailMessage(@Valid @RequestBody EmailTemplateEntity entity)
    {
        maliService.sendHtmlMailMessage(entity);
        return R.success("邮件已发送");
    }



}
