// components/Header.tsx

"use client";

import React, { useState, useEffect } from "react";
import Link from 'next/link';
import { FaTwitter, FaWallet } from "react-icons/fa";
import { CheckIcon } from "@heroicons/react/outline";
import { usePrivy } from "@privy-io/react-auth";

const Header: React.FC = () => {
  const { login, user, ready, logout, linkTwitter, unlinkTwitter, linkWallet, unlinkWallet } = usePrivy();
  const [twitterConnected, setTwitterConnected] = useState(false);
  const [walletConnected, setWalletConnected] = useState(false);
  const postLoginAPI = async (usertag) => {
    try {
      const response = await fetch('https://api.callfi.io/register_twitter_user.php?tag=' + usertag);

      if (!response.ok) {
        throw new Error('Network response was not ok');
      }

      const data = await response.json();
      console.log('API call successful:', data);
      setTwitterConnected(true);
    } catch (error) {
      console.error('Error during API call:', error);
    }
  };
  
  useEffect(() => {
    if (ready && user) {
        const username = user.twitter?.username ? `@${user.twitter.username}` : '@unknownUser';
      if (username !== "@unknownUser") {
        postLoginAPI(username);
      }
      const walletStatus = user.wallet;
      if (walletStatus) {
        setWalletConnected(true);
      }
    }
  }, [ready, user]);

  return (
    <header className="w-full flex justify-between items-center p-4 bg-gray-800 text-white glassmorphism">
      <div className="flex items-center">
      <Link href="/"><h1 className="text-xl font-bold px-10">CallFi</h1></Link>
        <nav className="flex space-x-4">
          <Link href="/users">
            <p className={` text-white hover:underline`}>User Ranking</p>
          </Link>
        </nav>
      </div>
      <div className="flex items-center">
        {ready && user ? (
          <div className="flex items-center bg-white bg-opacity-20 rounded-full px-4 py-2 glassmorphism">
            <button className={`relative ${twitterConnected ? "bg-black" : "bg-gray-700"} hover:bg-gray-500 text-white font-bold py-2 px-4 rounded-full mr-2 glassmorphism`} onClick={linkTwitter}>
              <FaTwitter className="h-5 w-5 text-white" />
              {twitterConnected && (
                <CheckIcon className="absolute top-0 right-0 h-4 w-4 text-green-500" />
              )}
            </button>
            <button className={`relative ${walletConnected ? "bg-black" : "bg-gray-700"} hover:bg-gray-500 text-white font-bold py-2 px-4 rounded-full mr-2 glassmorphism`} onClick={linkWallet}>
              <FaWallet className="h-5 w-5 text-white" />
              {walletConnected && (
                <CheckIcon className="absolute top-0 right-0 h-4 w-4 text-green-500" />
              )}
            </button>
            <button onClick={logout} className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded glassmorphism">
              Logout
            </button>
          </div>
        ) : !ready ? (
          <p>Loading...</p>
        ) : (
          <button onClick={login} className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded glassmorphism">
            Login
          </button>
        )}
      </div>
    </header>
  );
}

export default Header;
