package com.sptg.gateway.controller.auth;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.UserFlagConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.entity.VideoCallAccount;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.estate.api.feigns.VideoCallAccountFeign;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * title: DeviceManageController
 * description:
 *
 *                  
 *            
 */
@RestController
@RequiredArgsConstructor
public class DeviceManageController {

    private final SecurityServiceInterface<DeviceAdminUser> securityServiceInterface;
    private final OrgFeign orgFeign;
    private final DeviceFeign deviceFeign;
    private final VideoCallAccountFeign videoCallAccountFeign;
    private final ResidentialFeign residentialFeign;

    /**
     * 分页查询设备列表
     * @param pageBean 查询条件
     * @return ResultBean<Page>
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/device/manage/pagingQuery")
    public ResultBean<Page<Device>> pagingQuery(@RequestBody PageBean<Device, DeviceDto> pageBean){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        Organization userOrg = this.orgFeign.getOrgById(onLineUser.getOrgId());
        DeviceDto dto = pageBean.getDto();
        dto = null == dto ? new DeviceDto() : dto;
        dto.setXqId(onLineUser.getXqId());
        if (userOrg.getOrgType() == OperationConstant.THREE){
            //物业
            dto.setEstatePosition(onLineUser.getOrgPosition());
        }else{
            //机构
            dto.setOrgPosition(onLineUser.getOrgPosition());
        }
        return this.deviceFeign.pagingQuery(pageBean);
    }

    /**
     * 设备一键开门
     * @param device 设备信息
     * @return ResultBean
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/device/manage/openDoor")
    public ResultBean openDoor(@RequestBody Device device){
        Assert.notNull(device.getId(),"设备Id不能为空");
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        device.setOrgPosition(onLineUser.getOrgPosition());
        device.setUserId(UserFlagConstant.DEVICE_MANAGE_FLAG + onLineUser.getId());
        return this.deviceFeign.openDoor(device);
    }

    /**
     * 设备一键重启
     * @param device 设备信息
     * @return ResultBean
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/device/manage/restart")
    public ResultBean restart(@RequestBody Device device){
        Assert.notNull(device.getId(),"设备Id不能为空");
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        device.setOrgPosition(onLineUser.getOrgPosition());
        return this.deviceFeign.restart(device);
    }

    /**
     * 获取设备的视频对讲账号Id
     * @param account 设备信息
     * @return ResultBean
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/device/manage/getCallAccountByDevice")
    public ResultBean getCallAccountByDevice(@RequestBody VideoCallAccount account){
        Assert.hasText(account.getSerialNumber(),"设备序列号不能为空");
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        account.setXqId(onLineUser.getXqId());
        return this.videoCallAccountFeign.getCallAccountIdBySerialNumber(account);
    }


    /**
     * 校验紧急疏散状态
     * @return ResultBean<Boolean>
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/device/checkEvacuateStatus")
    public ResultBean<Boolean> checkEvacuateStatus(){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        //  获取小区信息
        Residential residential = this.residentialFeign.findResidentialById(onLineUser.getXqId());
        Assert.notNull(residential,"服务器异常");
        //  true:已开启紧急疏散状态,false：未开启紧急疏散状态
        boolean result = false;
        if (null != residential.getOpenEvacuateTime()){
            long minTime = System.currentTimeMillis() - 1000 * 60 * 60 * 3;
            result = minTime < residential.getOpenEvacuateTime();
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    /**
     * 开启或关闭紧急疏散状态
     * @param map ：1：开启，2：关闭
     * @return ResultBean<Boolean>
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/device/startOrClearEvacuate")
    public ResultBean<Boolean> startOrClearEvacuate(@RequestBody Map<String,Integer> map){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        Integer evacuateStatus = map.get("evacuateStatus");
        Assert.notNull(evacuateStatus,"紧急疏散状态不为空");
        if (1 == evacuateStatus){
            return this.deviceFeign.startOrClearEvacuate(onLineUser.getXqId(), System.currentTimeMillis());
        }else {
            return this.deviceFeign.startOrClearEvacuate(onLineUser.getXqId(),0L);
        }
    }
}
