package com.zhiqiyun.open.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dliyun.AlgorithmType;
import com.dliyun.ClientException;
import com.dliyun.DefaultClient;
import com.dliyun.Response;
import com.zhiqiyun.open.core.service.UnifiedLoginService;
import org.springframework.stereotype.Service;

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

/**
 * @author xin yang
 * @date 2022/6/13
 */
@Service
public class UnifiedLoginServiceImpl implements UnifiedLoginService {

    private final static String API_URL = "http://127.0.0.1:9800/router";

    private final static String APP_ID = "220228000020";

    public final static String APP_SECRET = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSTihNSHP5mqzMxkqxeEv150GWUcfdNy9eoD9wWhWO5re59vi8ownmfpcKOSHhqM09W+/jwC5xbqhYJN2kbCMWTyBHJ1BaoldX736aOPCmo0octyiq2N3Xx5JFnBh5asXdEO12lrOXGNqcwwq6iDuw2Na3H4u7p3QEIz6LKLcGJQIDAQAB";

    DefaultClient client = new DefaultClient(API_URL, APP_ID, APP_SECRET, AlgorithmType.RSA);

    @Override
    public Response<JSONObject> login(String account, String password) throws ClientException {
        Map<String, Object> form = new HashMap<>(2);
        form.put("userName", account);
        form.put("password", password);
        form.put("loginType", "tenant");
        return client.execute("oauth2.login", "1.0.0", form);
    }

    @Override
    public Response<JSONObject> loginAccessToken(String accessToken) throws ClientException {
        Map<String, Object> form = new HashMap<>(1);
        form.put("accessToken", accessToken);
        return client.execute("oauth2.account.info", "1.0.0", form);
    }

    @Override
    public Response<JSONObject> addUser(Map<String, Object> form) throws ClientException {
        return client.execute("oauth2.social.submit", "1.0.0", form);
    }

    @Override
    public Response<JSONObject> updateUser(Map<String, Object> form) throws ClientException {
        return client.execute("oauth2.social.update", "1.0.0", form);
    }
}
