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

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

class VodSeriesListScreen extends AbstractPreloadedRegularScreen implements UserInputHandler
{
	const ID = 'vod_series';

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

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

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

	private $vod;
	private $select_url = array();
	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] = ActionFactory::vod_play();
	$actions[GUI_EVENT_KEY_PLAY] = ActionFactory::vod_play();
	$add_action = UserInputHandlerRegistry::create_action($this, 'viewed');
	$add_action['caption'] = 'Просмотрено/Не просмотрено';
	$actions[GUI_EVENT_KEY_B_GREEN] = $add_action;
	$add_action = UserInputHandlerRegistry::create_action($this, 'qlt_item');
	$add_action['caption'] = 'Качество видео';
	$actions[GUI_EVENT_KEY_C_YELLOW] = $add_action;
	return $actions;
	}

/////////////////////////////////////////////////////////////////////////////////////
public function handle_user_input(&$user_input, &$plugin_cookies)
{
	if ($user_input->control_id === 'cleer_folder')
	{
		if (!isset($user_input->parent_media_url))
			return null;
		$parent_media_url = MediaURL::decode($user_input->parent_media_url);
		$sel_ndx = $user_input->sel_ndx+1;
		if ($sel_ndx < 0)
		$sel_ndx = 0;
		$range = $this->get_folder_range($parent_media_url, 0, $plugin_cookies);
		return ActionFactory::update_regular_folder($range, true, $sel_ndx);
	}
	if ($user_input->control_id == 'viewed')
	{
		if (!isset($user_input->selected_media_url))
			return null;
		$media_url = MediaURL::decode($user_input->selected_media_url);
		$m_url = MediaURL::decode($user_input->parent_media_url);
		$viewed_items = HD::get_items('viewed_items');
		$is_viewed = (array_key_exists($media_url->series_id, $viewed_items)) ? 1 : 0;
		if ($is_viewed == 1)
			unset ($viewed_items[$media_url->series_id]);
		else
			$viewed_items[$media_url->series_id] = 'viewed';
		HD::save_items('viewed_items', $viewed_items);
		$perform_new_action = UserInputHandlerRegistry::create_action($this, 'cleer_folder');
		return ActionFactory::invalidate_folders(array('vod_series'), $perform_new_action);
	}
	
	if ($user_input->control_id == 'qlt_item')
	{
		$defs = array();
		$video_quality = isset($plugin_cookies->video_quality) ? $plugin_cookies->video_quality : 4;
		$vquality = array();
		$vquality[1] = '360';
		$vquality[2] = '480';
		$vquality[3] = '720';
		$vquality[4] = '1080';
//		if (get_platform_kind_2() == "android")
//		{
//			$vquality[5] = '1440';
//			$vquality[6] = '2160 (Максимально возможное)';
//		}
		ControlFactory::add_combobox($defs, $this, null, 'video_quality', 'Качество видео:', $video_quality, $vquality, 0, $need_confirm = false, $need_apply = false);
		$do_save_sort_page = UserInputHandlerRegistry::create_action($this, 'save_sqlt_item');
		ControlFactory::add_custom_close_dialog_and_apply_buffon($defs,	'_do_save_sort_page', 'Применить', 300, $do_save_sort_page);
		return ActionFactory::show_dialog("Качество видео", $defs,	true);
	}
	if ($user_input->control_id == 'save_sqlt_item')
	{
		$plugin_cookies->video_quality = $user_input->video_quality;
		$perform_new_action = UserInputHandlerRegistry::create_action($this, 'cleer_folder');
		return ActionFactory::invalidate_folders(array('vod_series'), $perform_new_action);
	}
	return null;
}

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

public function get_all_folder_items(MediaURL $media_url, &$plugin_cookies)
{
	$items = array();
	$save_items =  false;
	$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_id))
	{
		$save_folder = HD::get_items('save_folder');
		if (isset ($save_folder[$media_url->movie_id]))
			$save_items =  array_flip($save_folder[$media_url->movie_id][$media_url->season_id]);
	}
	if (isset($movie->series_list))
	{
		$viewed_items = HD::get_items('viewed_items');
		foreach ($movie->series_list as $series)
		{
//echo("series_id from vod_series_list_screen.php............................................................." . $series->id);

			if ($series->season_id != $media_url->season_id) continue;
			$tt = $new = '';
			$color = 15;
			$ico = 'gui_skin://small_icons/movie.aai';
			$info = $series->name;
			if (isset($viewed_items[$series->id]))
			{
				if ($viewed_items[$series->id] =='viewed')
					$tt = '[ПРОСМ] ';
				else
					$tt = '[ПРОСМ ' . HD::sec_format_duration($viewed_items[$series->id][0]) . '/' . HD::sec_format_duration($viewed_items[$series->id][1]) . '] ';
				$info = "||$tt|" . $series->name;
				$color = 3;
			}
			else if (($save_items == true)&&(!isset($save_items[$series->id])))
			{
				$new = '[NEW] ';
				$info = "||$new|" . $series->name;
				$color = 19;
			}
			if ($series->img=='')
				$series_img = $movie->poster_url;
			else
				$series_img = $series->img;
			if (isset($this->select_url[$series->id]))
				$color = 18;

			$items[] = array
			(
			PluginRegularFolderItem::media_url =>
			MediaURL::encode(
			array
			(
			'screen_id' => self::ID,
			'movie_id' => $movie->id,
			'series_id'  => $series->id,
			)),
			PluginRegularFolderItem::caption => $tt . $series->name,
			PluginRegularFolderItem::view_item_params => array
			(
			ViewItemParams::icon_path => $ico,
			ViewItemParams::item_detailed_icon_path => $series_img,
			ViewItemParams::item_detailed_info => $info,
			ViewItemParams::item_caption_color => $color,
			),
			);
		}
	}
	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,
					ViewItemParams::icon_path => 'gui_skin://small_icons/movie.aai'
				),

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

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

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