<?php

namespace App\Factory;

use App\Entity\TransactionAdherentAdherent;
use App\Entity\TransactionAdherentPrestataire;
use App\Entity\TransactionComptoirAdherent;
use App\Entity\TransactionComptoirGroupe;
use App\Entity\TransactionComptoirPrestataire;
use App\Entity\TransactionPrestataireAdherent;
use App\Entity\TransactionPrestatairePrestataire;
use App\Entity\TransfertGroupeComptoir;
use App\Entity\TransfertPrestataireComptoir;
use App\Entity\TransfertPrestataireSiege;
use App\Entity\TransfertSiegeGroupe;
use App\Entity\User;
use App\Enum\MoyenEnum;
use App\Form\Type\AdherentInfosFormType;
use App\Form\Type\ComptoirInfosFormType;
use App\Form\Type\GroupeInfosFormType;
use App\Form\Type\GroupePrestataireInscriptionFormType;
use App\Form\Type\PrestataireInfosFormType;
use App\Form\Type\RegistrationFormType;
use App\Form\Type\TransactionAdherentAdherentFormType;
use App\Form\Type\TransactionAdherentPrestataireFormType;
use App\Form\Type\TransactionPrestataireAdherentFormType;
use App\Form\Type\TransactionPrestatairePrestataireFormType;
use App\Form\Type\TransfertGroupeComptoirFormType;
use App\Form\Type\TransfertPrestataireComptoirFormType;
use App\Form\Type\TransfertPrestataireSiegeFormType;
use App\Form\Type\TransfertSiegeGroupeFormType;
use Symfony\Component\Form\FormFactoryInterface as FormF;
use Symfony\Component\Routing\RouterInterface;

class FormFactory
{
    public $ff;
    public $router;

    public function __construct(FormF $ff, RouterInterface $router)
    {
        $this->ff = $ff;
        $this->router = $router;
    }

    public function getUserInfosForm(User $user)
    {
        if (empty($user)) {
            return null;
        }
        $form = $this->ff->create(RegistrationFormType::class, $user, array('action' => $this->router->generate('user_infos')));

        return $form->createView();
    }

    public function getGroupeInfosForm(User $user)
    {
        if (empty($user) || empty($user->getGroupesgere())) {
            return null;
        }
        $form = $this->ff->create(GroupeInfosFormType::class, $user->getGroupesgere(), array('action' => $this->router->generate('groupe_infos')));

        return $form->createView();
    }

    public function getComptoirInfosForm(User $user)
    {
        if (empty($user) || empty($user->getComptoirsgere())) {
            return null;
        }
        $form = $this->ff->create(ComptoirInfosFormType::class, $user->getComptoirsgere(), array('action' => $this->router->generate('comptoir_infos')));

        return $form->createView();
    }

    public function getPrestataireInfosForm(User $user)
    {
        if (empty($user) || empty($user->getPrestataire())) {
            return null;
        }
        $form = $this->ff->create(PrestataireInfosFormType::class, $user->getPrestataire(), array('action' => $this->router->generate('prestataire_infos')));

        return $form->createView();
    }

    public function getAdherentInfosForm(User $user)
    {
        if (empty($user) || empty($user->getAdherent())) {
            return null;
        }
        $form = $this->ff->create(AdherentInfosFormType::class, $user->getAdherent(), array('action' => $this->router->generate('adherent_infos')));

        return $form->createView();
    }

    public function getGroupePrestataireInscriptionForm(User $user)
    {
        if (empty($user) || empty($user->getPrestataire())) {
            return null;
        }
        $form = $this->ff->create(GroupePrestataireInscriptionFormType::class, $user->getPrestataire(), array('action' => $this->router->generate('groupeprestataire_inscription')));

        return $form->createView();
    }

    public function getTransactionAdherentAdherentForm(User $user)
    {
        if (empty($user) || empty($user->getAdherent())) {
            return null;
        }
        $entity = new TransactionAdherentAdherent();
        $entity->setOperateur($user);
        $entity->setExpediteur($user->getAdherent());
        $form = $this->ff->create(TransactionAdherentAdherentFormType::class, $entity, array('action' => $this->router->generate('transactionAdherentAdherent')));

        return $form->createView();
    }

