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

import cn.hanghui.outapi.platform.common.config.log.Log;
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.tenant.entity.dto.proof.ProvisionalIdentityProofDeviceDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofDeviceMiniDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofDeviceSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofDeviceUpdateMiniDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofDeviceVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofNoticeDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofNoticeLessVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofNoticeSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofNoticeVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofTemplateDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofTemplateLessVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofTemplateSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.proof.ProvisionalIdentityProofTemplateVo;
import cn.hanghui.outapi.platform.tenant.entity.enums.BusinessTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.LogTagEnum;
import cn.hanghui.outapi.platform.tenant.service.ProvisionalProofService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.controller
 * @class ProvisionalProofController
 * @date 2024/5/13 上午10:59
 * @description 临证相关（临时身份证明）
 */
@Slf4j
@Api(tags = "临证相关(临时身份证明)")
@Validated
@RestController
@RequestMapping(value = "/api/v1/provisional/proof")
public class ProvisionalProofController {

    @Autowired
    private ProvisionalProofService provisionalProofService;

    //临证须知
    @ApiOperation("须知-新增/编辑")
    @PostMapping("/notice/add")
    @ApiOperationSupport(order = 100)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "新增/编辑小程序授权配置",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> noticeEdit(@RequestBody @Valid ProvisionalIdentityProofNoticeDto dto) {
        return RestResponse.ok(provisionalProofService.noticeEdit(dto));
    }

    @ApiOperation("须知列表")
    @GetMapping("/notice/list")
    @ApiOperationSupport(order = 101)
    public RestResult<List<ProvisionalIdentityProofNoticeLessVo>> noticeList() {
        return RestResponse.ok(provisionalProofService.noticeList());
    }

    @ApiOperation("须知列表(分页)")
    @PostMapping("/notice/page")
    @ApiOperationSupport(order = 102)
    public RestResult<IPage<ProvisionalIdentityProofNoticeVo>> noticePage(@RequestBody ProvisionalIdentityProofNoticeSearch dto) {
        return RestResponse.ok(provisionalProofService.noticePage(dto));
    }

    @ApiOperation("须知-删除")
    @DeleteMapping("/notice/{noticeId}")
    @ApiOperationSupport(order = 103)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "删除小程序授权配置",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> noticeDelete(@PathVariable(value = "noticeId") Long noticeId) {
        return RestResponse.ok(provisionalProofService.noticeDelete(noticeId));
    }

    @ApiOperation("须知-详情")
    @GetMapping("/notice/detail")
    @ApiOperationSupport(order = 104)
    public RestResult<ProvisionalIdentityProofNoticeVo> noticeDetail(@RequestParam(value = "noticeId") Long noticeId) {
        return RestResponse.ok(provisionalProofService.noticeDetail(noticeId));
    }

    //模版相关
    @ApiOperation("模版-新增编辑")
    @PostMapping("/template/add")
    @ApiOperationSupport(order = 200)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "新增/编辑模版",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> templateEdit(@RequestBody @Valid ProvisionalIdentityProofTemplateDto dto) {
        return RestResponse.ok(provisionalProofService.templateEdit(dto));
    }

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

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

    @ApiOperation("模版-删除")
    @DeleteMapping("/template/{templateId}")
    @ApiOperationSupport(order = 203)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "删除模版",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> templateDelete(@PathVariable(value = "templateId") Long templateId) {
        return RestResponse.ok(provisionalProofService.templateDelete(templateId));
    }

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

    //设备相关
    @ApiOperation(value = "临证设备配置-添加/编辑")
    @PostMapping("/device/add")
    @ApiOperationSupport(order = 300)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "新增/编辑设备",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> deviceAdd(@RequestBody ProvisionalIdentityProofDeviceDto dto) {
        return provisionalProofService.deviceAdd(dto);
    }

    @ApiOperation(value = "临证设备列表（分页）")
    @PostMapping("/device/page")
    @ApiOperationSupport(order = 301)
    public RestResult<IPage<ProvisionalIdentityProofDeviceVo>> devicePage(@RequestBody ProvisionalIdentityProofDeviceSearch dto) {
        return provisionalProofService.devicePage(dto);
    }

    @ApiOperation(value = "临证设备删除")
    @DeleteMapping("/device/del/{deviceId}")
    @ApiOperationSupport(order = 302)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "删除设备",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> deviceDel(@PathVariable(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return provisionalProofService.deviceDel(deviceId);
    }

    @ApiOperation(value = "临证设备授权")
    @GetMapping("/device/enable")
    @ApiOperationSupport(order = 303)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "临证设备授权",businessTypeEnum = BusinessTypeEnum.AUTH)
    public RestResult<Boolean> deviceEnable(@RequestParam(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return provisionalProofService.deviceEnable(deviceId);
    }

    @ApiOperation(value = "临证设备取消授权")
    @GetMapping("/device/disable")
    @ApiOperationSupport(order = 304)
    @Log(tag = LogTagEnum.PROVISIONAL,title = "临证设备授权",businessTypeEnum = BusinessTypeEnum.CANCEL_AUTH)
    public RestResult<Boolean> deviceDisable(@RequestParam(value = "deviceId") @ApiParam(value = "deviceId", required = true) Long deviceId) {
        return provisionalProofService.deviceDisable(deviceId);
    }

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

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

    @ApiOperation(value = "临证设备-获取配置信息(集合)")
    @PostMapping("/sns/configs")
    @ApiOperationSupport(order = 307)
    public RestResult<List<ProvisionalIdentityProofDeviceVo>> snsConfigsForProof(@RequestBody ProvisionalIdentityProofDeviceMiniDto dto) {
        log.info("--->临证设备-获取配置信息(集合):{}", JSONObject.toJSONString(dto));
        //临证设备
        List<String> tokens = AppTokenUtil.getTokens();
        if (!tokens.contains(dto.getToken().trim())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        return RestResult.ok(provisionalProofService.snsConfigsForProof(dto));
    }

}
