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

import axios from "axios";
import { API_BASE_URL } from "../config";
import BoutonGraphique from "./BoutonGraphique";
import { useTranslation } from "react-i18next";

function WindInfo({ object, setGraphStates, graphStates, graphRefs, reference}) {
  const { t } = useTranslation();
  const [rawData, setRawData] = useState([]);
  const identifiant = object.id;
  useEffect(() => {
    axios.get(`${API_BASE_URL}/wind?id=${identifiant}`).then((response) => {
      setRawData(response.data);
    });
  }, [object]);
  useEffect(() => {
    if (reference?.current) {
      reference.current.scrollIntoView({ behavior: "smooth" });
    }
  }, [reference]);

  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-1">
          <Wind className="text-indigo-600" size={24} />
        </div>
        <h1 className="text-black text-2xl font-bold">{t('components.windInfo.currentWind')}</h1>
      </div>
      {lastData ? (
        <div className="flex flex-col items-center  gap-1">
          <img
            src={`.//img/${lastData.wind_direction}.png`}
            alt="Wind Direction"
            className="h-25"
          />
          <h1 className="text-gray-600 text-xl font-bold mb-1  ">
            {lastData.wind_direction}
          </h1>
          <div className="bg-indigo-50 rounded-lg flex flex-col items-center mb-1 w-full">
            <div className="flex align-items gap-3 w-full justify-between">
              <div className="flex align-items ml-3 gap-2">
                <div className="flex items-center">
                  <Wind className="text-indigo-600" size={40} />
                </div>
                <div className="flex flex-col items-start">
                  <h1 className="text-indigo-600 text-xl font-bold ">{t('components.windInfo.value')}</h1>
                  <h1 className="text-gray-700 text-4xl font-bold">
                    {lastData.wind_speed} <span className="text-lg">Km/h</span>
                  </h1>
                </div>
              </div>
               <BoutonGraphique
                type="wind"
                graphStates={graphStates}
                setGraphStates={setGraphStates}
                graphCible={graphRefs.wind}
              />
            </div>
          </div>
          <h1 className="text-gray-500 text-sm">
            {t('components.windInfo.lastRecord')} {lastData.timestamp}
          </h1>
        </div>
      ) : (
        <p>{t('components.windInfo.loading')}</p>
      )}
    </div>
  );
}

export default WindInfo;
