<?php

/**
 * Slick slider for Contao
 *
 * Copyright (c) 2016 Benjamin Roth
 *
 * @license LGPL-3.0+
 */

namespace eSM_slick;


/**
 * Front end content element slick slider start.
 */
class ContentSlickSliderStart extends \ContentElement
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'ce_slick_slider_start';


	/**
	 * Generate the content element
	 */
	protected function compile()
	{
		if (TL_MODE == 'BE')
		{
			$this->strTemplate = 'be_wildcard';

			/** @var \BackendTemplate|object $objTemplate */
			$objTemplate = new \BackendTemplate($this->strTemplate);

			$this->Template = $objTemplate;
			$this->Template->title = $this->headline;
		}

    $parentKey = ($this->arrData['ptable'] ?: 'tl_article') . '__' . $this->arrData['pid'];

    if (!empty($GLOBALS['TL_ESM_SLICK'][$parentKey])) {
      $GLOBALS['TL_ESM_SLICK_STACK'][$parentKey][] = $GLOBALS['TL_ESM_SLICK'][$parentKey];
    }

    $GLOBALS['TL_ESM_SLICK'][$parentKey] = $this->id;

		// Slider configuration
		$this->Template->config = $this->eSM_slick_autoPlaySpeed . ',' . $this->eSM_slick_speed . ',' . $this->eSM_slick_initialSlide . ',' . $this->eSM_slick_lazyLoad . ',' . $this->eSM_slick_infinite . ',' . $this->eSM_slick_arrows . ',' . $this->eSM_slick_pagination . ',' . $this->eSM_slick_pause . ',' . $this->eSM_slick_fade . ',' . $this->eSM_slick_slidesToShow . ',' . $this->eSM_slick_slidesToScroll . ',' . $this->eSM_slick_adaptiveHeight;

		// Background image
    if ($this->eSM_addImage && $this->multiSRC)
    {
      $source = \StringUtil::deserialize($this->multiSRC);
      $objFiles = \FilesModel::findMultipleByUuids($source);

      $imageFiles = array();
      $videoFiles = array();

      if ($objFiles !== null) {

        foreach ($objFiles as $file) {
          if (is_file(TL_ROOT . '/' . $file->path))
          {
            if (in_array($file->extension, array_map('trim', explode(',', \Config::get('validImageTypes')))))
            {
              $imageFiles[] = $file;
            } else if (in_array($file->extension, array_map('trim', array('mp4', 'm4v', 'mov', 'wmv', 'webm', 'ogv', 'm4a', 'mp3', 'wma', 'mpeg', 'wav', 'ogg'))))
            {
              $videoFiles[] = $file;
            }
          }
        }
      }

      if (isset($imageFiles[0]))
      {
        $this->singleSRC = $imageFiles[0]->path;

        $this->addImageToTemplate($this->Template, array('singleSRC' => $this->singleSRC, 'size' => $this->size));

        if ($this->eSM_slick_minHeight)
        {
          if ($this->eSM_slick_bgSize && $this->eSM_slick_bgSize == 'auto auto')
          {
            $this->Template->minHeight = $this->Template->picture['img']['height'] . 'px';
          } else
          {
            $this->Template->minHeight = round($this->Template->picture['img']['height'] / $this->Template->picture['img']['width'] * 100) . '%';
          }
        }

        $backgroundStyle = 'background-image: url(\'' . $this->Template->src . '\');';

        if ($this->eSM_slick_bgSize)
        {
          $backgroundStyle .= 'background-size: ' . $this->eSM_slick_bgSize . ';';
        } else
        {
          $backgroundStyle .= 'background-size: cover;';
        }
        if ($this->eSM_slick_bgPosition)
        {
          $backgroundStyle .= 'background-position: ' . $this->eSM_slick_bgPosition . ';';
        } else
        {
          $backgroundStyle .= 'background-position: 0 0;';
        }
        if ($this->eSM_slick_bgOpacity)
        {
          $backgroundStyle .= 'opacity:' . $this->eSM_slick_bgOpacity / 100 . ';';
        }
        if ($this->eSM_slick_bgRepeat)
        {
          $backgroundStyle .= 'background-repeat: ' . $this->eSM_slick_bgRepeat . ';';
        }

        $this->Template->backgroundStyle = $backgroundStyle;
      }

      if (count($videoFiles)) {
        $this->Template->videoFiles = $videoFiles;
      }
    }
	}
}