package com.ycwl.basic.controller.pc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.PrintTaskMapper;
import com.ycwl.basic.mapper.PrinterGuideMapper;
import com.ycwl.basic.model.pc.printer.entity.PrintTaskEntity;
import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
import com.ycwl.basic.model.pc.printer.entity.PrinterGuideEntity;
import com.ycwl.basic.model.pc.printer.req.PrintTaskReqQuery;
import com.ycwl.basic.model.pc.printer.req.ReprintRequest;
import com.ycwl.basic.service.printer.PrinterService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/printer/v1")
public class PrinterController {
    @Autowired
    private PrinterService printerService;

    @Autowired
    private PrintTaskMapper printTaskMapper;

    @Autowired
    private PrinterGuideMapper printerGuideMapper;

    // 查询列表
    @PostMapping("/list")
    public ApiResponse<List<PrinterEntity>> list(@RequestBody PrinterEntity condition) {
        return printerService.list(condition);
    }

    // 获取详情
    @GetMapping("/get/{id}")
    public ApiResponse<PrinterEntity> get(@PathVariable("id") Integer id) {
        return printerService.get(id);
    }

    // 新增
    @PostMapping("/add")
    public ApiResponse<Integer> add(@RequestBody PrinterEntity entity) {
        return printerService.add(entity);
    }

    // 更新
    @PostMapping("/update")
    public ApiResponse<Integer> update(@RequestBody PrinterEntity entity) {
        return printerService.update(entity);
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public ApiResponse<Integer> delete(@PathVariable("id") Integer id) {
        return printerService.delete(id);
    }

    // 分页查询打印任务
    @PostMapping("/task/page")
    public ApiResponse<PageInfo<PrintTaskEntity>> taskPage(@RequestBody PrintTaskReqQuery req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<PrintTaskEntity> list = printTaskMapper.queryByCondition(req.getPrinterId(), req.getStatus());
        PageInfo<PrintTaskEntity> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    // 重新打印(将状态设置为0-未开始,并更新打印机名称)
    @PostMapping("/task/reprint/{id}")
    public ApiResponse<Integer> reprint(@PathVariable("id") Integer id, @RequestBody ReprintRequest request) {
        int result = printerService.handleReprint(id, request);
        return ApiResponse.success(result);
    }

    /**
     * 查询待审核的打印任务
     * @param printerId 打印机ID(可选)
     * @return 待审核任务列表
     */
    @GetMapping("/task/pending-review")
    public ApiResponse<List<PrintTaskEntity>> getPendingReviewTasks(Integer printerId) {
        List<PrintTaskEntity> tasks = printerService.getPendingReviewTasks(printerId);
        return ApiResponse.success(tasks);
    }

    /**
     * 更新待审核任务的URL（重新处理水印等）
     * @param taskId 任务ID
     * @param url 新的打印URL
     * @return 操作结果
     */
    @PostMapping("/task/{taskId}/url")
    public ApiResponse<Boolean> updateTaskUrl(@PathVariable("taskId") Integer taskId, @RequestBody String url) {
        boolean success = printerService.updatePendingReviewTaskUrl(taskId, url);
        return ApiResponse.success(success);
    }

    /**
     * 批准待审核任务，下发到打印队列
     * @param taskIds 任务ID列表
     * @return 成功数量
     */
    @PostMapping("/task/approve")
    public ApiResponse<Integer> approveTasks(@RequestBody List<Integer> taskIds) {
        int count = printerService.approvePrintTasks(taskIds);
        return ApiResponse.success(count);
    }

    /**
     * 拒绝待审核任务
     * @param taskIds 任务ID列表
     * @return 成功数量
     */
    @PostMapping("/task/reject")
    public ApiResponse<Integer> rejectTasks(@RequestBody List<Integer> taskIds) {
        int count = printerService.rejectPrintTasks(taskIds);
        return ApiResponse.success(count);
    }

    // 查询打印机所有指引图片（含禁用）
    @GetMapping("/guide/list/{printerId}")
    public ApiResponse<List<PrinterGuideEntity>> guideList(@PathVariable("printerId") Integer printerId) {
        return ApiResponse.success(printerGuideMapper.listByPrinterId(printerId));
    }

    // 添加指引图片
    @PostMapping("/guide/add")
    public ApiResponse<Integer> guideAdd(@RequestBody PrinterGuideEntity entity) {
        if (entity.getSortOrder() == null) {
            entity.setSortOrder(0);
        }
        if (entity.getEnabled() == null) {
            entity.setEnabled(1);
        }
        printerGuideMapper.insertGuide(entity);
        return ApiResponse.success(entity.getId());
    }

    // 删除指引图片
    @DeleteMapping("/guide/delete/{id}")
    public ApiResponse<Integer> guideDelete(@PathVariable("id") Integer id) {
        return ApiResponse.success(printerGuideMapper.deleteById(id));
    }

    // 修改指引图片排序
    @PostMapping("/guide/updateSort/{id}")
    public ApiResponse<Integer> guideUpdateSort(@PathVariable("id") Integer id, @RequestParam("sortOrder") Integer sortOrder) {
        return ApiResponse.success(printerGuideMapper.updateSortOrder(id, sortOrder));
    }

    // 切换指引图片启用/禁用
    @PostMapping("/guide/toggleEnabled/{id}")
    public ApiResponse<Integer> guideToggleEnabled(@PathVariable("id") Integer id) {
        return ApiResponse.success(printerGuideMapper.toggleEnabled(id));
    }
}