package com.zg.project.wisdom.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zg.common.utils.http.HttpUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mock")
public class MockRemoteController {

    @PostMapping("/wcs/task/create")
    public Map<String, Object> mockWcs(@RequestBody Map<String, Object> param) {
        System.out.println("【MOCK-WCS】接收到下架或上架任务：" + param);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "WCS任务已接收");
        return result;
    }


    /**
     * 模拟 WCS 状态回调接口（发送 taskId 和状态数字 1~4）
     */
    @PostMapping("/wcs/task/callback")
    public Map<String, Object> mockWcsCallback(@RequestBody Map<String, Object> param) {
        System.out.println("【MOCK-WCS】模拟立库回调任务：" + param);

        String taskId = String.valueOf(param.get("taskId"));
        String callbackUrl = "http://localhost:8080/wisdom/wcs/callback"; // 改成你项目实际地址

        // 状态顺序：接收成功 → 开始执行 → 执行成功 → 执行失败
        List<String> statusList = Arrays.asList("1", "2", "3", "4");

        new Thread(() -> {
            ObjectMapper mapper = new ObjectMapper();
            for (String status : statusList) {
                Map<String, Object> callback = new HashMap<>();
                callback.put("taskId", taskId);
                callback.put("status", status);
                callback.put("msg", "模拟WCS状态：" + status);
                try {
                    String json = mapper.writeValueAsString(callback);
                    String resp = HttpUtils.sendPost(callbackUrl, json);
                    System.out.println("【MOCK-WCS】已回调状态：" + status + " → 响应：" + resp);
                } catch (Exception e) {
                    System.err.println("【MOCK-WCS】回调失败：" + status + " → " + e.getMessage());
                }
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }).start();

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "WCS模拟状态已开始逐步回调");
        return result;
    }

    /**
     * 模拟 AGV 状态回调接口（发送 requestId 和多个执行阶段状态）
     */
    @PostMapping("/agv/task/create")
    public Map<String, Object> mockAgv(@RequestBody Map<String, Object> param) {
        System.out.println("【MOCK-AGV】接收到搬运任务：" + param);

        String requestId = String.valueOf(param.get("requestId"));
        String callbackUrl = "http://localhost:8080/wisdom/agv/callback"; // 改成你实际项目地址

        // AGV 的标准执行流程状态
        List<String> statusList = Arrays.asList(
                "CREATED",
                "ALLOCATED",
                "WAITING_PICK_UP",
                "PICK_UP_COMPLETED",
                "WAITING_TAKE_DOWN",
                "END_ARRIVE",
                "TAKE_DOWN_COMPLETED",
                "FINISHED"
        );

        new Thread(() -> {
            ObjectMapper mapper = new ObjectMapper();
            for (String status : statusList) {
                Map<String, Object> callback = new HashMap<>();
                callback.put("requestId", requestId);
                callback.put("status", status);
                callback.put("msg", "模拟AGV状态：" + status);
                try {
                    String json = mapper.writeValueAsString(callback);
                    String resp = HttpUtils.sendPost(callbackUrl, json);
                    System.out.println("【MOCK-AGV】已回调状态：" + status + " → 响应：" + resp);
                } catch (Exception e) {
                    System.err.println("【MOCK-AGV】回调失败：" + status + " → " + e.getMessage());
                }
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }).start();

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "AGV模拟状态已开始逐步回调");
        return result;
    }
}
