<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace ArtabanMarketplace\Loop;

use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Model\CartItemQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Map\CartItemTableMap;
use ArtabanMarketplace\Model\Map\AmpSellerProductTableMap;
use Propel\Runtime\ActiveQuery\Join;
use Propel\Runtime\ActiveQuery\Criteria;

/**
 * Description of AmpCartSellerITem
 *
 * @author LENGY
 */
class AmpCartSellerITem extends BaseLoop implements PropelSearchLoopInterface {

    /**
     * @inheritDoc
     */
    protected function getArgDefinitions() {
        return new ArgumentCollection(
                Argument::createIntListTypeArgument('cart_id'), Argument::createIntListTypeArgument('amp_seller_id')
        );
    }

    public function parseResults(LoopResult $loopResult) {
        $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry();
        $locale = $this->getCurrentRequest()->getSession()->getLang()->getLocale();
        $checkAvailability = ConfigQuery::checkAvailableStock();
        $defaultAvailability = intval(ConfigQuery::read('default-available-stock', 100));

        /** @var CartItemModel $cartItem */
        foreach ($loopResult->getResultDataCollection() as $cartItem) {
            $product = $cartItem->getProduct(null, $locale);
            $productSaleElement = $cartItem->getProductSaleElements();

            $loopResultRow = new LoopResultRow();

            $loopResultRow->set("ITEM_ID", $cartItem->getId());
            $loopResultRow->set("TITLE", $product->getTitle());
            $loopResultRow->set("REF", $product->getRef());
            $loopResultRow->set("QUANTITY", $cartItem->getQuantity());
            $loopResultRow->set("PRODUCT_ID", $product->getId());
            $loopResultRow->set("PRODUCT_URL", $product->getUrl($this->getCurrentRequest()->getSession()->getLang()->getLocale()));
            if (!$checkAvailability || $product->getVirtual() === 1) {
                $loopResultRow->set("STOCK", $defaultAvailability);
            } else {
                $loopResultRow->set("STOCK", $productSaleElement->getQuantity());
            }
            $loopResultRow->set("PRICE", $cartItem->getPrice())
                    ->set("PROMO_PRICE", $cartItem->getPromoPrice())
                    ->set("TAXED_PRICE", $cartItem->getTaxedPrice($taxCountry))
                    ->set("PROMO_TAXED_PRICE", $cartItem->getTaxedPromoPrice($taxCountry))
                    ->set("IS_PROMO", $cartItem->getPromo() === 1 ? 1 : 0);
            $loopResultRow->set("TOTAL_PRICE", $cartItem->getPrice() * $cartItem->getQuantity())
                    ->set("TOTAL_PROMO_PRICE", $cartItem->getPromoPrice() * $cartItem->getQuantity())
                    ->set("TOTAL_TAXED_PRICE", $cartItem->getTotalTaxedPrice($taxCountry))
                    ->set("TOTAL_PROMO_TAXED_PRICE", $cartItem->getTotalTaxedPromoPrice($taxCountry));
            $loopResultRow->set("PRODUCT_SALE_ELEMENTS_ID", $productSaleElement->getId());
            $loopResultRow->set("PRODUCT_SALE_ELEMENTS_REF", $productSaleElement->getRef());
            $this->addOutputFields($loopResultRow, $cartItem);
            $loopResult->addRow($loopResultRow);
        }

        return $loopResult;
    }

    /**
     * @inheritdoc
     */
    public function buildModelCriteria() {
        $query = CartItemQuery::create();

        if (null != $cart_id = $this->getCartId()) {
            $query->filterByCartId($cart_id);
        }
        
        if (null != $amp_seller_id = $this->getAmpSellerId()) {
            if (is_array($amp_seller_id)) {
                $amp_seller_id = implode(",", $amp_seller_id);
            }

            $ampSellerJoin = new Join(CartItemTableMap::PRODUCT_ID, AmpSellerProductTableMap::PRODUCT_ID, Criteria::LEFT_JOIN);
            $query
                ->addJoinObject($ampSellerJoin, "ampSellerJoin")
                ->where(AmpSellerProductTableMap::AMP_SELLER_ID . " " . Criteria::IN . " (" . $amp_seller_id . ")");
        }

        return $query;
    }

}
