package com.sonic.frog.lib.client;

import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.sonic.common.AppRuntime;
import com.sonic.common.rpc.Result;
import com.sonic.common.rpc.RpcClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author code
 */
@Slf4j
@Service
public class UserMemberClient {

    private static final String USER_MEMBER_EXP_NOTIFY = "/api/user-member/exp-notify";
    private static final String USER_MEMBER_GIFT_USER_CREATE_COUNT = "/api/user-member/gift-user-create-count";

    private RpcClient rpcClient;
    private String host;

    public UserMemberClient(RpcClient rpcClient, AppRuntime appRuntime) {
        this.rpcClient = rpcClient;
        switch (appRuntime.getEnv()) {
            case dev:
                this.host = "http://localhost:8080";
                break;
            case local:
                this.host = "http://localhost:8080";
                break;
            case test:
                this.host = "http://test-frog-svc:8080";
                break;
            case product:
            default:
                this.host = "http://prod-frog-svc:8080";
        }
    }


    /**
     * 用户会员到期通知
     *
     * @param userId
     */
    public void userMemberExpNotify(Long userId) {
        Map<String, Object> input = new HashMap<>();
        input.put("userIdList", Lists.newArrayList(userId));
        rpcClient.post(host + USER_MEMBER_EXP_NOTIFY, input, new TypeReference<Result<Void>>() {
        });
    }

    /**
     * 用户会员到期通知
     *
     * @param userIdList
     */
    public void userMemberExpNotify(List<Long> userIdList) {
        Map<String, Object> input = new HashMap<>();
        input.put("userIdList", userIdList);
        rpcClient.post(host + USER_MEMBER_EXP_NOTIFY, input, new TypeReference<Result<Void>>() {
        });
    }


    /**
     * 订阅会员或续订会员赠送用户创作次数
     *
     * @param userId 用户id
     * @param startTime 订阅或续订开始时间
     * @param expireTime 会员过期时
     */
    public void subMemberGiftUserCreateCount(Long userId, LocalDateTime startTime, LocalDateTime expireTime) {
        Map<String, Object> input = new HashMap<>();
        input.put("userId", userId);
        input.put("startTime", startTime);
        input.put("expireTime", expireTime);
        rpcClient.post(host + USER_MEMBER_GIFT_USER_CREATE_COUNT, input, new TypeReference<Result<Void>>() {
        });
    }

}
