<?php

namespace App\Controller;

use Database\AthleteMapper;
use Shared\Validation;
use App\Container;
use App\Router\Request\IRequest;
use App\Router\Response\Response;
use Shared\Attributes\Route;
use Twig\Environment;
use Data\Core\Preferences;
use Shared\Log;
use Manager\UserManager;
use Database\Connexion;
use Database\AthleteGateway;

class AthleteController extends BaseController
{

    private Environment $twig;
    protected Preferences $preference;

    public function __construct()
    {
       //session_start();
       $this->preference = new Preferences();
    }

//    #[Route(path: '/search-user', name: 'search-user', methods: ['GET'])]
//    public function searchUser(string $username, IRequest $req): Response
//    {
//        $taberror = [];
//        if(!Validation::val_string($username)){
//            print("Nom invalide.");
//        }
//        else {
//            try {
//                $athleteGateway = new AthleteGateway(new Connexion("mysql:host=localhost;dbname=sae_3", "Perederii", ""));
////                $listSearch=$this->container->get(UserManager::class)->searchUsersByName($username);
//                $listSearch = $athleteGateway->getAthlete();
//                $map = new AthleteMapper ();
//                $athleteEntity = $map->athleteSqlToEntity($listSearch);
//                foreach ($athleteEntity as $entity) {
//                    $users = $map->athleteEntityToModel($entity);
//                    $listUsers[$i++]=['nom' => $users->getNom(), 'prenom'=>$users->getPrenom(), 'img'=>'john_doe'];
//                }
//
//                $i=0;
//                foreach ($users as $user){
//                    echo "<h1> HELLO </h1>";
//                    $listUsers[$i++]=['nom' => $user->getNom(), 'prenom'=>$user->getPrenom(), 'img'=>'john_doe', 'username'=>$user->getUsername()];
//                }
//                echo "<h1> HEllO </h1>";
//                //$currentUser=$this->container->get(UserManager::class)->getCurrentUser();
//                //$currentUser=$_SESSION['user'];
//                $response = $this->render('./page/addfriend.html.twig',[
//                    'css' => $_COOKIE['preferences'],
//                    'pp' => "test",
//                    'user' => 'johndoe',//$currentUser->getUsername(),
//                    'role' => 'Athlete',//$currentUser->getRole(),
//                    'users' => $listUsers
//                ]);
//            } catch (\Throwable $th) {
//                throw $th;
//                return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
//            }
//        }
//        return $response;
//
//    }

    #[Route(path: '/search-user', name: 'search-user', methods: ['GET'])]
    public function searchUser(string $username, IRequest $req): Response
    {
        $taberror = [];
        if (!Validation::val_string($username)) {
            print("Nom invalide.");
        } else {
            try {
                $athleteGateway = new AthleteGateway(new Connexion("pgsql:host=localhost;dbname=sae_3", "Perederii", ""));
                $listSearch = $athleteGateway->getAthlete($username);
                $map = new AthleteMapper();
                $athleteEntity = $map->athleteSqlToEntity($listSearch);

                $listUsers = [];
                $i = 0;
                foreach ($athleteEntity as $entity) {
                    $user = $map->athleteEntityToModel($entity);
                    $listUsers[$i] = ['idathlete' => number_format($user->getId(), 1), 'nom' => $user->getNom(),
                        'prenom' => $user->getPrenom(),'email' => $user->getEmail(), 'sexe' => $user->getSexe(),
                        'taille' => $user->getTaille(), 'poids' => $user->getPoids(), 'motdepasse' => $user->getMotDePasse(),
                        'datenaissance' => $user->getDateNaissance(), 'iscoach' => $user->getRole(), 'img' => 'test',
                        'username' => $user->getUsername()];
                    $i++;
                }

                $response = $this->render('./page/addfriend.html.twig',[
                    'css' => $_COOKIE['preferences'],
                    'pp' => 'test',
                    'user' => 'johndoe',//$currentUser->getUsername(),
                    'role' => 'Athlete',//$currentUser->getRole(),
                    'users' => $listUsers
                ]);

            } catch (\Throwable $th) {
                throw $th;
                return $this->render("addfriend.html.twig", ['tabError' => $taberror]);
            }
        }
        return $response;
    }

