package com.sonic.bear.lib.client;

import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.ImmutableMap;
import com.sonic.bear.lib.input.ThirdUserLoginInput;
import com.sonic.common.AppRuntime;
import com.sonic.common.auth.domains.Session;
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 UserLoginClient {

    private static final String URI_THIRD_USER_LOGIN = "/api/auth/third-user-login";
    private static final String URI_LOGOUT = "/api/auth/logout";
    private static final String URI_KICK_OUT = "/api/auth/kick-out";

    private RpcClient rpcClient;
    private String host;

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


    /**
     * 三方账号登录
     * @param input
     * @return
     */
    public Session thirdLogin(ThirdUserLoginInput input) {
        return rpcClient.post(host + URI_THIRD_USER_LOGIN, input, new TypeReference<Result<Session>>(){});
    }

    /**
     * 退出登录
     * @param token
     */
    public void logout(String token) {
        rpcClient.post(host.concat(URI_LOGOUT), ImmutableMap.of("token", token), new TypeReference<Result<Void>>(){});
    }

    /**
     * 踢下线
     * @param userId
     */
    public void kickOut(Long userId) {
        rpcClient.post(host.concat(URI_KICK_OUT), ImmutableMap.of("userId", userId), new TypeReference<Result<Void>>(){});
    }

}
