package com.wteam.controller.passport;

import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.security.enums.UserEnums;
import com.wteam.framework.common.security.token.Token;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.member.entity.dos.Member;
import com.wteam.framework.modules.member.entity.dto.MemberEditDTO;
import com.wteam.framework.modules.member.service.MemberService;
import com.wteam.framework.modules.sms.SmsUtil;
import com.wteam.framework.modules.verification.entity.enums.VerificationEnums;
import com.wteam.framework.modules.verification.service.VerificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;



/**
 * 买家端,会员接口
 *
 * @author Chopper
 * @since 2020/11/16 10:07 下午
 */
@Slf4j
@RestController
@Tag(name = "用户端,会员接口")
@RequestMapping("/buyer/passport/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private VerificationService verificationService;




    @Operation(summary = "登录接口")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "password", description = "密码", required = true, in = ParameterIn.QUERY)
    })
    @PostMapping("/userLogin")
    public ResultMessage<Token> userLogin(@NotNull(message = "用户名不能为空") @RequestParam String username,
                                          @NotNull(message = "密码不能为空") @RequestParam String password,
                                          @RequestHeader String uuid) {
        verificationService.check(uuid, VerificationEnums.LOGIN);
        return ResultUtil.data(this.memberService.usernameLogin(username, password));
    }

    @Operation(summary = "注销接口")
    @PostMapping("/logout")
    public ResultMessage<Object> logout() {
        this.memberService.logout(UserEnums.MEMBER);
        return ResultUtil.success();
    }

//    @Operation(summary = "短信登录接口")
//    @Parameters({
//            @Parameter(name = "mobile", description = "手机号", required = true, in = ParameterIn.QUERY),
//            @Parameter(name = "code", description = "验证码", required = true, in = ParameterIn.QUERY)
//    })
//    @PostMapping("/smsLogin")
//    public ResultMessage<Object> smsLogin(@NotNull(message = "手机号为空") @RequestParam String mobile,
//                                          @NotNull(message = "验证码为空") @RequestParam String code,
//                                          @RequestHeader String uuid) {
//        if (smsUtil.verifyCode(mobile, VerificationEnums.LOGIN, uuid, code)) {
//            return ResultUtil.data(memberService.mobilePhoneLogin(mobile));
//        } else {
//            throw new ServiceException(ResultCode.VERIFICATION_SMS_CHECKED_ERROR);
//        }
//    }

    @Operation(summary = "注册用户")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "password", description = "密码", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "mobilePhone", description = "手机号", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "code", description = "验证码", required = true, in = ParameterIn.QUERY)
    })
    @PostMapping("/register")
    public ResultMessage<Object> register(@NotNull(message = "用户名不能为空") @RequestParam String username,
                                          @NotNull(message = "密码不能为空") @RequestParam String password,
                                          @NotNull(message = "手机号为空") @RequestParam String mobilePhone,
                                          @RequestHeader String uuid,
                                          @NotNull(message = "验证码不能为空") @RequestParam String code) {

        if (smsUtil.verifyCode(mobilePhone, VerificationEnums.REGISTER, uuid, code)) {
            return ResultUtil.data(memberService.register(username, password, mobilePhone));
        } else {
            throw new ServiceException(ResultCode.VERIFICATION_SMS_CHECKED_ERROR);
        }

    }

    @Operation(summary = "获取当前登录用户接口")
    @GetMapping
    public ResultMessage<Member> getUserInfo() {

        return ResultUtil.data(memberService.getUserInfo());
    }

//    @Operation(summary = "通过短信重置密码")
//    @Parameters({
//            @Parameter(name = "mobile", description = "手机号", required = true, in = ParameterIn.QUERY),
//            @Parameter(name = "password", description = "是否保存登录", required = true, in = ParameterIn.QUERY)
//    })
//    @PostMapping("/resetByMobile")
//    public ResultMessage<Member> resetByMobile(@NotNull(message = "手机号为空") @RequestParam String mobile,
//                                               @NotNull(message = "验证码为空") @RequestParam String code,
//                                               @RequestHeader String uuid) {
//        //校验短信验证码是否正确
//        if (smsUtil.verifyCode(mobile, VerificationEnums.FIND_USER, uuid, code)) {
//            //校验是否通过手机号可获取会员,存在则将会员信息存入缓存，有效时间3分钟
//            memberService.findByMobile(uuid, mobile);
//            return ResultUtil.success();
//        } else {
//            throw new ServiceException(ResultCode.VERIFICATION_SMS_CHECKED_ERROR);
//        }
//    }

    @Operation(summary = "修改密码")
    @Parameters({
            @Parameter(name = "password", description = "是否保存登录", required = true, in = ParameterIn.QUERY)
    })
    @PostMapping("/resetPassword")
    public ResultMessage<Object> resetByMobile(@NotNull(message = "密码为空") @RequestParam String password, @RequestHeader String uuid) {

        return ResultUtil.data(memberService.resetByMobile(uuid, password));
    }

    @Operation(summary = "修改用户自己资料")
    @PutMapping("/editOwn")
    public ResultMessage<Member> editOwn(MemberEditDTO memberEditDTO) {

        return ResultUtil.data(memberService.editOwn(memberEditDTO));
    }

    @Operation(summary = "修改密码")
    @Parameters({
            @Parameter(name = "password", description = "旧密码", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "newPassword", description = "新密码", required = true, in = ParameterIn.QUERY)
    })
    @PutMapping("/modifyPass")
    public ResultMessage<Member> modifyPass(@NotNull(message = "旧密码不能为空") @RequestParam String password,
                                            @NotNull(message = "新密码不能为空") @RequestParam String newPassword) {
        return ResultUtil.data(memberService.modifyPass(password, newPassword));
    }

    @Operation(summary = "初始设置密码")
    @Parameters({
            @Parameter(name = "newPassword", description = "新密码", required = true, in = ParameterIn.QUERY)
    })
    @PutMapping("/canInitPassword")
    public ResultMessage<Object> canInitPassword() {
        return ResultUtil.data(memberService.canInitPass());
    }

    @Operation(summary = "初始设置密码")
    @Parameters({
            @Parameter(name = "newPassword", description = "新密码", required = true, in = ParameterIn.QUERY)
    })
    @PutMapping("/initPassword")
    public ResultMessage<Object> initPassword(@NotNull(message = "密码不能为空") @RequestParam String password) {
        memberService.initPass(password);
        return ResultUtil.success();
    }

    @Operation(summary = "注销账号")
    @Parameters({
            @Parameter(name = "password", description = "密码", required = true, in = ParameterIn.QUERY)
    })
    @PutMapping("/cancellation")
    public ResultMessage<Member> cancellation(@NotNull(message = "密码不能为空") @RequestParam String password) {
        memberService.cancellation(password);
        return ResultUtil.success();
    }

    @Operation(summary = "刷新token")
    @GetMapping("/refresh/{refreshToken}")
    public ResultMessage<Object> refreshToken(@NotNull(message = "刷新token不能为空") @PathVariable String refreshToken) {
        return ResultUtil.data(this.memberService.refreshToken(refreshToken));
    }

}
