package com.ruoyi.system.core.deviceEntity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.core.ClientExecute;
import com.ruoyi.system.core.domain.DeviceFunc;
import com.ruoyi.system.core.mqtt.DefProtocol;
import com.ruoyi.system.core.mqtt.MqttFuncTopic;
import com.ruoyi.system.core.mqtt.ReplyPackage;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.domain.SysDeviceProperty;
import com.ruoyi.system.domain.SysDeviceStatus;
import com.ruoyi.system.mapper.SysDeviceMapper;
import com.ruoyi.system.mapper.SysDeviceStatusMapper;
import com.ruoyi.system.service.ISysDevicePropertyService;
import com.ruoyi.system.util.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;

public interface ProductEntity {
    default void commonInfo(DefProtocol defProtocol, ClientExecute clientExecute, SysDevice device) {
        ISysDevicePropertyService propertyService = SpringContextHolder.getBean(ISysDevicePropertyService.class);
        SysDeviceProperty property = new SysDeviceProperty();
        property.setFuncId(defProtocol.getFunctionId());
        property.setDeviceType(device.getDeviceType());
        propertyService.selectList(property).subscribe(x -> {
            DeviceFunc deviceFunc = new DeviceFunc();
            deviceFunc.setMessage(defProtocol.getInputs());
            deviceFunc.setFuncTopic(MqttFuncTopic.REPLY);
            clientExecute.send(device, deviceFunc);
        });
    }

    default void commonPut(DefProtocol defProtocol, ClientExecute clientExecute, SysDevice device) {
        SysDeviceStatusMapper statusMapper = SpringContextHolder.getBean(SysDeviceStatusMapper.class);
        statusMapper.selectByFunctionId(defProtocol.getFunctionId())
                .switchIfEmpty(statusMapper.insert(new SysDeviceStatus(defProtocol)).thenReturn(new SysDeviceStatus(defProtocol)))
                .subscribe(x -> {
                    if (x.getDeviceStatusId() != null) {
                        x.setInputs(defProtocol.getInputs());
                        statusMapper.update(x).subscribe();
                    }
                    DeviceFunc deviceFunc = new DeviceFunc();
                    deviceFunc.setFuncTopic(MqttFuncTopic.REPLY);
                    deviceFunc.setMessage(new ReplyPackage(x, defProtocol).getInputs());
                    clientExecute.send(device, deviceFunc);
                    JSONArray objects = JSON.parseArray(defProtocol.getInputs());
                    List<SysDeviceProperty> properties = new ArrayList<>();
                    for (int i = 0; i < objects.size(); i++) {
                        JSONObject jsonObject = objects.getJSONObject(i);
                        SysDeviceProperty sysDeviceProperty = new SysDeviceProperty();
                        sysDeviceProperty.setValue(jsonObject.getString("value"));
                        sysDeviceProperty.setFuncId(defProtocol.getFunctionId());
                        sysDeviceProperty.setName(jsonObject.getString("name"));
                        sysDeviceProperty.setValueCalss(jsonObject.get("value").getClass().toString());
                        sysDeviceProperty.setDeviceType(device.getDeviceType());
                        properties.add(sysDeviceProperty);
                    }
                    ISysDevicePropertyService propertyService = SpringContextHolder.getBean(ISysDevicePropertyService.class);
                    propertyService.sync(device, properties, x.getFunctionId()).subscribe();
                    SysDeviceMapper deviceMapper = SpringContextHolder.getBean(SysDeviceMapper.class);
                    deviceMapper.update(device).subscribe();
                });
    }

    void put(DefProtocol defProtocol, ClientExecute clientExecute, SysDevice device);

    void info(DefProtocol defProtocol, ClientExecute clientExecute, SysDevice device);


}
