package cn.reghao.tnb.file.app.zdisk.controller;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.file.app.zdisk.model.dto.CamDeviceDto;
import cn.reghao.tnb.file.app.zdisk.model.dto.CamRecordDto;
import cn.reghao.tnb.file.app.zdisk.model.po.CamDevice;
import cn.reghao.tnb.file.app.zdisk.model.vo.CamDetail;
import cn.reghao.tnb.file.app.zdisk.model.vo.CamRecordDetail;
import cn.reghao.tnb.file.app.zdisk.model.vo.CamRecordInfo;
import cn.reghao.tnb.file.app.zdisk.model.vo.CamSearch;
import cn.reghao.tnb.file.app.zdisk.service.CamRecordService;
import cn.reghao.tnb.file.app.zdisk.service.CamService;
import cn.reghao.tnb.common.db.SelectOption;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author reghao
 * @date 2024-08-27 19:50:05
 */
@Tag(name = "摄像头接口")
@RestController
@RequestMapping("/api/disk/cam")
@AuthUser
public class CamController {
    private final CamService camService;
    private final CamRecordService camRecordService;

    public CamController(CamService camService, CamRecordService camRecordService) {
        this.camService = camService;
        this.camRecordService = camRecordService;
    }

    @Operation(summary = "添加摄像头", description = "N")
    @PostMapping(value = "/device", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addCamDevice(@RequestBody @Validated CamDeviceDto camDeviceDto) {
        camService.addCamDevice(camDeviceDto);
        return WebResult.success();
    }

    @Operation(summary = "添加录像文件", description = "N")
    @PostMapping(value = "/record", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addCamRecord(@Validated @RequestBody CamRecordDto camRecordDto) {
        Result result = camRecordService.addCamRecord(camRecordDto);
        return WebResult.result(result);
    }

    @Operation(summary = "摄像头列表", description = "N")
    @GetMapping("/list")
    public String getCamList() {
        List<CamDevice> kvs = camService.getCamList();
        return WebResult.success(kvs);
    }

    @Operation(summary = "摄像头详情", description = "N")
    @GetMapping("/detail")
    public String getCamDetail(@Validated CamSearch camSearch) {
        String currentDayStr = LocalDate.now().toString();
        String yearMonthDay = camSearch.getYearMonthDay();
        if (yearMonthDay != null) {
            currentDayStr = yearMonthDay;
        }

        Long camId = camSearch.getCamId();
        CamDevice camDevice = camService.getCamDevice(camId);
        List<CamRecordInfo> dayRecords = camRecordService.getCamRecordByDay(camId, currentDayStr);
        CamDetail camDetail = new CamDetail(camDevice, yearMonthDay, dayRecords);
        return WebResult.success(camDetail);
    }

    @Operation(summary = "某个月中哪些天有录像", description = "N")
    @GetMapping(value = "/record/month", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCamRecordByMonth(@Validated CamSearch camSearch) {
        List<String> list = camRecordService.getDaysInMonth(camSearch.getCamId(), camSearch.getYearMonth());
        return WebResult.success(list);
    }

    @Operation(summary = "获取某个录像的 URL", description = "N")
    @GetMapping(value = "/record/url/{recordId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCamRecord(@PathVariable("recordId") long recordId) {
        CamRecordDetail camRecord = camRecordService.getCamRecordDetail(recordId);
        if (camRecord != null) {
            return WebResult.success(camRecord);
        }

        return WebResult.fail();
    }

    @Operation(summary = "添加活动接口", description = "N")
    @PostMapping(value = "/activity", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addActivity() {
        camService.addActivity();
        return WebResult.success();
    }
}
