import { Wind } from "lucide-react";
import React, { useEffect, useState } from "react";

import axios from "axios";
import { API_BASE_URL } from "../config";

function WindInfo({ object, defAffWindGraph, AffWindGraph }) {
  const [rawData, setRawData] = useState([]);
  const identifiant = object.id;
  useEffect(() => {
    axios.get(`${API_BASE_URL}/wind?id=${identifiant}`).then((response) => {
      setRawData(response.data);
    });
  }, [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">
      <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">
          <Wind className="text-indigo-600" size={24} />
        </div>
        <h1 className="text-black text-2xl font-bold mb-1  ">Vent actuel</h1>
      </div>
      {lastData ? (
        <div className="flex flex-col items-center  gap-4">
          <div className="flex flex-row gap-4">
            <div className="flex flex-col items-center g-2">
              <img
                src={`./src/img/${lastData.wind_direction}.png`}
                alt="Wind Direction"
                className="h-45"
              />
              <h1 className="text-gray-600 text-xl font-bold mb-1  ">
                {lastData.wind_direction}
              </h1>
            </div>
            <div className="flex flex-col items-center gap-4">
              <div className="flex align-items gap-1">
                <div className="flex items-center">
                  <Wind className="text-red-600" size={24} />
                </div>
                <h1 className="text-red-600 text-xl font-bold ">Valeur</h1>
              </div>
              <h1 className="text-gray-700 text-4xl font-bold">
                {lastData.wind_speed} <span className="text-lg">km/h</span>
              </h1>
            </div>
          </div>
          {!AffWindGraph ? (
            <button
              className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 mt-4 px-4 rounded-full"
              onClick={() => defAffWindGraph(true)}
            >
              Afficher l'historique
            </button>
          ) : (
            <button
              className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 mt-4 px-4 rounded-full"
              onClick={() => defAffWindGraph(false)}
            >
              Masquer l'historique
            </button>
          )}

          <h1 className="text-gray-500 text-sm">
            Dernier enregistrement : {lastData.timestamp}
          </h1>
        </div>
      ) : (
        <p>Chargement des données...</p>
      )}
    </div>
  );
}

export default WindInfo;
