package com.ycwl.basic.controller.pc;

import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
import com.ycwl.basic.model.pc.printer.req.PrinterPreferredSizeUpdateReq;
import com.ycwl.basic.model.pc.printer.req.PrinterStatusUpdateReq;
import com.ycwl.basic.model.pc.printer.req.PrinterUsePrinterUpdateReq;
import com.ycwl.basic.service.printer.PrinterService;
import com.ycwl.basic.utils.ApiConst;
import com.ycwl.basic.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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/pc/printers/v1")
@RequiredArgsConstructor
public class PrinterManageController {

    private final PrinterService printerService;

    /**
     * 打印机列表查询
     */
    @GetMapping
    public ApiResponse<List<PrinterEntity>> list(@RequestParam(value = "scenicId", required = false) Long scenicId,
                                                 @RequestParam(value = "status", required = false) Integer status,
                                                 @RequestParam(value = "name", required = false) String name) {
        PrinterEntity condition = new PrinterEntity();
        condition.setScenicId(scenicId);
        condition.setStatus(status);
        condition.setName(name);
        return printerService.list(condition);
    }

    /**
     * 打印机详情
     */
    @GetMapping("/{id}")
    public ApiResponse<PrinterEntity> detail(@PathVariable("id") Integer id) {
        ApiResponse<PrinterEntity> response = printerService.get(id);
        if (response.getData() == null) {
            return ApiResponse.buildResponse(ApiConst.Code.CODE_NOT_EXIST, "打印机不存在");
        }
        return response;
    }

    /**
     * 新增打印机
     */
    @PostMapping
    public ApiResponse<Integer> create(@RequestBody PrinterEntity request) {
        request.setId(null);
        return printerService.add(request);
    }

    /**
     * 更新打印机信息
     */
    @PutMapping("/{id}")
    public ApiResponse<Integer> update(@PathVariable("id") Integer id, @RequestBody PrinterEntity request) {
        request.setId(id);
        return printerService.update(request);
    }

    /**
     * 更新打印机状态
     */
    @PatchMapping("/{id}/status")
    public ApiResponse<Integer> updateStatus(@PathVariable("id") Integer id,
                                             @RequestBody PrinterStatusUpdateReq req) {
        if (req == null || req.getStatus() == null) {
            return ApiResponse.buildResponse(ApiConst.Code.CODE_PARAM_ERROR, "状态不能为空");
        }
        PrinterEntity entity = new PrinterEntity();
        entity.setId(id);
        entity.setStatus(req.getStatus());
        return printerService.update(entity);
    }

    /**
     * 更新打印机首选尺寸
     */
    @PatchMapping("/{id}/preferred-size")
    public ApiResponse<Integer> updatePreferredSize(@PathVariable("id") Integer id,
                                                    @RequestBody PrinterPreferredSizeUpdateReq req) {
        if (req == null || (req.getPreferW() == null && req.getPreferH() == null)) {
            return ApiResponse.buildResponse(ApiConst.Code.CODE_PARAM_ERROR, "首选尺寸不能为空");
        }
        PrinterEntity entity = new PrinterEntity();
        entity.setId(id);
        entity.setPreferW(req.getPreferW());
        entity.setPreferH(req.getPreferH());
        return printerService.update(entity);
    }

    /**
     * 更新当前使用的打印机
     */
    @PatchMapping("/{id}/use-printer")
    public ApiResponse<Integer> updateUsePrinter(@PathVariable("id") Integer id,
                                                 @RequestBody PrinterUsePrinterUpdateReq req) {
        if (req == null) {
            return ApiResponse.buildResponse(ApiConst.Code.CODE_PARAM_ERROR, "请求参数不能为空");
        }
        PrinterEntity entity = new PrinterEntity();
        entity.setId(id);
        entity.setUsePrinter(req.getUsePrinter());
        return printerService.update(entity);
    }

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