package com.hzs.member.share.controller.api;

import com.hzs.common.core.config.BdConfig;
import com.hzs.common.core.constant.SysConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EEnv;
import com.hzs.common.core.enums.EShareType;
import com.hzs.common.core.utils.sign.Base64Util;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.third.share.IShareServiceApi;
import com.hzs.third.share.dto.ShareServiceDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 会员分享相关控制器
 * @Author: jiang chao
 * @Time: 2023/3/15 10:03
 * @Classname: ShareController
 * @PackageName: com.hzs.member.share.controller
 */
@Slf4j
@RestController
@RequestMapping("/api/share")
public class ApiShareController {

    @DubboReference
    IShareServiceApi iShareServiceApi;

    /**
     * 获取海粉分享码
     *
     * @return
     */
    @GetMapping("/fans-code")
    public AjaxResult fansCode() {
        Long userId = SecurityUtils.getUserId();
        // 海粉注册分享人就是自己
        R<String> resultR = iShareServiceApi.queryShareCode(ShareServiceDTO.builder()
                .eShareType(EShareType.FANS)
                .pkBusiness(userId)
                .pkCountry(SecurityUtils.getPkCountry())
                .userId(userId)
                .build());
        if (resultR.isSuccess()) {
            return AjaxResult.success("", resultR.getData());
        }
        return AjaxResult.error();
    }

    /**
     * 获取海粉分享码（H5）
     *
     * @return
     */
    @GetMapping("/fans-code-h5")
    public AjaxResult fansCodeH5() {
        Long userId = SecurityUtils.getUserId();
        // 海粉注册分享人就是自己
        R<String> resultR = iShareServiceApi.queryShareCode(ShareServiceDTO.builder()
                .eShareType(EShareType.FANS)
                .pkBusiness(userId)
                .pkCountry(SecurityUtils.getPkCountry())
                .userId(userId)
                .build());
        if (resultR.isSuccess()) {
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("dataUrl", resultR.getData());
            // 前端无法根据URL生成海报，此处需要转一下base64
            resultMap.put("dataStr", Base64Util.toUrlBase64(resultR.getData()));
            return AjaxResult.success(resultMap);
        }
        return AjaxResult.error();
    }

    /**
     * 获取分享专区分享码
     *
     * @return
     */
    @GetMapping("/share-code")
    public AjaxResult shareCode() {
        // 2024.01.29 添加需求，自2024年2月1日起，0元注册功能取消
        if (checkShare()) {
            return AjaxResult.error("二维码已停用，请联系相应亲友协助办理注册");
        }

        Long userId = SecurityUtils.getUserId();
        // 分享专区分享人就是自己
        R<String> resultR = iShareServiceApi.queryShareCode(ShareServiceDTO.builder()
                .eShareType(EShareType.SHARE)
                .pkBusiness(userId)
                .pkCountry(SecurityUtils.getPkCountry())
                .userId(userId)
                .build());
        if (resultR.isSuccess()) {
            return AjaxResult.success("", resultR.getData());
        }
        return AjaxResult.error();
    }

    /**
     * 获取分享专区分享码（H5）
     *
     * @return
     */
    @GetMapping("/share-code-h5")
    public AjaxResult shareCodeH5() {
        // 2024.01.29 添加需求，自2024年2月1日起，0元注册功能取消
        if (checkShare()) {
            return AjaxResult.error("二维码已停用，请联系相应亲友协助办理注册");
        }

        Long userId = SecurityUtils.getUserId();
        // 分享专区分享人就是自己
        R<String> resultR = iShareServiceApi.queryShareCode(ShareServiceDTO.builder()
                .eShareType(EShareType.SHARE)
                .pkBusiness(userId)
                .pkCountry(SecurityUtils.getPkCountry())
                .userId(userId)
                .build());
        if (resultR.isSuccess()) {
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("dataUrl", resultR.getData());
            // 前端无法根据URL生成海报，此处需要转一下base64
            resultMap.put("dataStr", Base64Util.toUrlBase64(resultR.getData()));
            return AjaxResult.success(resultMap);
        }
        return AjaxResult.error();
    }

    /**
     * 校验是否能注册
     *
     * @return
     */
    @GetMapping("/check-share-code")
    public AjaxResult checkShareCode() {
        if (checkShare()) {
            return AjaxResult.error("二维码已停用，请联系相应亲友协助办理注册");
        }
        return AjaxResult.success();
    }

    /**
     * 校验是否可以注册 -- 自2024年2月1日起，0元注册功能取消
     *
     * @return
     */
    private boolean checkShare() {
        if (EEnv.TEST.getValue().equals(BdConfig.getEnv())) {
            return new Date().getTime() >= SysConstants.CLOSE_ZERO_DATE_TEST;
        } else {
            return new Date().getTime() >= SysConstants.CLOSE_ZERO_DATE_ONLINE;
        }
    }

}
