package com.ai.da.controller;

import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.mapper.primary.entity.TrialOrder;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.AccountLoginVO;
import com.ai.da.model.vo.AccountPreLoginVO;
import com.ai.da.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "Account模块")
@Slf4j
@RestController
@RequestMapping("/api/account")
public class AccountController {

    @Resource
    private AccountService accountService;

    @ApiOperation(value = "预先登入")
    @PostMapping("/preLogin")
    public Response<AccountPreLoginVO> preLogin(@Valid @RequestBody AccountPreLoginDTO accountDTO) {
        return Response.success(accountService.preLogin(accountDTO));
    }

    @ApiOperation(value = "登入")
    @PostMapping("/login")
    public Response<AccountLoginVO> login(@Valid @RequestBody AccountLoginDTO accountDTO, HttpServletRequest request) {
        return Response.success(accountService.login(accountDTO, request));
    }

    @ApiOperation(value = "绑定邮箱")
    @PostMapping("/bindEmail")
    public Response<Boolean> bindEmail(@Valid @RequestBody AccountBindEmailDTO accountBindEmailDTO) {
        return Response.success(accountService.bindEmail(accountBindEmailDTO));
    }

    @ApiOperation(value = "忘记密码")
    @PostMapping("/resetPwd")
    public Response<Boolean> resetPwd(@Valid @RequestBody AccountRegisterDTO accountRegisterDTO) {
        return Response.success(accountService.forgetPwd(accountRegisterDTO));
    }

    @ApiOperation(value = "发送邮件")
    @PostMapping("/sendEmail")
    public Response<Boolean> sendEmail(@Valid @RequestBody EmailSendDTO emailSendDTO) {
        return Response.success(accountService.sendEmail(emailSendDTO));
    }

    @ApiOperation(value = "登出")
    @PostMapping("/logout")
    public Response<Boolean> logout(@Valid @RequestBody AccountLogoutDTO accountLogoutDTO) {
        return Response.success(accountService.logout(accountLogoutDTO));
    }

    @ApiOperation(value = "是否登入")
    @PostMapping("/isLogin")
    public Response<Boolean> isLogin(@Valid @RequestBody AccountLogoutDTO accountLogoutDTO) {
        return Response.success(accountService.isLogin(accountLogoutDTO));
    }

    @ApiOperation(value = "获取当前用户语言")
    @PostMapping("/getUserLanguage")
    public Response<String> getUserLanguage() {
        return Response.success(accountService.getUserLanguage());
    }

    @ApiOperation(value = "切换当前用户语言")
    @GetMapping("/changeUserLanguage")
    public Response<String> changeUserLanguage(String language) {
        return Response.success(accountService.changeUserLanguage(language));
    }

    @ApiOperation(value = "试用用户退出登录")
    @GetMapping("/trialUserLogout")
    public Response<Boolean> trialUserLogout() {
        return Response.success(accountService.trialUserLogout());
    }

    @ApiOperation(value = "完成新手教程")
    @PostMapping("/completeGuidance")
    public Response<Boolean> completeGuidance() {
        return Response.success(accountService.completeGuidance());
    }

    @ApiOperation(value = "试用订单列表")
    @PostMapping("/trialOrderList")
    public Response<PageBaseResponse<TrialOrder>> trialOrderList(@Valid @RequestBody TrialOrderDTO trialOrderDTO) {
        return Response.success(PageBaseResponse.success(accountService.trialOrderList(trialOrderDTO)));
    }

    @ApiOperation(value = "通过试用订单审批")
    @PostMapping("/trialOrderApproval")
    public Response<Boolean> trialOrderApproval(@RequestParam("ids") List<Long> ids) {
        return Response.success(accountService.trialOrderApproval(ids));
    }

    @ApiOperation(value = "拒绝试用订单审批")
    @PostMapping("/trialOrderRefuse")
    public Response<Boolean> trialOrderRefuse(@RequestParam("ids") List<Long> ids) {
        return Response.success(accountService.trialOrderRefuse(ids));
    }

    @ApiOperation(value = "获取是否自动审评")
    @PostMapping("/getIsAutoApproval")
    public Response<Boolean> getIsAutoApproval() {
        return Response.success(accountService.getIsAutoApproval());
    }

    @ApiOperation(value = "切换是否自动审评")
    @PostMapping("/switchIsAutoApproval")
    public Response<Boolean> switchIsAutoApproval() {
        return Response.success(accountService.switchIsAutoApproval());
    }

    @ApiOperation(value = "aws状态检测")
    @GetMapping("/healthy")
    @ResponseStatus(HttpStatus.OK)
    public Response<Map<String,Integer>> checkStatus(){
        Map<String,Integer> returnMap = new HashMap<>();
        returnMap.put("code",200);
        return Response.success(returnMap);
    }

    @ApiOperation(value = "查询账号到期时间")
    @PostMapping("/getExpiredTime")
    public Response<Long> getExpiredTime(){
        return Response.success(accountService.getExpiredTime());
    }

    @ApiOperation(value = "免密登录")
    @PostMapping("/noLoginRequired")
    public Response<AccountLoginVO> noLoginRequired(@RequestBody NoLoginRequiredDTO noLoginRequiredDTO, HttpServletRequest request){
        return Response.success(accountService.noLoginRequired(noLoginRequiredDTO, request));
    }

    @PostMapping("upgradeNotification")
    @ApiOperation(value = "升级邮件通知")
    public Response<Boolean> upgradeNotification() {
        accountService.upgradeNotification();
        return Response.success(true);
    }
}
