<?php
/**
* 2017 - Keyrnel SARL
*
* NOTICE OF LICENSE
*
* The source code of this module is under a commercial license.
* Each license is unique and can be installed and used on only one shop.
* Any reproduction or representation total or partial of the module, one or more of its components,
* by any means whatsoever, without express permission from us is prohibited.
* If you have not received this module from us, thank you for contacting us.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @author    Keyrnel
* @copyright 2017 - Keyrnel SARL
* @license   commercial
* International Registered Trademark & Property of Keyrnel SARL
*/

class FlashsalesPageModuleFrontController extends ModuleFrontController
{
    public $adapter;
    public $id_flash_sale;
    public $filter;

    public function __construct()
    {
        if (version_compare(_PS_VERSION_, '1.7', '>=')) {
            $this->adapter = new FlashSaleListingFrontController($this);
        }
        
        parent::__construct();

        $this->filter = 'page';
        $this->id_flash_sale = Tools::getvalue('id_flash_sale') ? (int)Tools::getvalue('id_flash_sale') : null;
    }

    public function init()
    {
        parent::init();

        if ($this->id_flash_sale) {
            $this->initFlashSaleDetails();
        } else {
            $this->initFlashSaleListing();
        }
    }

    public function initFlashSaleDetails()
    {
        if (version_compare(_PS_VERSION_, '1.7', '>=')) {
            $this->adapter->init();
        } else {
            $this->productSort();

            $nbProducts = (int)FlashSale::getNbProducts($this->id_flash_sale);
            $this->pagination($nbProducts);

            $products = FlashSale::getAllProducts((int)$this->context->language->id, $this->id_flash_sale, $this->filter, false, $this->p - 1, $this->n, $this->orderBy, $this->orderWay);

            if (version_compare(_PS_VERSION_, '1.6', '>')) {
                $this->addColorsToProductList($products);
            }
            $this->context->smarty->assign(array(
                'products' => $products,
                'nbProducts' => $nbProducts,
                'request' => $this->context->link->getModuleLink('flashsales', 'page', array('id_flash_sale' => $this->id_flash_sale)),
                'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
                'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
                'comparator_max_item' => Configuration::get('PS_COMPARATOR_MAX_ITEM')
            ));
        }
    }

    public function initFlashSaleListing()
    {
        $flash_sales = FlashSale::getFlashSales((int)$this->context->language->id, $this->filter);
        if (is_array($flash_sales) && count($flash_sales)) {
            foreach ($flash_sales as $key => $flash_sale) {
                if (!($products = $this->module->getProducts($this->filter, (int)$flash_sale['id_flash_sale']))) {
                    unset($flash_sales[$key]);
                    continue;
                }

                $flash_sales[$key]['products'] = $products;
                $banner = _PS_MODULE_DIR_.$this->module->name.'/views/img/banner/'.(int)$flash_sale['id_flash_sale'].'.jpg';
                $flash_sales[$key]['banner'] = file_exists($banner) ? _MODULE_DIR_.$this->module->name.'/views/img/banner/'.(int)$flash_sale['id_flash_sale'].'.jpg' : false;
            }

            if (!count($flash_sales)) {
                $flash_sales = false;
            }
        }

        $this->context->smarty->assign(array(
            'flash_sales' => $flash_sales,
            'txt' => Configuration::get('FLASHSALE_COUNTDOWN_STRING_HOME_PAGE', (int)$this->context->language->id)
        ));
    }

    public function initContent()
    {
        parent::initContent();

        if ($this->id_flash_sale) {
            $flashSale = new FlashSale((int)$this->id_flash_sale);
            $banner_url = _PS_MODULE_DIR_.$this->module->name.'/views/img/banner/'.(int)$flashSale->id.'.jpg';
            $banner = file_exists($banner_url) ? _MODULE_DIR_.$this->module->name.'/views/img/banner/'.(int)$flashSale->id.'.jpg' : false;

            $this->context->smarty->assign(array(
                'flashsale_name' => $flashSale->name[Context::getContext()->language->id],
                'flashsale_description' => $flashSale->description[Context::getContext()->language->id],
                'flashsale_banner' => $banner
            ));
        }

        $this->context->smarty->assign(array(
            'id_flash_sale' => $this->id_flash_sale,
            'layout' => 'page'
        ));

        $template = version_compare(_PS_VERSION_, '1.7', '>=') ? 'module:flashsales/views/templates/front/1.7/page.tpl' : '1.6/page.tpl';
        $this->setTemplate($template);
    }

    public function setMedia()
    {
        parent::setMedia();

        $this->addJS(_PS_MODULE_DIR_.'flashsales/views/js/front/countdown.js');
        $this->addCSS(_PS_MODULE_DIR_.'flashsales/views/css/front/countdown.css', 'all');
        $this->addCSS(_PS_MODULE_DIR_.'flashsales/views/css/front/global.css', 'all');

        if (version_compare(_PS_VERSION_, '1.7', '<')) {
            $this->addCSS(_THEME_CSS_DIR_.'category.css');
            $this->addCSS(_THEME_CSS_DIR_.'product_list.css');
            if (Configuration::get('PS_COMPARATOR_MAX_ITEM')) {
                $this->addJS(_THEME_JS_DIR_.'products-comparison.js');
            }
        }

        foreach ($this->module->getConfigs() as $config) {
            if ($config['name'] != 'FLASHSALE_CAROUSEL') {
                continue;
            }

            foreach ($config['layouts'] as $layout) {
                $name = $config['name'].'_'.Tools::strtoupper($layout);
                if (Configuration::get($name)) {
                    $this->addJS(_PS_MODULE_DIR_.'flashsales/views/js/tools/owl.carousel.min.js');
                    $this->addCSS(_PS_MODULE_DIR_.'flashsales/views/css/tools/owl.carousel.min.css', 'all');
                    $this->addCSS(_PS_MODULE_DIR_.'flashsales/views/css/tools/owl.theme.default.min.css', 'all');
                    if (Configuration::get('FLASHSALE_PRODUCT_LIST')) {
                        if (version_compare(_PS_VERSION_, '1.7', '<')) {
                            $this->addCSS(_PS_MODULE_DIR_.'flashsales/views/css/front/miniature16.css', 'all');
                        } else {
                            $this->addCSS(_PS_MODULE_DIR_.'flashsales/views/css/front/miniature17.css', 'all');
                        }
                    }

                    break 2;
                }
            }
        }
    }
}
