package com.ai.da.controller;

import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.context.UserContext;
import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.mapper.primary.entity.AccountExtend;
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.model.vo.BindEmailVO;
import com.ai.da.model.vo.PersonalHomepageVO;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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


@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<BindEmailVO> bindEmail(@Valid @RequestBody AccountBindEmailDTO accountBindEmailDTO, HttpServletRequest request) {
        return Response.success(accountService.bindEmail(accountBindEmailDTO, request));
    }

    @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);
    }

    @CrossOrigin
    @ApiOperation(value = "广场用户注册")
    @PostMapping("/designWorksRegister")
    public Response<Boolean> designWorksRegister(@Valid @RequestBody AccountDesignWorksRegisterDTO accountDesignWorksRegisterDTO) {
        return Response.success(accountService.designWorksRegister(accountDesignWorksRegisterDTO));
    }

    @CrossOrigin
    @ApiOperation(value = "广场用户注册")
    @PostMapping("/designWorksRegisterCode")
    public Response<AccountLoginVO> designWorksRegisterCode(@Valid @RequestBody AccountDesignWorksRegisterDTO accountDesignWorksRegisterDTO,
                                                            HttpServletRequest request) {
        return Response.success(accountService.designWorksRegisterCode(accountDesignWorksRegisterDTO, request));
    }

    /**
     * 填写调查问卷
     * @return
     */
/*    @ApiOperation(value = "填写调查问卷")
    @PostMapping("/questionnaire")
    public Response<Boolean> questionnaire(@Valid @RequestBody String questionnaireInfo){
        return Response.success(accountService.collectQuestionnaires(questionnaireInfo));
    }*/

    /**
     * 参与活动 获取福利
     * @return
     */
