package cn.skcks.docking.gb28181.wvp.service.device.control;

import cn.skcks.docking.gb28181.common.json.JsonException;
import cn.skcks.docking.gb28181.common.xml.XmlUtils;
import cn.skcks.docking.gb28181.core.sip.utils.SipUtil;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDocking;
import cn.skcks.docking.gb28181.wvp.service.device.DeviceService;
import cn.skcks.docking.gb28181.wvp.service.docking.DockingService;
import cn.skcks.docking.gb28181.wvp.sip.message.message.device.control.DeviceControlDTO;
import cn.skcks.docking.gb28181.wvp.sip.request.SipRequestBuilder;
import cn.skcks.docking.gb28181.wvp.sip.sender.SipSender;
import cn.skcks.docking.gb28181.wvp.sip.subscribe.SipSubscribe;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceControlService {
    private final SipSender sipSender;
    private final SipSubscribe sipSubscribe;
    private final DockingService dockingService;
    private final DeviceService deviceService;

    @SneakyThrows
    public void sendRecordControl(String deviceCode, String recordCmd){
        Optional<WvpProxyDevice> deviceByDeviceCode = deviceService.getDeviceByDeviceCode(deviceCode);
        if (deviceByDeviceCode.isEmpty()) {
            String reason = MessageFormat.format("未能找到 设备编码 为 {0} 的设备", deviceCode);
            log.error("{}",reason);
            throw new JsonException(reason);
        } else {
            WvpProxyDevice device = deviceByDeviceCode.get();
            sendRecordControl(device.getGbDeviceId(), device.getGbDeviceChannelId(), recordCmd);
        }
    }

    public void sendRecordControl(String gbDeviceId, String channel, String recordCmd){
        Optional<WvpProxyDocking> deviceByGbDeviceId = dockingService.getDeviceByGbDeviceId(gbDeviceId);
        if(deviceByGbDeviceId.isEmpty()){
            log.info("未能找到 国标编码 {} 的注册信息", gbDeviceId);
            return;
        }
        Optional<WvpProxyDevice> deviceByGbDeviceIdAndChannel = deviceService.getDeviceByGbDeviceIdAndChannel(gbDeviceId, channel);
        if (deviceByGbDeviceIdAndChannel.isEmpty()) {
            log.info("未能找到 编码 {}, 通道 {} 的设备", gbDeviceId, channel);
            return;
        }
        WvpProxyDocking device = deviceByGbDeviceId.get();

        String sn = String.valueOf((int) (Math.random() * 9 + 1) * 100000);
        DeviceControlDTO deviceControlDTO = DeviceControlDTO.builder()
                .sn(sn)
                .deviceId(channel)
                .recordCmd(recordCmd)
                .build();
        sipSender.sendRequest((provider, ip, port)-> SipRequestBuilder.createMessageRequest(device,ip,port,SipRequestBuilder.getCSeq(), XmlUtils.toXml(deviceControlDTO), SipUtil.generateViaTag(),
                SipUtil.generateFromTag(), provider.getNewCallId()));
    }
}
