<?php

namespace App\Service\Frontend;

use Doctrine\ORM\EntityManagerInterface;

use App\Repository\AffichageRepository;
use App\Repository\ArticleRepository;
use App\Repository\ServiceRepository;

class ServiceHome
{

    /**
     * @var AffichageRepository $affichageRepository
     */
    private $affichageRepository;

    /**
     * @var ArticleRepository $articleRepository
     */
    private $articleRepository;

    /**
     * @var ServiceRepository $serviceRepository
     */
    private $serviceRepository;

    /**
     * @var EntityManagerInterface $em
     */
    private $em;


    /**
     * @param AffichageRepository $affichageRepository
     * @param ArticleRepository $articleRepository
     * @param ServiceRepository $serviceRepository
     * @param EntityManagerInterface $em
     * @return $this
     */
    public function __construct(
        AffichageRepository $affichageRepository,
        ArticleRepository $articleRepository, 
        ServiceRepository $serviceRepository,
        EntityManagerInterface $em
    ){
        $this->affichageRepository = $affichageRepository;
        $this->articleRepository = $articleRepository;
        $this->serviceRepository = $serviceRepository;
        $this->em = $em;
    }

    public function show()
    {
        $affichages = $this->affichageRepository->findBy(['date_show'=>new \DateTime()], ["ligne"=>"ASC", "largeur"=>"DESC"]);
        $articles = $this->articleRepository->show();
        $services = $this->serviceRepository->show();

        $Art = $Ser = 0;
        $LengArt = count($articles);
        $LengSer = count($services);
        $promos = [];

        foreach($affichages as $affichage){
            $promos[$affichage->getLigne()]['largeur'][] = $affichage->getLargeur();
            
            $annonce = $affichage->getAnnonce();
            $promos[$affichage->getLigne()]['promos'][] = ['id'=>$annonce->getId(), 'img'=>$annonce->getImageHome(), 'type'=>'annonce', 'title'=>$annonce->getTitre()];   
        }

        for($i=1; $i<=12; $i++){
            
            $largeur = 0;
            if(isset($promos[$i]['largeur'])){
                foreach($promos[$i]['largeur'] as $larg){
                    $largeur += $larg;
                }
            }

            //(largeur=4)
            if($largeur == 4){
                break;
            }

            //(largeur=3) 1
            if($largeur == 3){
                $dataLarg1111 = $this->Largeur31($promos, $i, $articles, $Art, $LengArt, $services, $Ser, $LengSer);
                $Art = $dataLarg1111['Art'];
                $Ser = $dataLarg1111['Ser'];
                $promos = $dataLarg1111['promos'];
            }

            //(largeur=2) 1, 1
            if($largeur == 2){
                $dataLarg1111 = $this->Largeur211($promos, $i, $articles, $Art, $LengArt, $services, $Ser, $LengSer);
                $Art = $dataLarg1111['Art'];
                $Ser = $dataLarg1111['Ser'];
                $promos = $dataLarg1111['promos'];
            }


            //(largeur=1) 1, 1, 1
            if($largeur == 1){
                $dataLarg1111 = $this->Largeur1111($promos, $i, $articles, $Art, $LengArt, $services, $Ser, $LengSer);
                $Art = $dataLarg1111['Art'];
                $Ser = $dataLarg1111['Ser'];
                $promos = $dataLarg1111['promos'];
            }

            //(largeur=0) 1, 1, 1, 1
            if($largeur == 0){
                $dataLarg1111 = $this->Largeur01111($promos, $i, $articles, $Art, $LengArt, $services, $Ser, $LengSer);
                $Art = $dataLarg1111['Art'];
                $Ser = $dataLarg1111['Ser'];
                $promos = $dataLarg1111['promos'];
            }

        }
        
       
        ksort($promos);

        return $promos;
    }



