import React, { useState, useEffect } from 'react';
import './App.css';
import Header from './components/Header';
import Footer from './components/Footer';
import PlayerDetail from './components/PlayerDetail';
import PlayersTable from './components/PlayersTable';
import CreatePlayer from './components/CreatePlayer';
import ModifyPlayer from './components/ModifyPlayer';
import LoginForm from './components/LoginForm';
import RegisterForm from './components/RegisterForm';
import Alert from './components/Alert';
import axios from 'axios';

const FreePlayers = () => {
    const [logged, setLogged] = useState(false);
    const [showLoginForm, setShowLoginForm] = useState(false);
    const [showRegisterForm, setShowRegisterForm] = useState(false);
    const [showCreatePlayerForm, setShowCreatePlayerForm] = useState(false);
    const [showModifyPlayerForm, setShowModifyPlayerForm] = useState(false);

    const [showAlert, setShowAlert] = useState(false);
    const [alertType, setAlertType] = useState(0);
    const [alertMessage, setAlertMessage] = useState("");

    const [showPlayerDetail, setShowPlayerDetail] = useState(false);
    const [player, setPlayer] = useState(null);
    const [data, setData] = useState(null);
    const [isAdmin, setIsAdmin] = useState(false);

    const showPlayerDetails = (player) => {
        setPlayer(player);
        isAdmin ? setShowModifyPlayerForm(true) : setShowPlayerDetail(true);
    }

    const getPlayers = () => {
        axios.get('http://localhost:8080/pa165/players/freeAgents', {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + localStorage.getItem("token")
            }
        })
            .then(function (response) {
                setData(response.data);
            })
            .catch(function (error) {
                setAlertType(1);
                setAlertMessage("Coulnd load data");
                setShowAlert(true);
            });
    }

    useEffect(() => {
        if (logged) {
            if (data === null) {
                getPlayers();
            }
        } else {
            setData(null);
        }
        if (localStorage.getItem("isAdmin") == "true") {
            setIsAdmin(true);
        } else {
            setIsAdmin(false);
        }
    }, [logged, data]);

    return (
        <div className="myteam">
            <Alert showAlert={showAlert} alertType={alertType} alertMessage={alertMessage} setShowAlert={setShowAlert} setAlertType={setAlertType} setAlertMessage={setAlertMessage} />
            <Header logged={logged} setLogged={setLogged} setShowLoginForm={setShowLoginForm} showRegisterForm={setShowRegisterForm} />
            <LoginForm showLoginForm={showLoginForm} setShowLoginForm={setShowLoginForm} setLogged={setLogged} setShowRegisterForm={setShowRegisterForm} setAlertType={setAlertType} setAlertMessage={setAlertMessage} setShowAlert={setShowAlert} />
            <RegisterForm showRegisterForm={showRegisterForm} setShowRegisterForm={setShowRegisterForm} setShowLoginForm={setShowLoginForm} setAlertType={setAlertType} setAlertMessage={setAlertMessage} setShowAlert={setShowAlert} />
            <PlayerDetail showPlayerDetail={showPlayerDetail} setShowPlayerDetail={setShowPlayerDetail} player={player} />
            <CreatePlayer showCreatePlayerForm={showCreatePlayerForm} setShowCreatePlayerForm={setShowCreatePlayerForm} setAlertType={setAlertType} setAlertMessage={setAlertMessage} setShowAlert={setShowAlert} />
            <ModifyPlayer showModifyPlayerForm={showModifyPlayerForm} setShowModifyPlayerForm={setShowModifyPlayerForm} setAlertType={setAlertType} setAlertMessage={setAlertMessage} setShowAlert={setShowAlert} player={player} setPlayer={setPlayer} setData={setData} />
            <main className="container-fluid main">
                <section className="container-xxl">
                    <PlayersTable data={data} showPlayerDetails={showPlayerDetails} setShowCreatePlayerForm={setShowCreatePlayerForm} setData={setData} />
                </section>
            </main>
            <Footer />
        </div>
    );
}

export default FreePlayers;
