// Libs
import request from 'services/request';
import { environment } from 'enviroments/enviroments';
import { SensorDevice, SensorInterfacesList } from '../myApi';
import { makeAutoObservable, runInAction, toJS } from 'mobx';

export class SensorStore {
  constructor() {
    makeAutoObservable(this);
  }

  sensor: SensorDevice | null = null;

  sensorInterfaces: SensorInterfacesList | null = null;

  get getSensorDetailById(): SensorDevice | null {
    return toJS(this.sensor);
  }

  get getSensorInterfacesById(): SensorInterfacesList | null {
    return toJS(this.sensorInterfaces);
  }

  fetchSensorDetailById = async (id: number): Promise<void> => {
    const response = await request({
      method: 'get',
      url: `devices/${environment.devices.sensor}${id}/`,
    });

    runInAction(() => {
      this.sensor = response.data;
    });
  };

  fetchSensorInterfacesDetailById = async (id: number): Promise<void> => {
    const response = await request({
      method: 'get',
      url: `devices/sensor/${id}/system/interfaces/`,
    });

    runInAction(() => {
      this.sensorInterfaces = response.data;
    });
  };

  clearSensorDetailById = (): void => {
    this.sensor = null;
    this.clearSensorInterfacesById();
  };

  clearSensorInterfacesById = (): void => {
    this.sensorInterfaces = null;
  };
}
