package org.ta.pddserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.ta.pddserver.model.pddgd.request.GDTicketReplyRequest;
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.utils.ImageUtils;
import org.ta.pddserver.utils.PddHttpTool;
import org.ta.pddserver.utils.PddSignTool;

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

@Slf4j
@RestController
public class PddGDController {
    private static final String CLIENT_ID = "8f5f3f9c92e847b2894fc9c0afce3d61";
    private static final String CLIENT_SECRET = "281d79ad5117d20a51efb9e7b0d5f7ee336399b0";

    public JSONObject getTicketList() {
        JSONObject jsonObject = new JSONObject();
        long end = new Date().getTime() / 1000;
        jsonObject.put("end_updated_at", end);
        jsonObject.put("start_updated_at", end - 1800);
        jsonObject.put("page", 1);
        jsonObject.put("page_size", 100);
        jsonObject.put("client_id", CLIENT_ID);

        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(jsonObject));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.logistics.ticket.get");
        requestModel.setTarget_client_id(CLIENT_ID);
        requestModel.setClient_id(CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, CLIENT_SECRET));
        log.info("requestModel:{}", JSON.toJSONString(requestModel));
//        String resp = PddHttpTool.postRequest(requestModel);
        String resp = PddHttpTool.sendJsonPostGD(requestModel);
        log.info("resp = {}", resp);
        JSONObject respObj = JSON.parseObject(resp);
        int count = respObj.getInteger("total_count");
        JSONArray list = respObj.getJSONArray("logistics_ticket_list");
        if (!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject ticket = list.getJSONObject(i);
                GDTicketRepsonse gdTicketRepsonse = ticket.toJavaObject(GDTicketRepsonse.class);
                // todo 保存数据
            }
        }
        return respObj;
    }

    public JSONObject replyTicket() {

        Long ticketId = 1L;
        String waybill_no = "waybill_no";

        GDTicketReplyRequest replyRequest = new GDTicketReplyRequest();
        replyRequest.setTicket_id(ticketId);
        replyRequest.setWaybill_no(waybill_no);
        replyRequest.setHandle_result("无");
        replyRequest.setSign_state(1);
        replyRequest.setCompensate_state(1);
        replyRequest.setDuty(0);
        replyRequest.setExpress_dealer("XXX");
        replyRequest.setExpress_dealer_contact("18599877823");
        replyRequest.setReply_type(1);

        JYBaseRequestModel requestModel = new JYBaseRequestModel();
        requestModel.setRequest(JSON.toJSONString(replyRequest));
        requestModel.setRequest_id("" + new Date().getTime());
        requestModel.setType("pdd.logistics.ticket.get");
        requestModel.setTarget_client_id(CLIENT_ID);
        requestModel.setClient_id(CLIENT_ID);
        long time = new Date().getTime() / 1000;
        requestModel.setTimestamp(time);
        requestModel.setSign(PddSignTool.generateSign(requestModel, CLIENT_SECRET));
        log.info("requestModel:{}", JSON.toJSONString(requestModel));
//        String resp = PddHttpTool.postRequest(requestModel);
        String resp = PddHttpTool.sendJsonPostGD(requestModel);
        log.info("resp = {}", resp);
        JSONObject respObj = JSON.parseObject(resp);
        JSONObject respObject = respObj.getJSONObject("logistics_ticket_notify_response");
        GDTicketReplyRepsonse replyRepsonse = respObject.toJavaObject(GDTicketReplyRepsonse.class);
        log.info("replyRepsonse is_success:{}", replyRepsonse.getIs_success());
        return respObj;
    }

}
