package cn.hanghui.outapi.platform.tenant.controller;

import cn.hanghui.outapi.platform.common.config.log.Log;
import cn.hanghui.outapi.platform.common.controller.BaseController;
import cn.hanghui.outapi.platform.common.entity.rest.RestCode;
import cn.hanghui.outapi.platform.common.entity.rest.RestResponse;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.utils.AppTokenUtil;
import cn.hanghui.outapi.platform.common.utils.EnumUtils;
import cn.hanghui.outapi.platform.tenant.entity.dto.TenantDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhlocal.HhLocalDeviceDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhlocal.HhLocalDeviceSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhlocal.HhLocalDeviceTemplateDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhlocal.HhLocalDeviceTemplateLessVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhlocal.HhLocalDeviceTemplateSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhlocal.HhLocalDeviceTemplateVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhlocal.HhLocalDeviceVo;
import cn.hanghui.outapi.platform.tenant.entity.enums.*;
import cn.hanghui.outapi.platform.tenant.service.HhLocalDeviceService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.controller
 * @class HhLocalDeviceController
 * @date 2024/9/25 上午9:33
 * @description
 */
@Slf4j
@Api(tags = "HHLocal设备配置")
@RestController
@RequestMapping(value = "/api/v1/hh/local/device")
public class HhLocalDeviceController extends BaseController {

    @Autowired
    private HhLocalDeviceService hhLocalDeviceService;

    @ApiOperation("模版-新增/编辑")
    @PostMapping("template/edit")
    @ApiOperationSupport(order = 100)
    @Log(tag = LogTagEnum.HHLOCAL,title = "添加/编辑模版",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> templateEdit(@RequestBody @Valid HhLocalDeviceTemplateDto dto) {
        return RestResponse.ok(hhLocalDeviceService.templateEdit(dto));
    }

    @ApiOperation("模版列表")
    @GetMapping("template/list")
    @ApiOperationSupport(order = 110)
    public RestResult<List<HhLocalDeviceTemplateLessVo>> templateList() {
        return RestResponse.ok(hhLocalDeviceService.templateList());
    }

    @ApiOperation("模版列表(分页)")
    @PostMapping("template/page")
    @ApiOperationSupport(order = 120)
    public RestResult<IPage<HhLocalDeviceTemplateVo>> templatePage(@RequestBody HhLocalDeviceTemplateSearch dto) {
        return RestResponse.ok(hhLocalDeviceService.templatePage(dto));
    }

    @ApiOperation("模版-删除")
    @GetMapping("template/{deviceTemplateId}")
    @ApiOperationSupport(order = 130)
    @Log(tag = LogTagEnum.HHLOCAL,title = "删除模版",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> templateDelete(@PathVariable(value = "deviceTemplateId") Long deviceTemplateId) {
        return RestResponse.ok(hhLocalDeviceService.templateDelete(deviceTemplateId));
    }

    @ApiOperation("模版-模版详情")
    @GetMapping("template/detail")
    @ApiOperationSupport(order = 140)
    public RestResult<HhLocalDeviceTemplateVo> templateDetail(@RequestParam(value = "deviceTemplateId") Long deviceTemplateId) {
        return RestResponse.ok(hhLocalDeviceService.templateDetail(deviceTemplateId));
    }

    @ApiOperation(value = "设备配置-添加/编辑")
    @PostMapping("/add")
    @ApiOperationSupport(order = 150)
    @Log(tag = LogTagEnum.HHLOCAL,title = "添加/编辑设备",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> add(@RequestBody HhLocalDeviceDto dto) {
        return hhLocalDeviceService.add(dto);
    }

    @ApiOperation(value = "设备列表（分页）")
    @PostMapping("/page")
    @ApiOperationSupport(order = 160)
    public RestResult<IPage<HhLocalDeviceVo>> page(@RequestBody HhLocalDeviceSearch dto) {
        return hhLocalDeviceService.page(dto);
    }

    @ApiOperation(value = "获取设备编号列表")
    @PostMapping("/sn/list")
    public RestResult<List<String>> snlist() {
        return hhLocalDeviceService.snlist();
    }

    @ApiOperation(value = "设备删除")
    @PostMapping("/del/{deviceId}")
    @ApiOperationSupport(order = 170)
    @Log(tag = LogTagEnum.HHLOCAL,title = "删除设备",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> del(@PathVariable(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return hhLocalDeviceService.del(deviceId);
    }

    @ApiOperation(value = "设备授权")
    @GetMapping("/enable")
    @ApiOperationSupport(order = 180)
    @Log(tag = LogTagEnum.HHLOCAL,title = "设备授权",businessTypeEnum = BusinessTypeEnum.AUTH)
    public RestResult<Boolean> enable(@RequestParam(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return hhLocalDeviceService.opeDeviceAuth(deviceId, Boolean.TRUE);
    }

    @ApiOperation(value = "设备取消授权")
    @GetMapping("/disable")
    @ApiOperationSupport(order = 190)
    @Log(tag = LogTagEnum.HHLOCAL,title = "设备取消授权",businessTypeEnum = BusinessTypeEnum.CANCEL_AUTH)
    public RestResult<Boolean> disable(@RequestParam(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return hhLocalDeviceService.opeDeviceAuth(deviceId, Boolean.FALSE);
    }

    @ApiOperation(value = "HHLocal设备-获取配置信息")
    @PostMapping("/sn/config")
    @ApiOperationSupport(order = 200)
    public RestResult<List<HhLocalDeviceVo>> snConfig(@RequestBody TenantDto dto) {
        log.info("--->HHLocal设备-获取配置信息:{}", JSONObject.toJSONString(dto));
        //HHCulture设备
        List<String> tokens = AppTokenUtil.getTokens();
        if (!tokens.contains(dto.getToken().trim())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        return hhLocalDeviceService.snConfig(dto);
    }

    @ApiOperation(value = "获取手动输入内容项")
    @GetMapping("/getManualInParamsEnum")
    public RestResult<List<Map<String, String>>> getManualInParamsEnum() {
        return RestResult.ok(EnumUtils.enumToListMap( ManualInParamsEnum.class));
    }

    @ApiOperation(value = "获取摄像头类型")
    @GetMapping("/getCameraTypeEnum")
    public RestResult<List<Map<String, String>>> getCameraTypeEnum() {
        return RestResult.ok(EnumUtils.enumToListMap(CameraTypeEnum.class));
    }
    @ApiOperation(value = "获取活体检测模式")
    @GetMapping("/getLiveTypeEnum")
    public RestResult<List<Map<String, String>>> getLiveTypeEnum() {
        return RestResult.ok(EnumUtils.enumToListMap(LiveTypeEnum.class));
    }
    @ApiOperation(value = "获取成功显示人员信息列表")
    @GetMapping("/getSuccessShowPersonalInfoListEnum")
    public RestResult<List<Map<String, String>>> getSuccessShowPersonalInfoListEnum() {
        return RestResult.ok(EnumUtils.enumToListMap(SuccessShowPersonalInfoListEnum.class));
    }

    @ApiOperation(value = "设置——人员列表")
    @GetMapping("/getSetPersonalListEnum")
    public RestResult<List<Map<String, String>>> getSetPersonalListEnum() {
        return RestResult.ok(EnumUtils.enumToListMap(SetPersonalListEnum.class));
    }

}
