package com.usky.ai.controller.web;

import com.usky.ai.dto.*;
import com.usky.ai.service.EdgeMediaService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/edge-alg")
@RequiredArgsConstructor
public class EdgeAlgController {

    private final EdgeMediaService service;

    //算法任务配置
    @PostMapping("/task/config")
    public Object configTask(@RequestBody EdgeAlgTaskConfigRequestDTO req) {
        return service.configTask(req);
    }

    //算法任务控制
    @PostMapping("/task/control")
    public Object controlTask(@RequestParam String boardId,
                              @RequestParam String session,
                              @RequestParam Integer command) {
        return service.controlTask(boardId, session, command);
    }

    //算法任务删除
    @DeleteMapping("/task/{boardId}/{session}")
    public Object deleteTask(@PathVariable String boardId,
                             @PathVariable String session) {
        return service.deleteTask(boardId, session);
    }

    //算法任务查询
    @GetMapping("/tasks/{boardId}")
    public Object fetchTasks(@PathVariable String boardId) {
        return service.fetchTasks(boardId);
    }

    //新建计划模板
    @PostMapping("/schedule/create")
    public Object createSchedule(@RequestBody AlgScheduleCreateRequestDTO req) {
        return service.createSchedule(req);
    }

    //查询计划模板
    @GetMapping("/schedule/fetch/{boardId}")
    public Object fetchSchedules(@PathVariable String boardId) {
        AlgScheduleFetchResponseDTO response = service.fetchSchedules(boardId);
        System.out.println("获取到的计划模板响应: " + response);

        // 获取计划模板数据，优先从result.content获取，其次从content获取
        List<AlgScheduleFetchResponseDTO.Schedule> schedules = new ArrayList<>();
        if (response.isSuccess()) {
            if (response.getResult() != null && response.getResult().getContent() != null && !response.getResult().getContent().isEmpty()) {
                schedules = response.getResult().getContent();
            } else if (response.getContent() != null && !response.getContent().isEmpty()) {
                schedules = response.getContent();
            }
        }

        // 直接返回原始响应数据，不进行解析
        if (!schedules.isEmpty()) {
            // 打印每个计划模板的信息用于调试
            for (AlgScheduleFetchResponseDTO.Schedule schedule : schedules) {
                System.out.println("计划模板: ID=" + schedule.getId() +
                        ", Name=" + schedule.getName() +
                        ", Value长度=" + (schedule.getValue() != null ? schedule.getValue().length() : "null") +
                        ", Value=" + schedule.getValue());
            }
            return schedules;
        } else {
            List<String> result = new ArrayList<>();
            result.add("No schedules found");
            return result;
        }
    }

    //删除计划模板
    @DeleteMapping("/schedule/delete/{boardId}/{id}")
    public Object deleteSchedule(@PathVariable String boardId,
                                 @PathVariable Long id) {
        return service.deleteSchedule(boardId, id);
    }

    //新建工装组
    @PostMapping("/suit/create")
    public Object createSuitGroup(@RequestBody AlgSuitCreateRequestDTO req) {
        return service.createSuitGroup(req);
    }

    //删除工装组
    @DeleteMapping("/suit/group/{boardId}/{sid}")
    public Object removeSuitGroup(@PathVariable String boardId, @PathVariable int sid) {
        AlgSuitGroupRemoveRequestDTO req = new AlgSuitGroupRemoveRequestDTO();
        req.setBoardId(boardId);
        req.setSid(sid);
        return service.removeSuitGroup(req);
    }

    //上传工装模版
    @PostMapping("/suit/append")
    public Object appendSuitTemplate(@RequestBody AlgSuitAppendRequestDTO req) {
        return service.appendSuitTemplate(req);
    }

    //删除工装模版
    @DeleteMapping("/suit/remove/{boardId}/{sid}/{fid}")
    public Object removeSuitTemplate(@PathVariable String boardId, @PathVariable int sid, @PathVariable int fid) {
        AlgSuitRemoveRequestDTO req = new AlgSuitRemoveRequestDTO();
        req.setBoardId(boardId);
        req.setSid(sid);
        req.setFid(fid);
        return service.removeSuitTemplate(req);
    }

    //获取当前配置的模板信息
    @GetMapping("/suit/fetch/{boardId}")
    public Object fetchSuitGroup(@PathVariable String boardId) {
        return service.fetchSuitGroup(boardId);
    }

}