    public function getTransactionAdherentPrestataireForm(User $user)
    {
        if (empty($user) || empty($user->getAdherent())) {
            return null;
        }
        $entity = new TransactionAdherentPrestataire();
        $entity->setOperateur($user);
        $entity->setExpediteur($user->getAdherent());
        $form = $this->ff->create(TransactionAdherentPrestataireFormType::class, $entity, array('action' => $this->router->generate('transactionAdherentPrestataire')));

        return $form->createView();
    }

    public function getTransactionPrestataireAdherentForm(User $user)
    {
        if (empty($user) || empty($user->getPrestataire())) {
            return null;
        }
        $entity = new TransactionPrestataireAdherent();
        $entity->setOperateur($user);
        $entity->setExpediteur($user->getPrestataire());
        $form = $this->ff->create(TransactionPrestataireAdherentFormType::class, $entity, array('action' => $this->router->generate('transactionPrestataireAdherent')));

        return $form->createView();
    }

    public function getTransactionPrestatairePrestataireForm(User $user)
    {
        if (empty($user) || empty($user->getPrestataire())) {
            return null;
        }
        $entity = new TransactionPrestatairePrestataire();
        $entity->setOperateur($user);
        $entity->setExpediteur($user->getPrestataire());
        $form = $this->ff->create(TransactionPrestatairePrestataireFormType::class, $entity, array('action' => $this->router->generate('transactionPrestatairePrestataire')));

        return $form->createView();
    }

    public function getTransfertPrestataireSiegeForm(User $user)
    {
        if (empty($user) || empty($user->getPrestataire())) {
            return null;
        }
        $entity = new TransfertPrestataireSiege();
        // $entity->setReconverti(false);
        // $entity->setMoyen(MoyenEnum::MOYEN_MLC); //TODO : MOYEN_MLC correct ?
        // $entity->setOperateur($user);
        $entity->setExpediteur($user->getPrestataire());
        $form = $this->ff->create(TransfertPrestataireSiegeFormType::class, $entity, array('action' => $this->router->generate('transactionPrestataireSiege')));

        return $form->createView();
    }

    public function getTransfertPrestataireComptoirForm(User $user)
    {
        if (empty($user) || empty($user->getComptoirsgere())) {
            return null;
        }
        $entity = new TransfertPrestataireComptoir();
        // $entity->setReconverti(false);
        // $entity->setMoyen(MoyenEnum::MOYEN_MLC); //TODO : MOYEN_MLC correct ?
        // $entity->setOperateur($user);
        $entity->setDestinataire($user->getComptoirsgere());
        $form = $this->ff->create(TransfertPrestataireComptoirFormType::class, $entity, array('action' => $this->router->generate('transfertPrestataireComptoir')));

        return $form->createView();
    }

    public function getTransfertComptoirToXForm(User $user, $type)
    {
        $type = strtolower($type);
        if (empty($user) || empty($user->getComptoirsgere()) || !($type == 'adherent' || $type == 'groupe' || $type == 'prestataire')) {
            return null;
        }
        $class = "App\Entity\TransfertComptoir".ucwords($type);
        $entity = new $class();
        $entity->setOperateur($user);
        $entity->setExpediteur($user->getComptoirsgere());
        $form = $this->ff->create('App\Form\Type\TransfertComptoir'.ucwords($type).'FormType', $entity, array('action' => $this->router->generate('transfertComptoir'.ucwords($type))));

        return $form->createView();
    }

    // public function getTransfertGroupeComptoirForm(User $user)
    // {
    //     $type = strtolower($type);
    //     if (empty($user) || empty($user->getGroupesgere())) {
    //         return null;
    //     }
    //     $class = 'Transaction'.ucwords($type).'Comptoir';
    //     $entity = new TransfertGroupeComptoir();
    //     $entity->setOperateur($user);
    //     $entity->setExpediteur($user->getGroupesgere());
    //     $form = $this->ff->create(TransfertGroupeComptoirFormType::class, $entity, array('action' => $this->router->generate('transfertGroupeComptoir')));

    //     return $form->createView();
    // }

    // public function getTransfertSiegeGroupeForm(User $user)
    // {
    //     if (empty($user) || !$user->hasRole('ROLE_ADMIN_SIEGE')) {
    //         return null;
    //     }
    //     $entity = new TransfertSiegeGroupe();
    //     $entity->setOperateur($user);
    //     $form = $this->ff->create(TransfertSiegeGroupeFormType::class, $entity, array('action' => $this->router->generate('transfertSiegeGroupe')));

    //     return $form->createView();
    // }
}
