package com.ruoyi.api.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.lang.LangUnit;
import com.ruoyi.framework.web.service.EmailService;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Locale;

/**
 * API Controller
 *
 * @author Leo
 * @date 22-05-10
 */
@Api(tags = "3 注册接口")
@ApiSupport(order = 3)
@RestController
@RequestMapping("/v1")
public class RegisterApiController extends BaseController
{
    @Autowired
    private EmailService emailService;

    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private LangUnit langUnit;

//    @ApiOperation(value = "3.1 发送注册验证码")
//    @ApiOperationSupport(order = 1)
    @PostMapping("/sendCaptcha")
    public String updatePwd(@RequestParam(required = true) String email, @RequestParam(required = true) String uuid)
    {
        JSONObject jsonObj = new JSONObject();
        emailService.sendEmailCode(email, uuid, "注册验证码");
        jsonObj.put("code", 200);
        jsonObj.put("msg", "success");
        return jsonObj.toJSONString();
    }

//    @ApiOperation(value = "3.2 注册")
//    @ApiOperationSupport(order = 2)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error(langUnit.getValueTag("sysReg.1001", "当前系统没有开启注册功能！") );
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

}
