package com.sonic.lion.lib.client;

import com.alibaba.fastjson.TypeReference;
import com.sonic.lion.lib.input.BalanceCheckoutInput;
import com.sonic.lion.lib.input.GetAccountBuffInput;
import com.sonic.lion.lib.input.PlatformGiftInput;
import com.sonic.lion.lib.output.AccountBuffOutput;
import com.sonic.lion.lib.output.BalanceCheckoutOutput;
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;

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

    private static final String CREATE_ACCOUNT_BUFF = "/api/pay/trade/create-account-buff";
    private static final String GET_ACCOUNT_BUFF = "/api/pay/trade/get-account-buff";
    private static final String CHECKOUT_TO_USER = "/api/pay/trade/checkoutToUser";
    private static final String CHECKOUT_TO_B = "/api/pay/trade/checkoutToB";
    private static final String PLATFORM_GIFT = "/api/pay/trade/systemGift";

    private RpcClient rpcClient;
    private String host;

    public PayClient(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-lion-svc:8080";
                break;
            case product:
            default:
                this.host = "http://prod-lion-svc:8080";
        }
    }

    /**
     * 创建账号钱包
     *
     * @param userId
     * @return
     */
    public void createAccountBuff(Long userId) {
        GetAccountBuffInput input = new GetAccountBuffInput(userId);
        rpcClient.post(host + CREATE_ACCOUNT_BUFF, input, new TypeReference<Result<Void>>() {
        });
    }

    /**
     * 获取账号钱包数据
     *
     * @param userId
     * @return
     */
    public AccountBuffOutput getAccountBuff(Long userId) {
        GetAccountBuffInput input = new GetAccountBuffInput(userId);
        return rpcClient.post(host + GET_ACCOUNT_BUFF, input, new TypeReference<Result<AccountBuffOutput>>() {
        });
    }

    /**
     * 钱包余额结账给用户
     *
     * @param input
     * @return
     */
    public BalanceCheckoutOutput checkoutToUser(BalanceCheckoutInput input) {
        return rpcClient.post(host + CHECKOUT_TO_USER, input, new TypeReference<Result<BalanceCheckoutOutput>>() {
        });
    }

    /**
     * 钱包余额结账给平台
     *
     * @param input
     * @return
     */
    public BalanceCheckoutOutput checkoutToB(BalanceCheckoutInput input) {
        return rpcClient.post(host + CHECKOUT_TO_B, input, new TypeReference<Result<BalanceCheckoutOutput>>() {
        });
    }

    /**
     * 平台赠送
     *
     * @param input
     * @return
     */
    public Long platformGift(PlatformGiftInput input) {
        return rpcClient.post(host + PLATFORM_GIFT, input, new TypeReference<Result<Long>>() {
        });
    }

    public void saveUserBill(BalanceCheckoutInput input) {
    }

    public void checkoutToPlatformNotBill(BalanceCheckoutInput input) {
    }
}
