package com.liquidnet.service.adam.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.service.adam.dto.vo.AdamMemberSimpleVo;
import com.liquidnet.service.adam.dto.vo.AdamMemberVo;
import com.liquidnet.service.adam.dto.vo.AdamUserMemberVo;
import com.liquidnet.service.adam.service.IAdamMemberService;
import com.liquidnet.service.adam.service.IAdamRdmService;
import com.liquidnet.service.adam.service.IAdamUserMemberService;
import com.liquidnet.service.base.ResponseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Date;

@ApiSupport(order = 10030)
@Api(tags = "会员信息")
@Slf4j
@Validated
@RestController
@RequestMapping("member")
public class AdamMemberController {
    @Autowired
    IAdamRdmService adamRdmService;
    @Autowired
    IAdamMemberService adamMemberService;
    @Autowired
    IAdamUserMemberService adamUserMemberService;

    @ApiOperationSupport(order = 0)
    @ApiOperation(value = "会员信息")
    @GetMapping("info")
    public ResponseDto<AdamMemberVo> query() {
        AdamMemberSimpleVo memberSimpleVo = adamRdmService.getMemberSimpleVo();
        AdamMemberVo memberVo = adamRdmService.getMemberVoByMemberId(memberSimpleVo.getMemberId());
        if (null != memberVo) {
            memberVo.setPriceVoList(adamRdmService.getMemberPriceVoByMemberId(memberVo.getMemberId()));

            memberVo.setRightsVoList(adamRdmService.getMemberRightsVoByMemberId(memberVo.getMemberId()));

            memberVo.setMemberInfo(adamRdmService.getUserMemberVoByUid(CurrentUtil.getCurrentUid()));
        }
        return ResponseDto.success(memberVo);
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "会员协议")
    @GetMapping("agreement")
    public ResponseDto<String> agreement() {
        return ResponseDto.success(adamRdmService.getMemberAgreement());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "是否会员")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "uid", value = "用户ID[64]", example = "1"),
    })
    @GetMapping("check")
    public ResponseDto<Boolean> isMember(@NotBlank @RequestParam String uid) {
        if (StringUtils.isBlank(uid)) return ResponseDto.success(false);

        AdamUserMemberVo userMemberInfo = adamRdmService.getUserMemberVoByUid(uid);
        if (null != userMemberInfo) {
            Integer state = userMemberInfo.getState();
            if (null != state && state == 1) {
                // TODO: 2021/5/25 时间界定
                Date expiryAtDate = DateUtil.parse(userMemberInfo.getExpiryAt(), DateUtil.DATE_SMALL_STR);

                return ResponseDto.success(null != expiryAtDate && expiryAtDate.after(DateUtil.now()));
            }
        }
        return ResponseDto.success(false);
    }
}
