package com.fuyuanshen.web.controller.device;

import com.fuyuanshen.equipment.domain.bo.AppOperationVideoBo;
import com.fuyuanshen.app.domain.dto.AppFileDto;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.exception.ServiceException;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.ratelimiter.annotation.FunctionAccessAnnotation;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
import com.fuyuanshen.equipment.domain.vo.WebDeviceVo;
import com.fuyuanshen.web.domain.Dto.DeviceDebugEditDto;
import com.fuyuanshen.web.domain.Dto.DeviceDebugLogoUploadDto;
import com.fuyuanshen.web.domain.vo.DeviceInfoVo;
import com.fuyuanshen.web.service.device.DeviceBizService;
import com.fuyuanshen.web.service.device.DeviceDebugService;
import com.fuyuanshen.web.service.device.DeviceXinghanBizService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 联调中心
 *
 * @author Lion Li
 * @date 2025-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("api/device/debug")
public class DeviceDebugController extends BaseController {

    private final DeviceBizService appDeviceService;
    private final DeviceXinghanBizService deviceXinghanBizService;
    private final DeviceDebugService deviceDebugService;

    /**
     * 查询设备列表
     */
    @GetMapping("/list")
    public TableDataInfo<WebDeviceVo> list(DeviceQueryCriteria bo, PageQuery pageQuery) {
        return appDeviceService.queryWebDeviceList(bo, pageQuery);
    }

    /**
     * 上传文件
     */
    @Log(title = "批量上传文件")
    @PostMapping("/addFile")
    public R<Void> uploadFile(@Validated @ModelAttribute AppFileDto bo) throws IOException {
        return toAjax(deviceDebugService.addFileHash(bo,true));
    }

    /**
     * 操作视频添加
     */
    @Log(title = "批量添加操作视频")
    @PostMapping("/addVideo")
    public R<Void> addOperationVideo(@RequestBody AppOperationVideoBo bo) {
        return toAjax(deviceDebugService.addVideoList(bo));
    }

    /**
     * 上传设备logo图片
     */
    @Log(title = "批量上传设备logo图片")
    @PostMapping("/addLogo")
    @FunctionAccessAnnotation("uploadLogo")
    public R<Void> uploadLogo670(@Validated @ModelAttribute DeviceDebugLogoUploadDto bo) {

        MultipartFile file = bo.getFile();
        if(file.getSize()>1024*1024*2){
            return R.warn("图片不能大于2M");
        }
        deviceXinghanBizService.uploadDeviceLogoBatch(bo);
        return R.ok();
    }

    /**
     * 设备详情
     */
    @Operation(summary = "设备详情")
    @GetMapping(value = "/detail/{id}")
    public R<DeviceInfoVo> getDeviceInfo(@PathVariable Long id) {
        return R.ok(deviceDebugService.getDeviceInfo(id));
    }

    /**
     * 修改设备联调信息
     */
    @Log(title = "修改设备联调信息")
    @PostMapping("/editDebug")
    public R<Void> editDeviceDebug(@Validated @ModelAttribute DeviceDebugEditDto bo) throws Exception {
        // 1. 基础参数必填校验
        validateDeviceDebugEdit(bo);

        // 修改上传设备说明
        if (bo.getExplanationFiles() != null) {
            AppFileDto appFileDto = new AppFileDto();
            appFileDto.setDeviceIds(new Long[]{ bo.getDeviceId() });
            appFileDto.setFileType(1L);
            appFileDto.setFiles(bo.getExplanationFiles());
            deviceDebugService.addFileHash(appFileDto,false);
        }
        // 修改上传设备参数
        if (bo.getParameterFiles() != null) {
            AppFileDto appFileDto = new AppFileDto();
            appFileDto.setDeviceIds(new Long[]{ bo.getDeviceId() });
            appFileDto.setFileType(2L);
            appFileDto.setFiles(bo.getParameterFiles());
            deviceDebugService.addFileHash(appFileDto,false);
        }
        deviceDebugService.delFile(bo.getFileIds());
        // 修改操作视频
        if (!bo.getVideoUrl().isEmpty()) {
            AppOperationVideoBo appOperationVideoBo = new AppOperationVideoBo();
            appOperationVideoBo.setDeviceIds(new Long[]{ bo.getDeviceId() });
            appOperationVideoBo.setVideoUrl(bo.getVideoUrl());
            deviceDebugService.addVideoList(appOperationVideoBo);
        }
        // 修改设备logo  每个型号设备走不同协议无法共用同一个上传
//        if(bo.getLogoFile() != null){
//            MultipartFile file = bo.getLogoFile();
//            if(file.getSize()>1024*1024*2){
//                return R.warn("图片不能大于2M");
//            }
//            AppDeviceLogoUploadDto bo1 = new AppDeviceLogoUploadDto();
//            bo1.setDeviceId(bo.getDeviceId());
//            bo1.setDeviceImei(bo.getDeviceImei());
//            bo1.setFile(file);
//            deviceXinghanBizService.uploadDeviceLogo(bo1);
//        }

        return R.ok();
    }

    /* ------------------ 私有复用 ------------------ */

    private void validateDeviceDebugEdit(DeviceDebugEditDto bo) {
        if (bo.getDeviceId() == null || bo.getDeviceId() == 0L) {
            throw new ServiceException("请选择设备");
        }
//        if (bo.getDeviceImei().isEmpty()) {
//            throw new ServiceException("设备 IMEI 不能为空");
//        }
    }

}
