import { api } from "../../../services/Api";
import { DeviceMeasureDto, IDeviceDto } from "../types";

export class HomePageService {
    public static async getDeviceList(): Promise<IDeviceDto[]> {

        try {
            const response = await api.get<IDeviceDto[]>("device");
            if (response.status === 200) return response.data;
        }

        catch (error) {
            console.error("Error: ", error);
        }
        return [];
    }

    public static async fetchDeviceMeasurements(deviceId: number)
        : Promise<DeviceMeasureDto | null> {
        const response = await api.get<DeviceMeasureDto>(`device/${deviceId}/fetch`)
        if (response.status === 200) return response.data;
        return null;
    }

    public static async updateDevice(dto: IDeviceDto): Promise<IDeviceDto | null> {
        try {
            const response = await api.put<IDeviceDto>(`device`, dto);
            if (response.status === 200) return response.data;
        }

        catch (error) {
            console.error("Error: ", error);
        }

        return null;
    }

    public static async deleteDevice(id: number): Promise<IDeviceDto | null> {
        try {
            const response = await api.delete<IDeviceDto>(`device/${id}`);
            if (response.status === 200) return response.data;
        }

        catch (error) {
            console.error("Error: ", error);
        }

        return null;
    }
}