<?php

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

class VodHistoryScreen extends AbstractPreloadedRegularScreen implements UserInputHandler
{
	const ID = 'vod_history_screen';

	public static function get_media_url_str()
	{
		return MediaURL::encode(array('screen_id' => self::ID));
	}

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

	private $vod;

	public function __construct(Vod $vod)
	{
		$this->vod = $vod;

		parent::__construct(self::ID,
			$vod->get_vod_list_folder_views(&$plugin_cookies));

		UserInputHandlerRegistry::get_instance()->register_handler($this);
	}

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

	public function get_action_map(MediaURL $media_url, &$plugin_cookies)
	{
	$actions = array();
	$actions[GUI_EVENT_KEY_ENTER]	= ActionFactory::open_folder();
	$add_action = UserInputHandlerRegistry::create_action($this, 'add_favorite');
	$add_action['caption'] = 'В Избранное';
	$actions[GUI_EVENT_KEY_D_BLUE] = $add_action;
	$add_action = UserInputHandlerRegistry::create_action($this, 'search');
	$add_action['caption'] = 'Поиск';
	$actions[GUI_EVENT_KEY_B_GREEN] = $add_action;
	$add_action = UserInputHandlerRegistry::create_action($this, 'del_item');
	$add_action['caption'] = 'Удалить';
	$actions[GUI_EVENT_KEY_C_YELLOW] = $add_action;
	return $actions;
	}

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

	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		if ($user_input->control_id == 'del_item')
		{
			if (!isset($user_input->selected_media_url))
				return null;
			$media_url = MediaURL::decode($user_input->selected_media_url);
			$history_items = HD::get_items('history_items');
			unset ($history_items[$media_url->movie_id]);
			HD::save_items('history_items', $history_items);
			$parent_media_url = MediaURL::decode($user_input->parent_media_url);
			$sel_ndx = $user_input->sel_ndx;
			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 == 'search')
		{
			if (!isset($user_input->selected_media_url))
				return null;
			$media_url = MediaURL::decode($user_input->selected_media_url);
			$defs = array();
			ControlFactory::add_text_field($defs,
			$this, $add_params=null,
			'do_new_search', '',
			$media_url->name, 0, 0, 1, 1, 1300,0,1);
			ControlFactory::add_vgap($defs, 500);
			return ActionFactory::show_dialog('Поиск', $defs, true);
		}

////////////////////
		if ($user_input->control_id == 'do_new_search')
		{
			$do_search = UserInputHandlerRegistry::create_action($this, 'do_search');
			return ActionFactory::close_dialog_and_run($do_search);
		}
		if ($user_input->control_id == 'do_search')
		{
			$media_url = MediaURL::decode($user_input->selected_media_url);
			HD::save_item('search_item', $user_input->do_new_search);
			$search_items = HD::get_items('search_items');
			$k = array_search($user_input->do_new_search, $search_items);
			if ($k !== false)
				unset ($search_items [$k]);
			array_unshift($search_items, $user_input->do_new_search);
			HD::save_items('search_items', $search_items);
			$action = ActionFactory::open_folder(DemoVodListScreen::get_media_url_str('search', $user_input->do_new_search), "Поиск: ".$user_input->do_new_search);
			return ActionFactory::invalidate_folders(array('search_screen'),$action);
		}
///////////////////
		if ($user_input->control_id == 'add_favorite')
		{
			$this->vod->ensure_favorites_loaded($plugin_cookies);
			$is_favorite = $this->vod->is_favorite_movie_id($user_input->selected_media_url);
			if ($is_favorite)
			{
				$this->vod->remove_favorite_movie($user_input->selected_media_url, $plugin_cookies);
				return ActionFactory::show_title_dialog('Удалено из Избранного');
			}
			else
			{
				$this->vod->add_favorite_movie($user_input->selected_media_url, $plugin_cookies);
				return ActionFactory::show_title_dialog('Добавленно в Избранное');
			}
		}
		return null;
	}

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

	public function get_all_folder_items(MediaURL $media_url, &$plugin_cookies)
	{
		$this->vod->folder_entered($media_url, $plugin_cookies);
		$history_items = HD::get_items('history_items');
		$items = array();
		$save_folder = HD::get_items('save_folder');
		foreach ($history_items as $movie_id => $v)
		{
			$color = 15; $new = ''; $starred = false;
			$caption = $v['name'];
			$poster_url = $v['poster_url'];
			$info = $v['info'];

			$items[] = array
			(
			PluginRegularFolderItem::media_url => VodMovieScreen::get_media_url_str($movie_id, $caption, $poster_url, $info),
			PluginRegularFolderItem::caption => $caption,
			PluginRegularFolderItem::starred => $starred,
			PluginRegularFolderItem::view_item_params => array
			(
			ViewItemParams::icon_path => $poster_url,
			ViewItemParams::item_detailed_info => $info,
			ViewItemParams::item_caption_color => $color,
			)
			);
		}
		return $items;
	}

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