package com.ruoyi.ap;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.global.iop.api.IopClient;
import com.global.iop.api.IopClientImpl;
import com.global.iop.api.IopRequest;
import com.global.iop.api.IopResponse;
import com.global.iop.domain.Protocol;
import com.global.iop.util.ApiException;
import com.global.iop.util.Constants;
import com.global.iop.util.FileItem;

/**
 * @ClassName AliexpressTest
 * @Description TODO
 * @Author xxs
 * @Date 2024/2/22 15:32
 * @Version 1.0
 **/
public class AliexpressTest {
    private static IopClient iopClient = new IopClientImpl("https://api-sg.aliexpress.com", "500516", "5KD1nXaPxADnXUxISAImi1gcgPqbKMN4");;
    private static String token;

    public static void init() throws ApiException {
        iopClient = new IopClientImpl("https://api-sg.aliexpress.com", "500516", "5KD1nXaPxADnXUxISAImi1gcgPqbKMN4");
        IopRequest request = new IopRequest();
        request.setApiName("/auth/token/create");
        request.addApiParameter("code", "0_2DL4DV3jcU1UOT7WGI1A4rY91");
        request.addApiParameter("uuid", "uuid");
        IopResponse response = iopClient.execute(request, Protocol.GOP);
        token = response.getGopResponseBody();
    }

    // 4.换取access_token 调用: /auth/token/create 获取access_token。
    public static void authToke() throws ApiException {
//        iopClient = new IopClientImpl("https://api-sg.aliexpress.com", "500516", "5KD1nXaPxADnXUxISAImi1gcgPqbKMN4");
        IopRequest request = new IopRequest();
        request.setApiName("/auth/token/create");
        request.addApiParameter("code", "3_500516_dfXuvcFlRyBdEptp8jE2f4xx6122");
//        request.addApiParameter("uuid", "uuid");
        IopResponse response = iopClient.execute(request, Protocol.GOP);
        System.out.println("response:" + response);
        String responseBody = response.getGopResponseBody();
        JSONObject entries = JSONUtil.parseObj(responseBody);
        System.out.println("token:" + token);
        token = entries.get("access_token").toString();
        System.out.println("accessToken:" + token);
    }


    public static void getProductList() throws ApiException {
        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.solution.product.list.get");
        request.addApiParameter("aeop_a_e_product_list_query", "{\"gmt_modified_start\":\"2012-01-01 12:13:14\",\"gmt_modified_end\":\"2012-01-01 12:13:14\",\"off_line_time\":\"7\",\"gmt_create_start\":\"2012-01-01 12:13:14\",\"subject\":\"knew odd\",\"have_national_quote\":\"n\",\"ws_display\":\"expire_offline\",\"product_status_type\":\"onSelling\",\"owner_member_id\":\"aliqatest01\",\"gmt_create_end\":\"2012-01-01 12:13:14\",\"group_id\":\"1234\",\"product_id\":\"123\",\"excepted_product_ids\":[\"[32962333569,32813963253]\",\"[32962333569,32813963253]\"],\"sku_code\":\"123ABC\",\"current_page\":\"2\",\"page_size\":\"30\"}");
        IopResponse response = iopClient.execute(request, "50000101922g2UZqpeWbbweOUTkniPJ4fx18c3d40fxGDxHPqENvxQrQZZvqiUibKx01", Protocol.TOP);
        System.out.println(response.getGopResponseBody());
    }

    public static void main(String[] args) throws Exception {
//        authToke();

        token = "50000100810eAzdp7j9nwt11e7586aQDBYn1hnSitvhdCbyemVxjhEPKZHhuGq44F3KQ";
//        testUpload();
//        testQuery();
//        group();
        updateImg();
        imgList();
//        getPhotoInfo();
    }

    public static void updateImg() throws ApiException {
        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.photobank.redefining.uploadimage");
        request.addApiParameter("file_name", "001.jpg");
        request.addApiParameter("group_id", "0");
        request.addFileParameter("image_bytes",new FileItem("E:\\工作资料\\图片素材\\001.jpg"));
        IopResponse response = iopClient.execute(request, token, Protocol.TOP);
        System.out.println(response.getGopResponseBody());
    }

