package org.ta.pddserver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.ta.pddserver.model.enums.OrderProblemType;
import org.ta.pddserver.model.pddgd.request.GDTicketListRequest;
import org.ta.pddserver.model.pddgd.request.GDTicketReplyRequest;
import org.ta.pddserver.model.pddgd.request.GDUploadRequest;
import org.ta.pddserver.model.pddgd.response.GDTicketReplyRepsonse;
import org.ta.pddserver.model.pddgd.response.GDTicketRepsonse;
import org.ta.pddserver.model.pddjy.request.JYBaseRequestModel;
import org.ta.pddserver.model.pddjy.request.PddServiceConsoProblemOrderNotifyRequest;
import org.ta.pddserver.model.pddlg.request.LGBaseModel;
import org.ta.pddserver.utils.ImageUtils;
import org.ta.pddserver.utils.PddHttpTool;
import org.ta.pddserver.utils.PddSignTool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class GDTest3 {

    private static final String CLIENT_ID = "8f5f3f9c92e847b2894fc9c0afce3d61";
    private static final String CLIENT_SECRET = "281d79ad5117d20a51efb9e7b0d5f7ee336399b0";

    @Test
    public void test1() {
        GDTicketListRequest request = new GDTicketListRequest();
        request.setClient_id(CLIENT_ID);

        long end = new Date().getTime() / 1000;
        end = 1762244459;
        request.setEnd_updated_at(end);
        request.setStart_updated_at(end - 1800);
        request.setPage(1);
        request.setPage_size(100);
        request.setType("pdd.logistics.ticket.get");
        long time = new Date().getTime() / 1000;
        request.setTimestamp(time);
        request.setSign(PddSignTool.generateSign(request, CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPostGD(request);
        log.info("resp = {}", resp);
        JSONObject jsonObject = JSON.parseObject(resp);
        List<GDTicketRepsonse> list = jsonObject.getJSONArray("list").toJavaList(GDTicketRepsonse.class);
        JSONArray array = jsonObject.getJSONArray("logistics_ticket_list");



    }

    @Test
    public void Test2(){
        Long ticketId = 176277067809089L;
        String waybill_no = "KRJDV3298516266148";

        GDTicketReplyRequest replyRequest = new GDTicketReplyRequest();
        replyRequest.setTicket_id(ticketId);
        replyRequest.setWaybill_no(waybill_no);
        replyRequest.setHandle_result("工单回复22222");
        replyRequest.setSign_state(1);
        replyRequest.setCompensate_state(2);
        replyRequest.setCompensate_amount(100);
        replyRequest.setDuty(1);
        replyRequest.setExpress_dealer("处理人2");
        replyRequest.setExpress_dealer_contact("15067011004");
        replyRequest.setReply_type(1);
        replyRequest.setAttach_path_list("[\"https://img-test.pddpic.com/open-gw/20add7022e/e7247b32584d7ae52b7f621d22f3c70a.jpg\"]");


        replyRequest.setClient_id(CLIENT_ID);

        replyRequest.setType("pdd.logistics.ticket.notify");
        long time = new Date().getTime() / 1000;
        replyRequest.setTimestamp(time);
        replyRequest.setSign(PddSignTool.generateSign(replyRequest, CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPostGD(replyRequest);
        log.info("resp = {}", resp);

        JSONObject respObj = JSON.parseObject(resp);
        JSONObject respObject = respObj.getJSONObject("logistics_ticket_notify_response");
        if (respObject == null) {
            return;
        }
        GDTicketReplyRepsonse replyRepsonse = respObject.toJavaObject(GDTicketReplyRepsonse.class);
        log.info("replyRepsonse is_success:{}", replyRepsonse.getIs_success());
    }


    @Test
    public void uploadImage() throws IOException {

        GDUploadRequest request = new GDUploadRequest();
        request.setClient_id(CLIENT_ID);

        request.setType("pdd.logistics.ticket.image.upload");
        long time = new Date().getTime() / 1000;
        request.setTimestamp(time);
        request.setImage(new ImageUtils().downloadCompressToBase64("https://images.v2.tradeany.com/files/20251030/5eaac48c24c5427fa9e055d0976d5e89.jpg"));
        request.setSign(PddSignTool.generateSign(request, CLIENT_SECRET));
        String resp = PddHttpTool.sendJsonPostGD(request);
        JSONObject jsonObject = JSONObject.parseObject(resp);
        log.info("resp = {}", resp);
        log.info("image_url = {}", jsonObject.getJSONObject("logistics_ticket_image_upload_response")
                .getString("image_url"));

    }
}