    //largeur 2, 1, 1
    public function Largeur211($promos, $start, $articles, $art, $lengArt, $services, $ser, $lengSer)
    {
        if( ($art<$lengArt) && (isset($articles[$art])) ){
            $promos[$start]['largeur'][] = 1;
            $promos[$start]['promos'][] = ['id'=>$articles[$art]->getId(), 'img'=>$articles[$art]->getImageDefault(), 'type'=>'article', 'title'=>$articles[$art]->getTitre()];
            $art++;
        }elseif( ($ser<$lengSer) && (isset($services[$ser])) ){
            $promos[$start]['largeur'][] = 1;
            $promos[$start]['promos'][] = ['id'=>$services[$ser]->getId(), 'img'=>$services[$ser]->getImageDefault(), 'type'=>'service', 'title'=>$services[$ser]->getTitre()];
            $ser++;
        }

        if( ($ser<$lengSer) && (isset($services[$ser])) ){
            $promos[$start]['largeur'][] = 1;
            $promos[$start]['promos'][] = ['id'=>$services[$ser]->getId(), 'img'=>$services[$ser]->getImageDefault(), 'type'=>'service', 'title'=>$services[$ser]->getTitre()];
            $ser++;
        }elseif( ($art<$lengArt) && (isset($articles[$art])) ){
            $promos[$start]['largeur'][] = 1;
            $promos[$start]['promos'][] = ['id'=>$articles[$art]->getId(), 'img'=>$articles[$art]->getImageDefault(), 'type'=>'article', 'title'=>$articles[$art]->getTitre()];
            $art++;
        }

        return ['promos'=>$promos, 'Art'=>$art, 'Ser'=>$ser];
    }

    //largeur 3, 1
    public function Largeur31($promos, $start, $articles, $art, $lengArt, $services, $ser, $lengSer)
    {
        if( ($art<$lengArt) && (isset($articles[$art])) ){
            $promos[$start]['largeur'][] = 1;
            $promos[$start]['promos'][] = ['id'=>$articles[$art]->getId(), 'img'=>$articles[$art]->getImageDefault(), 'type'=>'article', 'title'=>$articles[$art]->getTitre()];
            $art++;
        }elseif( ($ser<$lengSer) && (isset($services[$ser])) ){
            $promos[$start]['largeur'][] = 1;
            $promos[$start]['promos'][] = ['id'=>$services[$ser]->getId(), 'img'=>$services[$ser]->getImageDefault(), 'type'=>'service', 'title'=>$services[$ser]->getTitre()];
            $ser++;
        }

        return ['promos'=>$promos, 'Art'=>$art, 'Ser'=>$ser];
    }

    //largeur 1, 1, 1
    public function Largeur1111($promos, $start, $articles, $art, $lengArt, $services, $ser, $lengSer)
    {
        $l = 1;
        do{
            if( ($art<$lengArt) && (isset($articles[$art])) ){
                $promos[$start]['largeur'][] = 1;
                $promos[$start]['promos'][] = ['id'=>$articles[$art]->getId(), 'img'=>$articles[$art]->getImageDefault(), 'type'=>'article', 'title'=>$articles[$art]->getTitre()];
                $art++;
            }
    
            if( ($ser<$lengSer) && (isset($services[$ser])) ){
                $promos[$start]['largeur'][] = 1;
                $promos[$start]['promos'][] = ['id'=>$services[$ser]->getId(), 'img'=>$services[$ser]->getImageDefault(), 'type'=>'service', 'title'=>$services[$ser]->getTitre()];
                $ser++;
            }

            $l++;
        }while( ($l<=3) and (($art<$lengArt) or ($ser<$lengSer)) );
        
        return ['promos'=>$promos, 'Art'=>$art, 'Ser'=>$ser];
    }

    //largeur 1, 1, 1, 1
    public function Largeur01111($promos, $start, $articles, $art, $lengArt, $services, $ser, $lengSer)
    {
        $l = 1;
        do{
            if( ($art<$lengArt) && (isset($articles[$art])) ){
                $promos[$start]['largeur'][] = 1;
                $promos[$start]['promos'][] = ['id'=>$articles[$art]->getId(), 'img'=>$articles[$art]->getImageDefault(), 'type'=>'article', 'title'=>$articles[$art]->getTitre()];
                $art++;
            }
    
            if( ($ser<$lengSer) && (isset($services[$ser])) ){
                $promos[$start]['largeur'][] = 1;
                $promos[$start]['promos'][] = ['id'=>$services[$ser]->getId(), 'img'=>$services[$ser]->getImageDefault(), 'type'=>'service', 'title'=>$services[$ser]->getTitre()];
                $ser++;
            }

            $l++;
        }while( ($l<=4) and (($art<$lengArt) or ($ser<$lengSer)) );
        
        return ['promos'=>$promos, 'Art'=>$art, 'Ser'=>$ser];
    }




}

