package com.xueyi.system.meeting.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.core.constant.basic.SecurityConstants;
import com.xueyi.common.core.web.result.R;
import com.xueyi.common.sms.service.SmsService;
import com.xueyi.system.api.device.domain.vo.DeviceTenantSourceMergeVo;
import com.xueyi.system.api.digitalmans.domain.po.DmDigitalmanExtPo;
import com.xueyi.system.api.digitalmans.feign.RemoteDigitalmanService;
import com.xueyi.system.api.meeting.domain.dto.DmMeetingOrdersDto;
import com.xueyi.system.api.meeting.domain.dto.DmMeetingRoomsDto;
import com.xueyi.system.api.meeting.feign.RemoteMeetingService;
import com.xueyi.system.api.pass.domain.dto.DmRecognizedRecordsDto;
import com.xueyi.system.api.pass.feign.RemoteRecognizedRecordsService;
import com.xueyi.system.resource.controller.api.BaseApiController;
import com.xueyi.system.utils.common.ImageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 会议室预约管理 API  TEST
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/meeting/api")
public class DmMeetingApiController extends BaseApiController {

    private static final Logger log = LoggerFactory.getLogger(DmMeetingApiController.class);
    @Autowired
    private RemoteMeetingService remoteMeetingService;

    @Autowired
    private RemoteDigitalmanService digitalmanService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping(value = "/room-lists/{devId}")
    public List<DmMeetingRoomsDto> roomList(@PathVariable(value = "devId") String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);


        R<DmDigitalmanExtPo> extR = digitalmanService.selectExtByDeviceId(devId,vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
        DmDigitalmanExtPo extPo = null;
        if (null != extR) {
            extPo = extR.getData();
        }
        if (extPo != null) {
            R<List<DmMeetingRoomsDto>> result = remoteMeetingService.roomListInner(extPo.getDeptId(),vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
            return result.getData();
        } else {
            return new ArrayList<>();
        }

    }


    @ResponseBody
    @PostMapping(value = "/person-lists")
    public JSONObject personList(@RequestParam(required = true) Long orderBy,@RequestParam(required = true) String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.personListInner(orderBy, vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
    }


    @ResponseBody
    @PostMapping(value = "/new-order")
    public JSONObject order(@Valid DmMeetingOrdersDto order, String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.saveInner(order, vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @ResponseBody
    @PostMapping(value = "/quit-order")
    public JSONObject quit(@RequestParam("id") Long id, @RequestParam(required = true) String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.delInner(id, vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @ResponseBody
    @PostMapping(value = "/cancel-order")
    public JSONObject cancel(@RequestParam(required = true) String devId) {
//        redisTemplate.delete("group:websocket" + ":" + devId);
        redisTemplate.delete("group:device" + ":" + devId + ":" +"session");
        log.info("取消预约，删除redis缓存");
        return outputSuccess().toJSON();
    }

    @ResponseBody
    @PostMapping(value = "/lists")
    public JSONObject list(String dateStr, Long spaceId, String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.listsInner(dateStr, spaceId, vo.getTenantId(),vo.getSourceSlave(),SecurityConstants.INNER);
    }

    @ResponseBody
    @GetMapping(value = "/lists/{devId}")
    public JSONObject listAll(@PathVariable(required = true) String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.listsAllInner(vo.getTenantId(),vo.getSourceSlave(),SecurityConstants.INNER);
    }


    @ResponseBody
    @PostMapping(value = "/date-lists")
    public JSONObject lists(String dateStr, String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.listsByDate(dateStr, vo.getTenantId(),vo.getSourceSlave(),SecurityConstants.INNER);
    }


    @ResponseBody
    @PostMapping(value = "/lists-all")
    public JSONObject listAfter(String dateStr, Long spaceId, String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.listAllInner(dateStr, spaceId, vo.getTenantId(),vo.getSourceSlave(),SecurityConstants.INNER);
    }

    @Autowired
    private RemoteRecognizedRecordsService recognizedRecordsService;

    @PostMapping(value = "/upload-pass")
    public JSONObject upload(@RequestBody DmRecognizedRecordsDto recognizedRecordsDto, String devId) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return recognizedRecordsService.uploadPass(recognizedRecordsDto, vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
    }


    @GetMapping(value = "/test")
    public Object test(@RequestParam(required = false) String url){

        return ImageUtil.urlImageToBase64(url);
    }


    @GetMapping(value = "/recent/{devId}/{dateStr}")
    @ResponseBody
    public R<List<JSONObject>> ableOrderList(@PathVariable(value = "devId") String devId, @PathVariable(value = "dateStr") String dateStr, @RequestParam(value = "roomId", required = false) Long roomId, @RequestParam(value = "startTime", required = false) String startTime) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        R<DmDigitalmanExtPo> extR = digitalmanService.selectExtByDeviceId(devId, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
        DmDigitalmanExtPo extPo = null;
        if (null != extR) {
            extPo = extR.getData();
        }
        if (extPo != null) {
            List<JSONObject> result = remoteMeetingService.recent(extPo.getDeptId(), dateStr, roomId, startTime, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
            return R.ok(result);
        } else {
            return R.fail("未找到设备信息");
        }
    }
    @GetMapping(value = "/recent/{devId}")
    @ResponseBody
    public R<List<JSONObject>> ableOrderList(@PathVariable(value = "devId") String devId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        R<DmDigitalmanExtPo> extR = digitalmanService.selectExtByDeviceId(devId,vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
        DmDigitalmanExtPo extPo = null;
        if (null != extR) {
            extPo = extR.getData();
        }
        if (extPo != null) {
            List<JSONObject> result = remoteMeetingService.recent(extPo.getDeptId(), vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
            return R.ok(result);
        } else {
            return R.fail("未找到设备信息");
        }
    }

    @ResponseBody
    @GetMapping(value = "/enableOrder/{devId}")
    public JSONObject queryExist(@PathVariable(value = "devId") String devId, Long roomId, String dateStr, String startTime) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.queryExist(roomId, dateStr, startTime, vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @ResponseBody
    @GetMapping(value = "/enableOrderMeeting/{devId}")
    public JSONObject queryAbleExist(@PathVariable(value = "devId") String devId, Long roomId, String dateStr, String startTime, Integer duration) {
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);

        return remoteMeetingService.queryExistOrder(roomId, dateStr, startTime, duration, vo.getTenantId(),vo.getSourceSlave(), SecurityConstants.INNER);
    }
}
