package com.uas.eis.sdk.okhttp;
import com.alibaba.fastjson.JSON;
import com.uas.eis.sdk.core.HttpMethod;
import com.uas.eis.sdk.core.HttpRequest;
import com.uas.eis.sdk.core.HttpResponse;
import com.uas.eis.sdk.entity.Result;
import com.uas.eis.sdk.resp.AccessTokenGetResp;
import com.uas.eis.sdk.resp.SellerGetResp;
import com.uas.eis.sdk.sign.ApiSign;
import lombok.Builder;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;

@Data
@Builder
public class AKRestClient {

    private String endpoint;

    public AKRestClient(String endpoint) {
        this.endpoint = endpoint;
    }

    public AccessTokenGetResp getAccessToken(String appId, String appSecret) throws Exception {
        HttpRequest<Result> build = HttpRequest.builder(Result.class)
                .method(HttpMethod.POST)
                .endpoint(this.endpoint)
                .path("api/auth-server/oauth/access-token")
                .queryParam("appId", appId)
                .queryParam("appSecret", appSecret)
                .build();
        HttpResponse execute = HttpExecutor.create().execute(build);
        return execute.readEntity(AccessTokenGetResp.class);
    }

    public Object refreshToken(String appId, String refreshToken) throws Exception {
        HttpRequest<Object> build = HttpRequest.builder(Object.class)
                .method(HttpMethod.POST)
                .endpoint(this.endpoint)
                .path("api/auth-server/oauth/refresh")
                .queryParam("appId", appId)
                .queryParam("refreshToken", refreshToken)
                .build();
        HttpResponse execute = HttpExecutor.create().execute(build);
        return execute.readEntity(Object.class);
    }

    public Object sign(Map<String, Object> params) throws Exception {
        HttpRequest<Object> build = HttpRequest.builder(Object.class)
                .method(HttpMethod.POST)
                .endpoint(this.endpoint)
                .path("api/auth-server/oauth/api/authorize")
                .queryParams(params)
                .build();
        HttpResponse execute = HttpExecutor.create().execute(build);
        return execute.readEntity(Object.class);
    }

    public SellerGetResp getSellers(String appId, String access_token) throws Exception {
        Map<String, Object> requestHeaders = defaultHeaders(appId, access_token);
        String sign = getRequestSign(appId, requestHeaders, new HashMap<>());
        HttpRequest<Object> build = HttpRequest.builder(Object.class)
                .method(HttpMethod.GET)
                .endpoint(this.endpoint)
                .path("erp/sc/data/seller/lists")
                .queryParams(requestHeaders)
                .queryParam("sign", sign)
                .build();
        HttpResponse execute = HttpExecutor.create().execute(build);
        return execute.readEntity(SellerGetResp.class);
    }
    public Object getSellerOrders(String appId,  String access_token, Map<String, Object> requestParams) throws Exception {
        Map<String, Object> requestHeaders = defaultHeaders(appId, access_token);
        String sign = getRequestSign(appId, requestHeaders, requestParams);
        HttpRequest<Object> build = HttpRequest.builder(Object.class)
                .method(HttpMethod.POST)
                .endpoint(this.endpoint)
                .path("erp/sc/data/mws/orders")
                .queryParams(requestHeaders)
                .queryParam("sign", sign)
                .json(JSON.toJSONString(requestParams))
                .build();
        HttpResponse execute = HttpExecutor.create().execute(build);
        return execute.readEntity(Object.class);
    }

    public Map<String,Object> defaultHeaders(String appId,String access_token) {
        Map<String, Object> requestHeaders = new HashMap<>();
        requestHeaders.put("timestamp", System.currentTimeMillis() / 1000 + "");
        requestHeaders.put("access_token", access_token);
        requestHeaders.put("app_key", appId);
        return requestHeaders;
    }

    public String getRequestSign(String appSecret,  Map<String,Object> requestHeaders, Map<String,Object> requestParams) {
        Map<String, Object> signMap = new HashMap<>();
        signMap.putAll(requestHeaders);
        if(!requestParams.isEmpty()) {
            signMap.putAll(requestParams);
        }
        return ApiSign.sign(signMap, appSecret);

    }

}
