import { Thermometer, Sun, CircleGauge, Droplet } from "lucide-react";
import React, { useEffect, useState } from "react";
import axios from "axios";
import { API_BASE_URL } from "../config";
import BoutonGraphique from "./BoutonGraphique";
import AlertInactive from "./AlertInactive";
import ParticularMeteo from "./ParticularMeteo";

function MeteoInfos({ object, graphStates, setGraphStates, graphRefs }) {
  const [rawData, setRawData] = useState([]);
  const [AffAlert, setAffAlert] = useState(false);
  const [AffRegles, setAffRegles] = useState(false);
  const identifiant = object.id;

  useEffect(() => {
    axios.get(`${API_BASE_URL}/meteo?id=${identifiant}`).then((response) => {
      setRawData(response.data);
      if (rawData.length < 5) {
        setAffAlert(true);
      }
    });
  }, [object]);

  const lastData = rawData.length > 0 ? rawData[rawData.length - 1] : null;
  return (
    <div key={object.id} className="bg-white  p-6 rounded-xl min-w-5xl">
      {AffAlert && object.status === "active" && (
        <AlertInactive affAlert={AffAlert} setAffAlert={setAffAlert} />
      )}
      <div className="flex align-items gap-6">
        <div className="w-12 h-12 bg-indigo-100 rounded-lg flex items-center justify-center mb-4">
          <Sun className="text-indigo-600" size={24} />
        </div>
        <h1 className="text-black text-2xl font-bold mb-1  ">Météo actuelle</h1>
      </div>
      {lastData ? (
        <div className="flex flex-col items-center  gap-4">
          <ParticularMeteo
            type="temperature"
            data={lastData}
            Icon={Thermometer}
            texte1="Température"
            texte2="°C"
            graphStates={graphStates}
            setGraphStates={setGraphStates}
            graphRefs={graphRefs}
          />
          
          <ParticularMeteo
            type="pressure"
            data={lastData}
            Icon={CircleGauge}
            texte1="Pression"
            texte2="hPa"
            graphStates={graphStates}
            setGraphStates={setGraphStates}
            graphRefs={graphRefs}
          />

          <ParticularMeteo
            type="humidity"
            data={lastData}
            Icon={Droplet}
            texte1="Humidité"
            texte2="%"
            graphStates={graphStates}
            setGraphStates={setGraphStates}
            graphRefs={graphRefs}
          />
          <h1 className="text-gray-500 text-sm">
            Dernier enregistrement : {lastData.timestamp}
          </h1>
        </div>
      ) : (
        <p>Chargement des données...</p>
      )}
    </div>
  );
}

export default MeteoInfos;