/*    @ApiOperation(value = "参与活动 获取福利")
    @GetMapping("/activity")
    public Response<String> getActivityBenefits(){
        return Response.success(accountService.getActivityBenefits());
    }*/

    @ApiOperation(value = "将用户账号过期时间设置为过期当天的23：59：59")
    @GetMapping("/setUserValidToDayEnd")
    public Response<List<Long>> setUserValidToDayEnd(){
        return Response.success(accountService.setUserValidToDayEnd());
    }

    // 用户上传头像
    @ApiOperation(value = "上传头像")
    @PostMapping(path = "/uploadAvatar")
    public Response<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        if (null == file || StringUtils.isEmpty(file.getOriginalFilename())) {
            throw new BusinessException("file.cannot.be.empty");
        }
        return Response.success(accountService.uploadAvatar(file));
    }

    @ApiOperation(value = "个人主页浏览量增加")
    @GetMapping("/viewsIncrease")
    public Response<Boolean> viewsGet(@RequestParam("id") Long id) {
        return Response.success(accountService.viewsIncrease(id));
    }

    @ApiOperation(value = "获取个人主页信息")
    @GetMapping("/personalHomepage")
    public Response<PersonalHomepageVO> getPersonalHomepage(@RequestParam("id") Long id){
        return Response.success(accountService.getPersonalHomepage(id));
    }

    @ApiOperation(value = "getUsernameModifyTimes")
    @GetMapping("/getNicknameModifyTimes")
    public Response<Long> getNicknameModifyTimes(){
        return Response.success(accountService.getNicknameModifyTimes());
    }

    @ApiOperation(value = "editUserName")
    @GetMapping("/editUserName")
    public Response<String> editUserName(@RequestParam("newUserName") String newUserName){
        accountService.editUserName(newUserName);
        return Response.success("success");
    }

    /*@ApiOperation(value = "verifyUserEmail")
    @GetMapping("/verifyUserEmail")
    public Response<String> verifyUserEmail(@RequestParam("verifyCode") String verifyCode){
        accountService.verifyUserEmail(verifyCode);
        return Response.success("success");
    }

    @ApiOperation(value = "changeUserEmail")
    @GetMapping("/changeUserEmail")
    public Response<String> changeUserEmail(@RequestParam("newMailbox") String newMailbox){
        accountService.changeUserEmail(newMailbox);
        return Response.success("success");
    }

    @ApiOperation(value = "activateNewEmail")
    @GetMapping("/activateNewEmail")
    public Response<String> activateNewEmail(@RequestParam("token") String token){
        accountService.activateNewEmail(token);
        return Response.success("success");
    }*/

    @PostMapping("halfPricePromotion")
    @ApiOperation(value = "十月半价活动")
    public Response<Boolean> halfPricePromotion() {
        accountService.halfPricePromotion();
        return Response.success(true);
    }

    @PostMapping("temporaryUpgrade")
    @ApiOperation(value = "临时升级")
    public Response<Boolean> temporaryUpgrade() {
        accountService.temporaryUpgrade();
        return Response.success(true);
    }

    @PostMapping("enterpriseLogin")
    @ApiOperation(value = "企业登录")
    public Response<AccountPreLoginVO> enterpriseLogin(@Valid @RequestBody AccountLoginDTO accountDTO) {
        return Response.success(accountService.enterpriseLogin(accountDTO));
    }

    @PostMapping("schoolLogin")
    @ApiOperation(value = "学校登录")
    public Response<AccountPreLoginVO> schoolLogin(@Valid @RequestBody AccountLoginDTO accountDTO) {
        return Response.success(accountService.schoolLogin(accountDTO));
    }

    @PostMapping("organizationNameSearch")
    @ApiOperation(value = "组织名模糊查询")
    public Response<Set<String>> organizationNameSearch(@RequestParam("type") String type, @RequestParam("name") String name) {
        return Response.success(accountService.organizationNameSearch(type, name));
    }

    @PostMapping("addOrUpdateSubAccount")
    @ApiOperation(value = "子账号新增")
    public Response<Boolean> addSubAccount(@Valid @RequestBody AddSubAccountDTO addSubAccountDTO) {
        return Response.success(accountService.addSubAccount(addSubAccountDTO));
    }

    @PostMapping("deleteSubAccount")
    @ApiOperation(value = "子账号删除")
    public Response<Boolean> deleteSubAccount(@Valid @RequestBody AddSubAccountDTO addSubAccountDTO) {
//        return Response.success(accountService.deleteSubAccount(addSubAccountDTO));
        accountService.removeSubAccount(addSubAccountDTO, UserContext.getUserHolder().getId());
        return Response.success();
    }

    @PostMapping("subAccountList")
    @ApiOperation(value = "子账号查询")
    public Response<PageBaseResponse<Account>> subAccountList(@Valid @RequestBody SubAccountPageDTO subAccountPageDTO) {
        return Response.success(accountService.subAccountList(subAccountPageDTO));
    }

    @GetMapping("accountDetail")
    @ApiOperation(value = "账号详情")
    public Response<Account> accountDetail(@RequestParam("id") Long id) {
        return Response.success(accountService.accountDetail(id));
    }

    @PostMapping("getAccountDetail")
    @ApiOperation(value = "获取账户信息")
    public Response<AccountLoginVO> getAccountDetail() {
        return Response.success(accountService.getAccountDetail());
    }

    @GetMapping("/bindGoogle")
    @ApiOperation(value = "绑定谷歌")
    public Response<AccountExtend> bindGoogle(@RequestParam("credential") String credential) {
        return Response.success(accountService.bindGoogle(credential));
    }

    @GetMapping("/bindWeChat")
    @ApiOperation(value = "绑定微信")
    public Response<AccountExtend> bindWeChat(@RequestParam("code") String code) {
        return Response.success(accountService.bindWeChat(code));
    }

    @GetMapping("/bindEmail")
    @ApiOperation(value = "绑定邮箱")
    public Response<BindEmailVO> bindEmail(@RequestParam("email") String email) {
        return Response.success(accountService.bindEmail(email));
    }

    @GetMapping("/unbindWeChat")
    @ApiOperation(value = "解除绑定微信")
    public Response<Boolean> unbindWeChat() {
        return Response.success(accountService.unbindWeChat());
    }

    @GetMapping("/unbindGoogle")
    @ApiOperation(value = "解除绑定谷歌")
    public Response<Boolean> unbindGoogle() {
        return Response.success(accountService.unbindGoogle());
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "更新用户国家、职业信息")
    public Response<Boolean> updateUserInfo(@Valid @RequestBody UpdateUserInfoDTO updateUserInfoDTO) {
        return Response.success(accountService.updateUserInfo(updateUserInfoDTO));
    }

    @GetMapping("/subAccountImportExcelDownload")
    @ApiOperation(value = "模板下载")
    public void subAccountImportExcelDownload(HttpServletResponse response) {
        accountService.subAccountImportExcelDownload(response);
    }

    @GetMapping("/exportAccountsToExcel")
    @ApiOperation(value = "下载子账号信息")
    public void exportAccountsToExcel(HttpServletResponse response) {
        accountService.exportAccountsToExcel(response);
    }

    @PostMapping("/subAccountImport")
    @ApiOperation(value = "模板导入")
    public Response<Boolean> subAccountImport(@RequestParam("file") MultipartFile file) {
        return Response.success(accountService.subAccountImport(file));
    }

    /*@GetMapping("/send618Email")
    @ApiOperation(value = "618邮件发送")
    public Response<String> send618PromotionEmailTemp() {
        accountService.send618PromotionEmailTemp();
        return Response.success("success");
    }*/

    /*@GetMapping("/refreshCreditsMonthly")
    @ApiOperation(value = "刷新子账号积分")
    public void refreshCreditsMonthly() {
        accountService.refreshCreditsMonthly();
    }*/
    /*@GetMapping("/checkEduAdminExpireStatus")
    @ApiOperation(value = "检查教育管理员账号到期情况")
    public void checkEduAdminExpireStatus() {
        accountService.checkEduAdminExpireStatus();
    }*/


}
