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.config.ampe.AmpeDto;
import cn.hanghui.outapi.platform.tenant.entity.config.ampe.AmpeSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.ampe.AmpeVo;
import cn.hanghui.outapi.platform.tenant.entity.config.iot.template.IotDeviceConfigTemplateDto;
import cn.hanghui.outapi.platform.tenant.entity.config.iot.template.IotDeviceConfigTemplateLessVo;
import cn.hanghui.outapi.platform.tenant.entity.config.iot.template.IotDeviceConfigTemplateSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.iot.template.IotDeviceConfigTemplateVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.*;
import cn.hanghui.outapi.platform.tenant.entity.enums.*;
import cn.hanghui.outapi.platform.tenant.service.AmpeService;
import cn.hanghui.outapi.platform.tenant.service.IotDeviceConfigService;
import cn.hanghui.outapi.platform.tenant.service.IotDeviceConfigTemplateService;
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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.controller
 * @class IotDeviceConfig
 * @date 2023/12/5 下午1:25
 * @description
 */
@Slf4j
@Api(tags = "HHArome配置")
@RestController
@RequestMapping(value = "/api/v1/iot/device/config")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IotDeviceConfigController extends BaseController {

    private final IotDeviceConfigService iotDeviceConfigService;
    private final AmpeService ampeService;
    private final IotDeviceConfigTemplateService iotDeviceConfigTemplateService;

    @ApiOperation("iot模版-新增/编辑")
    @PostMapping("template/edit")
    @ApiOperationSupport(order = 100)
    @Log(tag = LogTagEnum.HHAROME,title = "添加/编辑模版",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> edit(@RequestBody @Valid IotDeviceConfigTemplateDto dto) {
        return RestResponse.ok(iotDeviceConfigTemplateService.edit(dto));
    }
    @ApiOperation("iot模版列表")
    @GetMapping("template/list")
    @ApiOperationSupport(order = 110)
    public RestResult<List<IotDeviceConfigTemplateLessVo>> iotDeviceConfigTemplateList() {
        return RestResponse.ok(iotDeviceConfigTemplateService.iotDeviceConfigTemplateList());
    }
    @ApiOperation("iot模版列表(分页)")
    @PostMapping("template/page")
    @ApiOperationSupport(order = 120)
    public RestResult<IPage<IotDeviceConfigTemplateVo>> iotDeviceConfigTemplatePage(@RequestBody @Valid  IotDeviceConfigTemplateSearch dto) {
        return RestResponse.ok(iotDeviceConfigTemplateService.iotDeviceConfigTemplatePage(dto));
    }
    @ApiOperation("iot模版-删除")
    @DeleteMapping("template/{iotDeviceConfigTemplateId}")
    @ApiOperationSupport(order = 130)
    @Log(tag = LogTagEnum.HHAROME,title = "删除模版",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> templateDelete(@PathVariable(value = "iotDeviceConfigTemplateId") Long iotDeviceConfigTemplateId) {
        return RestResponse.ok(iotDeviceConfigTemplateService.iotDeviceConfigTemplateDelete(iotDeviceConfigTemplateId));
    }
    @ApiOperation("iot模版-模版详情")
    @GetMapping("template/detail")
    @ApiOperationSupport(order = 140)
    public RestResult<IotDeviceConfigTemplateVo> templateDetail(@RequestParam(value = "iotDeviceConfigTemplateId") Long iotDeviceConfigTemplateId) {
        return RestResponse.ok(iotDeviceConfigTemplateService.templateDetail(iotDeviceConfigTemplateId));
    }



    @ApiOperation("ampe-新增/编辑")
    @PostMapping("ampe/edit")
    @ApiOperationSupport(order = 200)
    @Log(tag = LogTagEnum.HHAROME,title = "添加/编辑ampe",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> edit(@RequestBody @Valid AmpeDto dto) {
        return RestResponse.ok(ampeService.edit(dto));
    }
    @ApiOperation("ampe列表")
    @GetMapping("ampe/list")
    @ApiOperationSupport(order = 210)
    public RestResult<List<AmpeVo>> ampeList() {
        return RestResponse.ok(ampeService.ampeList());
    }
    @ApiOperation("ampe列表(分页)")
    @PostMapping("ampe/page")
    @ApiOperationSupport(order = 220)
    public RestResult<IPage<AmpeVo>> ampePage(@RequestBody AmpeSearch dto) {
        return RestResponse.ok(ampeService.ampePage(dto));
    }
    @ApiOperation("ampe-删除")
    @DeleteMapping("ampe/{ampeId}")
    @ApiOperationSupport(order = 230)
    @Log(tag = LogTagEnum.HHAROME,title = "删除ampe",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> ampeDelete(@PathVariable(value = "ampeId") Long ampeId) {
        return RestResponse.ok(ampeService.ampeDelete(ampeId));
    }


    @ApiOperation(value = "HHArome打印机接口配置列表")
    @GetMapping("/printerConfigList")
    @ApiOperationSupport(order = 299)
    public RestResult<List<Map<Integer, String>>> printerConfigList() {
        return RestResult.ok(EnumUtils.enumToListMap(PrinterConfigEnum.class));
    }
    @ApiOperation(value = "HHArome麦克风类型列表")
    @GetMapping("/microphoneTypeList")
    @ApiOperationSupport(order = 299)
    public RestResult<List<Map<Integer, String>>> microphoneTypeList() {
        return RestResult.ok(EnumUtils.enumToListMap(MicrophoneTypeEnum.class));
    }
    @ApiOperation(value = "容器配置-添加/编辑")
    @PostMapping("/add")
    @ApiOperationSupport(order = 300)
    @Log(tag = LogTagEnum.HHAROME,title = "添加/编辑设备",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> add(@RequestBody IotDeviceConfigDto dto) {
        return iotDeviceConfigService.add(dto);
    }
    @ApiOperation(value = "容器配置列表（分页）")
    @PostMapping("/page")
    @ApiOperationSupport(order = 310)
    public RestResult<IPage<IotDeviceConfigVo>> pageTask(@RequestBody IotDeviceConfigPageDto dto) {
        return iotDeviceConfigService.page(dto);
    }
    @ApiOperation(value = "容器配置删除")
    @PostMapping("/del/{iotDeviceConfigId}")
    @ApiOperationSupport(order = 320)
    @Log(tag = LogTagEnum.HHAROME,title = "删除设备",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> del(@PathVariable(value = "iotDeviceConfigId") @ApiParam(value = "iotDeviceConfigId", required = true) Long iotDeviceConfigId) {
        return iotDeviceConfigService.del(iotDeviceConfigId);
    }
    @ApiOperation(value = "设备授权")
    @GetMapping("/empower")
    @ApiOperationSupport(order = 330)
    @Log(tag = LogTagEnum.HHAROME,title = "设备授权",businessTypeEnum = BusinessTypeEnum.AUTH)
    public RestResult<Boolean> empower(@RequestParam(value = "iotDeviceConfigId") @ApiParam(value = "iotDeviceConfigId", required = true) Long iotDeviceConfigId) {
        return iotDeviceConfigService.empower(iotDeviceConfigId);
    }
    @ApiOperation(value = "设备取消授权")
    @GetMapping("/cancelEmpower")
    @ApiOperationSupport(order = 340)
    @Log(tag = LogTagEnum.HHAROME,title = "设备取消授权",businessTypeEnum = BusinessTypeEnum.CANCEL_AUTH)
    public RestResult<Boolean> cancelEmpower(@RequestParam(value = "iotDeviceConfigId") @ApiParam(value = "iotDeviceConfigId", required = true) Long iotDeviceConfigId) {
        return iotDeviceConfigService.cancelEmpower(iotDeviceConfigId);
    }


    @ApiOperation(value = "设备-获取配置信息")
    @PostMapping("/sn/config")
    @ApiOperationSupport(order = 1000)
    public RestResult<IotDeviceAppletConfigVo> snConfig(@RequestBody IotDeviceAppletConfigDto dto) {
        log.info("--->设备-获取容器配置信息:{}", JSONObject.toJSONString(dto));
        //ampe容器管理
        List<String> tokens = AppTokenUtil.getTokens();
        if (!tokens.contains(dto.getToken().trim())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        return RestResult.ok(iotDeviceConfigService.snConfig(dto));
    }
    @ApiOperation(value = "设备-修改配置信息")
    @PostMapping("/sn/updateConfig")
    @ApiOperationSupport(order = 1010)
    public RestResult<Boolean> updateConfig(@RequestBody IotDeviceAppletUpdateConfigDto dto) {
        log.info("--->设备-容器修改配置信息:{}", JSONObject.toJSONString(dto));
        //ampe容器管理
        List<String> tokens = AppTokenUtil.getTokens();
        if (StrUtil.isBlank(dto.getToken()) || !tokens.contains(dto.getToken())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        return RestResult.ok(iotDeviceConfigService.updateConfig(dto));
    }

}
