package com.ycwl.basic.controller.printer;


import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.integration.device.dto.device.DeviceV2DTO;
import com.ycwl.basic.integration.device.service.DeviceIntegrationService;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
import com.ycwl.basic.model.pc.mp.MpConfigEntity;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.repository.DeviceRepository;
import com.ycwl.basic.repository.FaceRepository;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.ScenicService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.WxMpUtil;
import jakarta.websocket.server.PathParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

@IgnoreToken
// 打印机大屏对接接口
@RestController
@RequestMapping("/printer/v1/tv")
@RequiredArgsConstructor
public class PrinterTvController {

    private final DeviceRepository deviceRepository;
    private final ScenicRepository scenicRepository;
    private final FaceRepository faceRepository;

    /**
     * 获取景区列表
     *
     * @return 景区列表
     */
    @GetMapping("/scenic/list")
    public ApiResponse<List<ScenicV2DTO>> getScenicList() {
        ScenicReqQuery query = new ScenicReqQuery();
        query.setStatus("1"); // 只查询启用状态的景区
        query.setPageNum(1);
        query.setPageSize(1000);
        return ApiResponse.success(scenicRepository.list(query));
    }

    /**
     * 根据景区ID查询设备列表
     *
     * @param scenicId 景区ID
     * @return 设备列表
     */
    @GetMapping("/device/list")
    public ApiResponse<List<DeviceV2DTO>> getDeviceListByScenicId(@RequestParam Long scenicId) {
        List<DeviceV2DTO> result = deviceRepository.getAllDeviceByScenicId(scenicId);
        return ApiResponse.success(result);
    }

    @GetMapping("/{sampleId}/qrcode")
    public void getQrcode(@PathVariable("sampleId") Long sampleId, HttpServletResponse response) throws Exception {
        File qrcode = new File("qrcode_"+sampleId+".jpg");
        try {
            FaceSampleEntity faceSample = faceRepository.getFaceSample(sampleId);
            MpConfigEntity scenicMpConfig = scenicRepository.getScenicMpConfig(faceSample.getScenicId());
            WxMpUtil.generateUnlimitedWXAQRCode(scenicMpConfig.getAppId(), scenicMpConfig.getAppSecret(), "pages/printer/from_sample", sampleId.toString(), qrcode);

            // 设置响应头
            response.setContentType("image/jpeg");
            response.setHeader("Content-Disposition", "inline; filename=\"" + qrcode.getName() + "\"");

            // 将二维码文件写入响应输出流
            try (FileInputStream fis = new FileInputStream(qrcode);
                 OutputStream os = response.getOutputStream()) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.flush();
            }
        } finally {
            // 删除临时文件
            if (qrcode.exists()) {
                qrcode.delete();
            }
        }
    }
}
