<?php

/**
 * Extends Loops
 *
 * Class Loop
 * @author Gilles Lengy
 */

namespace ArtabanMarketplace\EventListeners;

use ArtabanMarketplace\Model\Map\AmpSellerProductVersionTableMap;
use ArtabanMarketplace\Model\AmpSellerProductQuery;
use ArtabanMarketplace\Model\AmpSellerVersionQuery;
use ArtabanMarketplace\Model\AmpSellerI18nQuery;
use ArtabanMarketplace\Model\AmpSeller;
use Thelia\Model\Map\ProductTableMap;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Event\Loop\LoopExtendsArgDefinitionsEvent;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Core\Event\Loop\LoopExtendsInitializeArgsEvent;
//use Thelia\Core\EventListeners\LoopExtendsBuildModelCriteriaEvent as LoopExtendsBuildModelCriteriaEventListeners; // !!!!!!!!!!!!!!!!!!!! ATTENTION ARTABAN GL :::: Vraiment utile ? Apparement non. Ca à l'air de marcher sans.
use Thelia\Core\Event\Loop\LoopExtendsBuildModelCriteriaEvent;
use Propel\Runtime\ActiveQuery\Join;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Core\Event\Loop\LoopExtendsParseResultsEvent;

class Loop implements EventSubscriberInterface {

    /** @var Request $request */
    protected $request = null;

    public function __construct(Request $request) {
        $this->request = $request;
    }

    /**
     * @inheritdoc
     */
    public static function getSubscribedEvents() {
        return [
            // Product
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_ARG_DEFINITIONS, 'product') => ['productArgDefinitions', 128],
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_INITIALIZE_ARGS, 'product') => ['productInitializeArgs', 128],
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_BUILD_MODEL_CRITERIA, 'product') => ['productBuildModelCriteria', 128],
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_PARSE_RESULTS, 'product') => ['productParseResults', 128],
        ];
    }

    /**
     * Add a new parameter for loop lang
     * you can now call the loop with this argument
     */
    public function productArgDefinitions(LoopExtendsArgDefinitionsEvent $event) {
        $argument = $event->getArgumentCollection();
        $argument->addArgument(Argument::createIntListTypeArgument('amp_seller_id'));
    }

    /**
     * Change the query search of the loop product
     */
    public function productBuildModelCriteria(LoopExtendsBuildModelCriteriaEvent $event) {
        $Array_id = $event->getLoop()->getAmpSellerId();
        if (!empty($Array_id)) {
            $id = $Array_id[0];
            $ampProductSellerJoin = new Join(ProductTableMap::ID, AmpSellerProductVersionTableMap::PRODUCT_ID, Criteria::LEFT_JOIN);
            $event->getModelCriteria()
                    ->addJoinObject($ampProductSellerJoin, "ampSellerProductJoin")
                    ->where(AmpSellerProductVersionTableMap::AMP_SELLER_ID . " " . Criteria::IN . " (" . $id . ")");
        }
    }

    /**
     * Add the output variables of the loop product
     */
    public function productParseResults(LoopExtendsParseResultsEvent $event) {
        $loopResult = $event->getLoopResult();
        
        foreach ($loopResult as $row) {

            $query_product = new AmpSellerProductQuery();
            $result_query_array_product = $query_product->filterByProductId($row->get('ID'))->find();
            if($result_query_array_product[0]){
            $amp_seller_id = $result_query_array_product[0]->getAmpSellerId();
            }
            
            if ($amp_seller_id > 0) {
                $query_amp_seller = new AmpSellerVersionQuery();
                $result_query_array_amp_seller = $query_amp_seller->filterById($amp_seller_id)->find();
                $amp_seller = $result_query_array_amp_seller[0];
                $row->set('ADDRESS1', $amp_seller->getAddress1());
                $row->set('ID_AMP_SELLER', $amp_seller->getID());
                $query_amp_sellerI18 = new AmpSellerI18nQuery; 
                $result_query_array_amp_sellerI18 = $query_amp_sellerI18->filterById($amp_seller->getID())->find();
                $amp_sellerI18 = $result_query_array_amp_sellerI18[0];
                $row->set('TITLE_SELLER', $amp_sellerI18->getTitle());
            }            
        }
    }

    /**
     * Set parameters from the query string
     */
    public function productInitializeArgs(LoopExtendsInitializeArgsEvent $event) {
        $parameters = $event->getLoopParameters();
        if ($this->request->query->has('loop-amp_seller_id')) {
            $parameters['amp_seller_id'] = 1; // ARTABAN GL 'booléen'
            $event->setLoopParameters($parameters);
        }
    }

}
