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.TenantVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceTemplateDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceTemplateLessVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceTemplateSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceTemplateVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceUpdateMiniDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.hhculture.HhCultureDeviceVo;
import cn.hanghui.outapi.platform.tenant.entity.enums.BusinessTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.LogTagEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.ManualInParamsEnum;
import cn.hanghui.outapi.platform.tenant.service.HHCultureDeviceService;
import cn.hutool.core.util.StrUtil;
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 HHCultureDeviceController
 * @date 2024/3/5 下午2:11
 * @description
 */
@Slf4j
@Api(tags = "HHCulture设备配置")
@RestController
@RequestMapping(value = "/api/v1/hh/culture/device")
public class HHCultureDeviceController extends BaseController {

    @Autowired
    private HHCultureDeviceService hhCultureDeviceService;

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

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

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

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

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

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

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

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

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

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

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

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

    @ApiOperation(value = "HHCulture设备-修改配置信息")
    @PostMapping("/sn/updateConfig")
    @ApiOperationSupport(order = 210)
    public RestResult<Boolean> updateConfig(@RequestBody HhCultureDeviceUpdateMiniDto dto) {
        log.info("--->HHCulture设备-修改配置信息:{}", JSONObject.toJSONString(dto));
        //HHCulture设备
        List<String> tokens = AppTokenUtil.getTokens();
        if (StrUtil.isBlank(dto.getToken()) || !tokens.contains(dto.getToken())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        return hhCultureDeviceService.updateConfig(dto);
    }

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

}
