package net.northking.cctp.device.pub;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.device.api.ctrl.service.CdDeviceCtrlApiService;
import net.northking.cctp.device.api.engineInfo.service.CdEngineInfoApiService;
import net.northking.cctp.device.api.mobile.service.CdMobileDeviceApiService;
import net.northking.cctp.device.db.entity.CdDeviceCtrl;
import net.northking.cctp.device.dto.device.CdDeviceRegisterDto;
import net.northking.cctp.device.dto.engineInfo.CdEngineInfoRegisterDto;
import net.northking.cctp.device.dto.engineInfo.CdEngineInfoResultDto;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;

@Api(tags = "注册登记（pub）")
@RequestMapping("/pub")
@RestController
public class RegisterPubCtrl {

    private final static Logger logger = LoggerFactory.getLogger(RegisterPubCtrl.class);

    @Autowired
    private CdEngineInfoApiService cdEngineInfoApiService;

    @Autowired
    private CdDeviceCtrlApiService cdDeviceCtrlApiService;

    @Autowired
    private CdMobileDeviceApiService cdMobileDeviceApiService;

    @Autowired
    private RedisLockRegistry lockRegistry;

    @ApiOperation(value = "注册登记(引擎和PC)")
    @PostMapping(value = "/engine/register",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CdEngineInfoResultDto register(
            @RequestBody CdEngineInfoRegisterDto registerDto){
        //redis锁:pc设备
        CdEngineInfoResultDto resultDto = this.cdEngineInfoApiService.register(registerDto);
        return resultDto;
    }

    @ApiOperation(value = "注册登记(上位机)")
    @PostMapping(value = "/ctrls/register",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> registerCtrl(@RequestBody CdDeviceCtrl ctrl){
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String result = this.cdDeviceCtrlApiService.registerCtrl(ctrl);
        return wrapper.success(result);
    }

    @ApiOperation(value = "注册登记(移动端)")
    @PostMapping(value = "/device/register/mobile",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> registerMobile(
            @RequestBody CdDeviceRegisterDto device) {
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        Lock lock = lockRegistry.obtain("mobile:device:" + device.getDeviceInfo().get("serial").toString());
        String result = new String();
        if (lock.tryLock()) {
            try {
                result = this.cdMobileDeviceApiService.registerMobile(device);
            } catch (PlatformRuntimeException e) {
                throw e;
            } catch (Exception e) {
                logger.info("获取锁异常：", e);
            } finally {
                lock.unlock();
            }
        }
        return wrapper.success(result);
    }


}
