package net.northking.cctp.device.pub;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.device.api.activity.service.DeviceActivityApiService;
import net.northking.cctp.device.api.mobile.service.CdMobileDeviceApiService;
import net.northking.cctp.device.api.pcDevice.service.CdPcDeviceApiService;
import net.northking.cctp.device.db.entity.CdDeviceStatus;
import net.northking.cctp.device.db.entity.CdDeviceToken;
import net.northking.cctp.device.db.service.CdDeviceStatusService;
import net.northking.cctp.device.db.service.CdDeviceTokenService;
import net.northking.cctp.device.dto.device.CdMobileDeviceExitDto;
import net.northking.cctp.device.dto.device.DeviceInfoDto;
import net.northking.cctp.device.dto.device.DeviceLockDto;
import net.northking.cctp.device.dto.pcDevice.CdDeviceRemoteDto;
import net.northking.cctp.device.dto.pcDevice.CdPcDeviceExitDto;
import net.northking.cctp.device.dto.pcDevice.CdPcDeviceRemoteDto;
import net.northking.cctp.device.util.DeviceStatusUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "设备信息（pub）")
@RequestMapping("/pub")
@RestController
public class DevicePubCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(DevicePubCtrl.class);

    @Autowired
    private DeviceActivityApiService apiService;

    @Autowired
    private CdDeviceStatusService cdDeviceStatusService;

    @Autowired
    private CdDeviceTokenService deviceTokenService;

    @Autowired
    private CdPcDeviceApiService cdPcDeviceApiService;

    @Autowired
    private CdMobileDeviceApiService mobileApiService;

    @Autowired
    private RedisLockRegistry redisLockRegistry;

    @Autowired
    private DeviceStatusUtil statusUtil;

    @ApiOperation(value = "根据设备id查询设备信息(pub)")
    @GetMapping(value = "/device/lock/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<DeviceInfoDto> getLock(@PathVariable("uuid") String uuid) {
        ResultWrapper<DeviceInfoDto> wrapper = new ResultWrapper<>();
        DeviceInfoDto result = this.apiService.pubGetInfo(uuid);
        return wrapper.success(result);
    }

    @ApiOperation(value = "根据设备id离线设备(pub)")
    @GetMapping(value = "/device/off-line/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Integer offLineDevice(@PathVariable("uuid") String uuid){
        CdDeviceStatus status = this.cdDeviceStatusService.findByPrimaryKey(uuid);
        if (status!= null){
            CdDeviceStatus cdDeviceStatus = new CdDeviceStatus();
            //PC设备离线为不可用
            if (status.getDeviceType().equals("2")){
                cdDeviceStatus.setUsageStatus("3");
            }
            cdDeviceStatus.setDeviceStatus("2");
            cdDeviceStatus.setId(uuid);
            statusUtil.updatePcStatus(cdDeviceStatus);
        }
        return 1;
    }

    @ApiOperation(value = "设备锁校验")
    @PostMapping(value = "/device/lock/check",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Boolean> checkLock(@RequestBody DeviceLockDto dto){
        ResultWrapper<Boolean> wrapper = new ResultWrapper<>();
        Boolean result = this.apiService.checkToken(dto);
        return wrapper.success(result);

    }

    @ApiOperation(value = "根据设备锁获取设备信息")
    @GetMapping(value = "/device/info/{token}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<DeviceInfoDto> getInfoFromToken(@PathVariable("token") String token) {
        ResultWrapper<DeviceInfoDto> wrapper = new ResultWrapper<>();
        DeviceInfoDto result = this.apiService.getInfoFromToken(token);
        return wrapper.success(result);
    }

    @ApiOperation(value = "释放设备")
    @GetMapping(value = "/device/release/{token}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Boolean> deviceRelease(@PathVariable("token") String token) {
        ResultWrapper<Boolean> wrapper = new ResultWrapper<>();
        Boolean result = this.apiService.deviceRelease(token);
        return wrapper.success(result);
    }


    @ApiOperation(value = "申请使用PC设备")
    @GetMapping(value = "/pc/device/remote",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String pcDeviceRemote(@RequestParam("id") String id,@RequestParam("deviceId")String deviceId,HttpServletRequest request) {
        CdDeviceRemoteDto dto = new CdDeviceRemoteDto();
        dto.setId(id);
        dto.setDeviceId(deviceId);
        dto.setUserBy("1");
        CdPcDeviceRemoteDto device = this.cdPcDeviceApiService.achieve(dto, request);
        return device.getToken();
    }

    @ApiOperation(value = "批量申请设备")
    @PostMapping(value = "/deviceList/apply/remote")
    public ResultWrapper<List<Map<String,String>>> applyDevice(@RequestBody Map<String,Object> deviceList, HttpServletRequest request){
        logger.info("批量申请设备(pub)------------------------start");
        ResultWrapper<List<Map<String,String>>> resultWrapper = new ResultWrapper<>();
        List<Map<String, String>> mapList = apiService.deviceListApply(deviceList,request);
        logger.info("批量申请设备(pub)------------------------end");
        return resultWrapper.success(mapList);
    }

    @ApiOperation(value = "批量释放设备")
    @PostMapping(value = "/deviceList/release")
    public void releaseDevice(@RequestBody Map<String,Object> deviceList){
        logger.info("批量释放设备(pub)------------------------------start");
        apiService.releaseDeviceList(deviceList);
        logger.info("批量释放设备(pub)------------------------------end");
    }

    @ApiOperation(value = "查询离线设备")
    @PostMapping(value = "/device/check/status")
    public List<Map<String,Object>> queryDeviceStatus(@RequestBody List<String> deviceIds){
        logger.info("查询离线设备(pub)------------------------------start,设备id为:{}",deviceIds.toString());
        List<Map<String, Object>> mapList = apiService.checkDeviceStatus(deviceIds);
        logger.info("查询离线设备(pub)------------------------------end");
        return mapList;
    }

    @ApiOperation(value = "根据移动设备id查询设备类型")
    @PostMapping(value = "/query/deviceTypeByIds")
    public ResultWrapper<Map<String,List<String>>> queryDeviceType(@RequestBody List<String> deviceIds){
        ResultWrapper<Map<String,List<String>>> wrapper = new ResultWrapper<>();
        Map<String, List<String>> result = apiService.queryDeviceType(deviceIds);
        return wrapper.success(result);
    }
}
