import { makeAutoObservable } from "mobx";
import { DeviceMeasureDto, IDeviceDto } from "../types";
import { HomePageService } from "../services/HomePageService";
import { homePageStore } from "./HomePageStore";

export class DeviceCardStore {

    deviceMeasurments: DeviceMeasureDto | null = null;
    deviceId: number
    isLoading = true;
    isInvalid = false;

    constructor(id: number) {
        this.deviceId = id;
        makeAutoObservable(this);
    }

    setIsLoading(value: boolean) {
        this.isLoading = value;
    }

    setMeasurements(value: DeviceMeasureDto) {
        this.deviceMeasurments = value;
    }

    async loadDeviceMeasurements() {
        if (this.isInvalid) return;

        if (this.deviceMeasurments === null) this.setIsLoading(true);
        const measurements = await HomePageService.fetchDeviceMeasurements(this.deviceId);
        if (measurements === null) {
            this.isInvalid = true;
            return;
        }
        this.setMeasurements(measurements);

        this.setIsLoading(false);
    }


}