<?php
///////////////////////////////////////////////////////////////////////////

require_once 'lib/vod/vod.php';
require_once 'lib/abstract_preloaded_regular_screen.php';

class VodSeasonListScreen extends AbstractPreloadedRegularScreen
	implements UserInputHandler
{
	const ID = 'vod_season';

	public static function get_media_url_str($movie_id, $season_url=null)
	{
		$arr['screen_id'] = self::ID;
		$arr['movie_id'] = $movie_id;
		if ($season_url == true)
		$arr['season_url'] = $season_url;
		return MediaURL::encode($arr);
	}

	public function get_handler_id()
	{ return self::ID; }
	///////////////////////////////////////////////////////////////////////

	private $vod;

	public function __construct(Vod $vod){
		$this->vod = $vod;
		parent::__construct(self::ID, $this->get_folder_views());
		UserInputHandlerRegistry::get_instance()->
			register_handler($this);
	}

	///////////////////////////////////////////////////////////////////////

	public function get_action_map(MediaURL $media_url, &$plugin_cookies)
	{
		$actions = array();
		$actions[GUI_EVENT_KEY_ENTER]		= UserInputHandlerRegistry::create_action($this, 'add_actions');
		$actions[GUI_EVENT_KEY_PLAY]		= UserInputHandlerRegistry::create_action($this, 'add_actions');
		return $actions;
	}

	///////////////////////////////////////////////////////////////////////
	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		if ($user_input->control_id == 'add_actions')
		{
			if (!isset($user_input->selected_media_url))
				return null;
			$media_url = MediaURL::decode($user_input->selected_media_url);
			if (isset($media_url->series_id))
				return ActionFactory::vod_play();
			else
				return ActionFactory::open_folder();
		}
		return null;
	}



	///////////////////////////////////////////////////////////////////////
	public function get_all_folder_items(MediaURL $media_url, &$plugin_cookies)
	{
		$this->vod->folder_entered($media_url, $plugin_cookies);
		$movie = $this->vod->get_loaded_movie($media_url->movie_id, $plugin_cookies);
		$movie->series_list = null;
		$array_level = array_level($movie->urls);//Уровень вложенности
		if ($movie === null) return array();
/*	
$items = array();
foreach ($movie->season_list as $season)
{
foreach($movie->urls[$season->season_url] as $num_seria=>$stream)
{
$movie->add_series_data($movie->name ."_" . $season->name . "_" . $num_seria, $num_seria, $stream, false, $season->season_url);
}
$items[] = array(
PluginRegularFolderItem::media_url => VodSeriesListScreen::get_media_url_str($movie->id, $season->season_url),
PluginRegularFolderItem::caption => $season->name,
PluginRegularFolderItem::view_item_params => array(ViewItemParams::icon_path => 'gui_skin://small_icons/folder.aai'),
);
}
return $items;
*/
	
		if (isset($media_url->season_url))
		{
			$arr = $movie->urls[$media_url->season_url];//сезоны+стримы
		}
		else
		{
			$arr = $movie->season_list;	//дубляжи
		}
		$items = array();
		foreach ($arr as $k => $ozvuchka)
		{
			if (isset($ozvuchka->name) && $array_level >= 3)
			{			
				$season_url = $ozvuchka->name;
				$name = $ozvuchka->name;
				$img = 'gui_skin://small_icons/folder.aai';
				$mUrl = self::get_media_url_str($movie->id, $season_url);
			}			
			else if (isset($ozvuchka->name) && $array_level == 2)//Без сезонов
			{
				foreach($movie->urls[$ozvuchka->season_url] as $num_seria=>$stream)
				{
				$movie->add_series_data($movie->name ."_" . $ozvuchka->name . "_" . $num_seria, $num_seria, $stream, false, $ozvuchka->season_url);
				}
				$img = 'gui_skin://small_icons/folder.aai';
				$mUrl = VodSeriesListScreen::get_media_url_str($movie->id, $ozvuchka->season_url);
				$name = $ozvuchka->name;
			}
			
			else
			{
				$img = 'gui_skin://small_icons/folder.aai';
				foreach ($ozvuchka as $s=>$stream)
				{
					$seria = $s;
					$name=$k;
					$movie->add_series_data($movie->name ."_" . $media_url->season_url . "_" . $name . "_" . $s, $seria, $stream, false, $name);
				}
				$mUrl = VodSeriesListScreen::get_media_url_str($movie->id, $name);
			}
			$items[] = array
			(
			PluginRegularFolderItem::media_url => $mUrl,
			PluginRegularFolderItem::caption => $name,
			PluginRegularFolderItem::view_item_params => array
			(
			ViewItemParams::icon_path => $img,
			ViewItemParams::item_detailed_icon_path => $movie->poster_url,
			ViewParams::background_path=> "plugin_file://icons/bg4.jpg",
			),
			);
		}
		return $items;
	}


	///////////////////////////////////////////////////////////////////////
	public function get_all_folder_items_OLD(MediaURL $media_url, &$plugin_cookies)
	{

		$this->vod->folder_entered($media_url, $plugin_cookies);
		$movie = $this->vod->get_loaded_movie($media_url->movie_id, $plugin_cookies);
		if ($movie === null) return array();
		if (isset($media_url->season_url))
		{
			if (is_object($media_url->season_url))
			{
				foreach ($media_url->season_url as $k=>$sezon)
				{
					if (preg_match("|сезон|", $k))
					{
						foreach ($sezon as $seria=>$url)
						$arr[$k][$seria] = $url;
					}
					else
						$arr[$k] = $sezon;
				}
			}		
			else
			{
				foreach ($movie->series_list as $a)
				{
					if ($a->season_id == $media_url->season_url)
					$arr[$a->name] = $a->playback_url;
				}
			}
		}
		else
		{
			$arr = $movie->season_list;
		}
		$items = array();
		
		foreach ($arr as $k => $series)
		{
			if (!isset($media_url->season_url))
			{
				$season_url = $series->season_url;
				$name = $series->name;
			}
			else
			{
				$season_url = $series;
				$name = $k;
			}		
			//if (is_array($season_url))
			if (is_array($season_url) || preg_match("|сезон|", $season_url))
			{
				$img = 'gui_skin://small_icons/folder.aai';
				$mUrl = self::get_media_url_str($movie->id, $season_url);
			}
			else
			{
				$mUrl = MediaURL::encode(array('screen_id' => self::ID,'movie_id' => $movie->id,'series_id'  => $season_url));
				$img = 'gui_skin://small_icons/movie.aai';
				$movie->add_series_data($season_url,$name,$season_url,false);
			}
			
			$items[] = array
			(
			PluginRegularFolderItem::media_url => $mUrl,
			PluginRegularFolderItem::caption => $name,
			PluginRegularFolderItem::view_item_params => array
			(
			ViewItemParams::icon_path => $img,
			ViewItemParams::item_detailed_icon_path => $movie->poster_url,
			ViewParams::background_path=> "plugin_file://icons/bg4.jpg",
			),
			);
		}
		return $items;
	}

	///////////////////////////////////////////////////////////////////////

	private function get_folder_views()
	{
		return array(
		array
		(
		PluginRegularFolderView::async_icon_loading => true,
		PluginRegularFolderView::view_params => array
		(
			ViewParams::num_cols => 1,
			ViewParams::num_rows => 12,
			ViewParams::paint_details => true,
			ViewParams::paint_item_info_in_details => true,
			ViewParams::item_detailed_info_auto_line_break => true,
			ViewParams::item_detailed_info_title_color => 10,
			ViewParams::item_detailed_info_text_color => 15,
			ViewParams::background_path=> "plugin_file://icons/bg4.jpg",
			ViewParams::background_order => 0,
			ViewParams::background_height => 1080,
			ViewParams::background_width => 1920,
			ViewParams::optimize_full_screen_background => true,
		),

		PluginRegularFolderView::base_view_item_params => array
		(
			ViewItemParams::item_layout => HALIGN_LEFT,
			ViewItemParams::icon_valign => VALIGN_CENTER,
			ViewItemParams::icon_dx => 10,
			ViewItemParams::icon_dy => -5,
			ViewItemParams::item_caption_dx => 60,
		),

		PluginRegularFolderView::not_loaded_view_item_params => array (),
		),
		);
	}

	public function get_archive(MediaURL $media_url)
	{
		return $this->vod->get_archive($media_url);
	}
}

///////////////////////////////////////////////////////////////////////////