    #[Route(path: '/analyses', name: 'analyses', methods: ['GET'])]
    public function analyses(): Response
    {
        return $this->render('./page/analyze.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => 'johndoe',//$currentUser->getUsername(),
            'role' => 'Athlete',//$currentUser->getRole(),
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => []
        ]);
    }

    #[Route(path: '/activity', name: 'activity', methods: ['GET'])]
    public function activity(): Response
    {
        return $this->render('./page/activity.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test",
            'user' => 'johndoe',//$currentUser->getUsername(),
            'role' => 'Athlete',//$currentUser->getRole(),
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => []
        ]);
    }


    #[Route(path: '/exercice', name: 'exercice', methods: ['GET'])] // 8
    public function exercice(): Response
    {
        return $this->render('./page/exercice.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => 'johndoe',//$currentUser->getUsername(),
            'role' => 'Athlete',//$currentUser->getRole(),
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => []
        ]);
    }

    #[Route(path: '/exercices', name: 'exercices', methods: ['POST'])] // 8
    public function exercices(String $type, String $intensite, String $date, IRequest $req): Response
    {
        $exercicesArray = [
            [
                'date' => $date,
                'type' => $type,
                'intensite' => $intensite,
                'status' => 'A venir',
            ]
        ];
        return $this->render('./page/exercice.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => 'johndoe',//$currentUser->getUsername(),
            'role' => 'Athlete',//$currentUser->getRole(),
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => $exercicesArray,
            'member' => []
        ]);
    }

    #[Route(path: '/add-friend', name: 'add-friend', methods: ['POST'])]
    public function addFriend(string $username, IRequest $req): Response
    {
        try {
            //Ajouter la personne sur laquelle on a clique dans la liste de currentuser
            $listSearch=$this->container->get(UserManager::class)->searchUsersByName($username);
            $i=0;
            foreach ($listSearch as $user){
                $listUsers[$i++]=['nom' => $user->getNom(), 'prenom'=>$user->getPrenom(), 'img'=>'test', 'username'=>$user->getUsername()];
            }
            return $this->render('./page/addfriend.html.twig',[
                'css' => $this->preference->getCookie(),
                'pp' => "test2",
                'user' => 'johndoe',//$currentUser->getUsername(),
                'role' => 'Athlete',//$currentUser->getRole(),
                'friendship' => [],
                'analyzes' => [],
                'mails' => [],
                'users' => $listUsers,
                'infoUser' => [],
                'exos' => [],
                'member' => [],
                'responce' => "Notification d'ajout envoyée à $username"
            ]);
        } catch (\Throwable $th) {
            throw $th;
            return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
        }        
    }

    #[Route(path: '/friend', name: 'friend', methods: ['GET'])]
    public function friend(): Response
    {
        return $this->render('./page/addfriend.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => 'John',//$currentUser->getUsername(),
            'role' => 'Athlete',//$currentUser->getRole(),
            'friendship' => [],
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => [],
        ]);
    }

    /*#[Route(path: '/friendlist', name: 'friendlist', methods: ['POST'])]
    public function friendlist(string $username, IRequest $req): Response
    {
        $friendList = [
            [
                'nom' => 'John',
                'prenom' => 'Doe',
                'img' => 'john_doe',
                'username' => 'johndoe',
            ],
            [
                'nom' => 'Alice',
                'prenom' => 'Smith',
                'img' => 'alice_smith',
                'username' => 'alicesmith',
            ],
        ];
        // TODO 

        // -> Enlever ou bloquer un utilisateur en fonction de son username
        //$friendList = currentUser->getFriends();
        return $this->render('./page/friend.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => 'johndoe',//$currentUser->getUsername(),
            'role' => 'Athlete',//$currentUser->getRole(),
            'friendship' => $friendList,
        ]);
    }*/

    #[Route(path: '/friendlist', name: 'friendlist2', methods: ['GET'])]
    public function friendlist2(): Response
    {
        $friendList = $this->container->get(UserManager::class)->getFriends();//$currentUser->getFriends();
        return $this->render('./page/friend.html.twig',[
            'css' => $this->preference->getCookie(),
            'pp' => "test2",
            'user' => "Doe",
            'role' => "Athlète",
            'friendship' => $friendList,
            'analyzes' => [],
            'mails' => [],
            'users' => [],
            'infoUser' => [],
            'exos' => [],
            'member' => [],
        ]);
    }

    // #[Route(path: '/delete-friend', name: 'delete-friend', methods: ['POST'])]
    
}

?>