package org.ta.pddserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.ta.pddserver.entity.*;
import org.ta.pddserver.mapper.TraceObjectMapper;
import org.ta.pddserver.model.pddjy.request.*;
import org.ta.pddserver.model.pddlg.common.TraceObject;
import org.ta.pddserver.model.pddlg.request.LGBaseModel;
import org.ta.pddserver.model.pddlg.request.PddLogisticsCoTrackNotifyRequest;
import org.ta.pddserver.model.pddlg.request.PddLogisticsCoTrackQueryRequest;
import org.ta.pddserver.model.pddlg.request.PddLogisticsCoTrackSubRequest;
import org.ta.pddserver.model.pddlg.response.PddLogisticsCoTrackNotifyResponse;
import org.ta.pddserver.service.impl.*;
import org.ta.pddserver.utils.JsonResponse;
import org.ta.pddserver.utils.PddHttpTool;
import org.ta.pddserver.utils.PddSignTool;

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

@Slf4j
@RestController
public class PddFZController {

    @Resource
    private TraceObjectImpl traceObjectImpl;


    @GetMapping("/notify/{code}")
    public void subscribe(@PathVariable("code") String code) {
        // code 给拼多多要发的订单code
       // 根据code 查询相应信息组装发给pdd


        PddLogisticsCoTrackNotifyRequest request = new PddLogisticsCoTrackNotifyRequest();
        request.setClient_id("8f5f3f9c92e847b2894fc9c0afce3d61");
        request.setShip_id("567");
        request.setTarget_client_id("8f5f3f9c92e847b2894fc9c0afce3d61");
        request.setTimestamp(System.currentTimeMillis() / 1000);
        PddLogisticsCoTrackNotifyRequest.DataObject dataObject = getDataObject(code);
        request.setDataObject(dataObject);
        request.encodeData();
        request.setSign(PddSignTool.generateSign(request, "281d79ad5117d20a51efb9e7b0d5f7ee336399b0"));
        log.info("requestModel:{}", JSON.toJSONString(request));
        String resp = PddHttpTool.sendJsonPost(request);
        log.info("resp = {}", resp);
        PddLogisticsCoTrackNotifyResponse response = JSON.parseObject(resp, PddLogisticsCoTrackNotifyResponse.class);
        log.info("response = {}", JSON.toJSONString(response));
    }

    private  PddLogisticsCoTrackNotifyRequest.DataObject getDataObject(String code) {
        PddLogisticsCoTrackNotifyRequest.DataObject dataObjects = new PddLogisticsCoTrackNotifyRequest.DataObject();
        dataObjects.setTraces(new ArrayList<>());

        List<TraceObjectEntity> list = traceObjectImpl.selectByTrackingNumber(code);
        List<TraceObject> traceObjects = new ArrayList<>();
        for (TraceObjectEntity traceObjectEntity : list) {
            TraceObject traceObject = new TraceObject();

            traceObject = TraceObject.createFromTraceEntity(traceObjectEntity);

            dataObjects.getTraces().add(traceObject);
        }

        return dataObjects;
    }

}
