"use client";

import { useEffect } from 'react';
import axios from 'axios';
import { usePrivy } from '@privy-io/react-auth';
import Link from 'next/link';

const Home: React.FC = () => {
    const { login, ready, user } = usePrivy();

    useEffect(() => {

        const setResult = async (result: string, wallet: string) => {
            const params = new URLSearchParams(window.location.search);
            const requestIdFromUrl = params.get('request_id');

            if (!result.includes('privy')) {
                return;
            }
            try {
                await axios({
                    method: 'get',
                    url: `https://vps.playpoolstudios.com/metahunt/api/launcher/set_request_response.php`,
                    withCredentials: false,
                    params: {
                        id: requestIdFromUrl,
                        result: result
                    },
                });
                console.log(`https://vps.playpoolstudios.com/metahunt/api/launcher/set_request_response.php?id=${requestIdFromUrl}&result=${result}`);
                await axios({
                    method: 'get',
                    url: `https://vps.playpoolstudios.com/metahunt/api/launcher/set_wallet.php`,
                    withCredentials: false,
                    params: {
                        id: result,
                        wallet: wallet,
                        init: "true"
                    },
                });
            } catch (error) {
                console.error('Error making GET request:', error);
            }
        };

        const requestIdFromUrl = new URLSearchParams(window.location.search).get('request_id');

        if (!ready) {
            console.log("not ready yet");
        }
        else if (requestIdFromUrl) {
            setResult(user?.id ?? "", user?.wallet?.address ?? "no wallet"); // Replace 'some_result' with the actual result you want to send
        }
    }, [ready, user]);

    return (
        <div className='flex items-center justify-center bg-gradient-to-br from-purple-900 via-blue-900 to-indigo-900 h-screen text-white'>
            {user && ready ? (
                <div className='justify-center flex flex-col items-center bg-black bg-opacity-30 p-8 rounded-xl backdrop-blur-md shadow-lg'>
                    <h1 className='text-4xl font-bold mb-6 text-transparent bg-clip-text bg-gradient-to-r from-green-400 to-blue-500'>Login Successful!</h1>
                    <p className='mb-6 text-lg text-gray-300'>Your W3B Launcher is now securely connected.</p>
                    <Link href="/dashboard">
                        <div className='bg-gradient-to-r from-purple-500 to-indigo-600 px-10 py-3 items-center justify-center flex rounded-full text-lg font-semibold hover:from-purple-600 hover:to-indigo-700 transition duration-300 shadow-md hover:shadow-xl'>
                            Enter Dashboard
                        </div>
                    </Link>
                </div>
            ) : (
                <div className="flex flex-col items-center justify-center bg-black bg-opacity-30 p-10 rounded-xl backdrop-blur-md shadow-lg">
                    <h1 className="text-4xl font-bold mb-6 text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-blue-500">Connect your W3B Games Launcher</h1>
                    <p className="mb-6 text-lg text-gray-300">Connect your crypto wallet to access the W3B Games ecosystem</p>
                    <button 
                        className="bg-gradient-to-r from-green-400 to-blue-500 hover:from-green-500 hover:to-blue-600 rounded-full px-10 py-3 text-lg font-semibold transition duration-300 shadow-md hover:shadow-xl flex items-center" 
                        onClick={login}
                    >
                        <svg className="w-6 h-6 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
                        </svg>
                        Secure Login
                    </button>
                </div>
            )}
        </div>
    );
};

export default Home;