    public static void testQuery() throws Exception {
//        IopRequest iopRequest = new IopRequest("aliexpress.photobank.redefining.queryphotobankimagebypaths");
//        iopRequest.setHttpMethod(Constants.METHOD_POST);
//        iopRequest.addApiParameter("paths", "001.jpg");
//        //tip：请注意simplify为请求公共参数，在设置该参数时请作为一级参数单独设置，不要作为任意子参数放到业务参数下（即单独使用下面一行代码）
////        iopRequest.addApiParameter("simplify", "true");
//            IopResponse rsp = iopClient.execute(iopRequest, token, Protocol.TOP);
//        System.out.println("testQuery:" + JSONUtil.toJsonStr(rsp));

        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.photobank.redefining.queryphotobankimagebypaths");
        request.addApiParameter("paths", "001.jpg");
        IopResponse response = iopClient.execute(request, token, Protocol.TOP);
        System.out.println("testQuery:" + JSONUtil.toJsonStr(response.getGopResponseBody()));
    }

    public static void imgList() throws ApiException {
        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.photobank.redefining.listimagepagination");
        request.addApiParameter("aeop_image_pagination_request", "{\"group_id\":\"0\", \"current_page\":\"1\",\"location_type\":\"0\",\"page_size\":\"10\"}");
        IopResponse response = iopClient.execute(request, token, Protocol.TOP);
        System.out.println("list:" + response.getGopResponseBody());
    }

    public static void group() throws ApiException {
        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.photobank.redefining.listgroup");
//        request.addApiParameter("group_id", "8401");
        IopResponse response = iopClient.execute(request, token, Protocol.TOP);
        System.out.println(response.getGopResponseBody());
    }

    public static void getPhotoInfo() throws ApiException {
        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.photobank.redefining.getphotobankinfo");
        IopResponse response = iopClient.execute(request, token, Protocol.TOP);
        System.out.println(response.getGopResponseBody());
    }

    public static void testUpload() throws Exception {
        IopRequest iopRequest = new IopRequest("aliexpress.image.redefining.uploadtempimage");
        iopRequest.setHttpMethod(Constants.METHOD_POST);
        iopRequest.addFileParameter("file_data", new FileItem("E:\\工作资料\\图片素材\\001.jpg"));
        iopRequest.addApiParameter("src_file_name", "001.jpg");
        iopRequest.addApiParameter("simplify", "true");
        // 如果是TOP协议下的老api 则选择top协议，apiName以'.'为分隔符的是TOP协议 比如aliexpress.image.redefining.uploadtempimage
        // 如果是在新开放平台(GOP)配置的api，则选择GOP协议，apiName以'/'为分隔符
        // 具体某个API属于哪一种协议请参考每个API页面的sample code
        IopResponse rsp = iopClient.execute(iopRequest, token, Protocol.TOP);
        System.out.println("testUpload:" + JSONUtil.toJsonStr(rsp));
    }


    public void testGopTraditionalApi() throws Exception {
        IopRequest iopRequest = new IopRequest("/globalopen/api/get/a");
        iopRequest.setHttpMethod(Constants.METHOD_GET);
        iopRequest.addApiParameter("apiId", "1");
        iopRequest.addApiParameter("simplify", "true");
        IopResponse rsp = iopClient.execute(iopRequest, Protocol.GOP);
        System.out.println(JSONUtil.toJsonStr(rsp));
    }

    public void testGopRestfulApi() throws Exception {
        // path参数
        IopRequest iopRequest = new IopRequest("/globalopen/api/getApiDsl/{region}/{env}/{apiId}");
        iopRequest.setHttpMethod(Constants.METHOD_PUT);
        iopRequest.addApiParameter("region", "global");
        iopRequest.addApiParameter("env", "PRE");
        iopRequest.addApiParameter("apiId", "1");
        iopRequest.addApiParameter("simplify", "true");
        IopResponse rsp = iopClient.execute(iopRequest, token, Protocol.REST_VND_2);
        System.out.println(JSONUtil.toJsonStr(rsp));
    }
}
