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

require_once 'lib/abstract_preloaded_regular_screen.php';

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

class TvGroupListScreen extends AbstractPreloadedRegularScreen implements UserInputHandler
{

public function get_handler_id() { return "tv"; }

	const ID = 'tv_group_list';

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

	protected $tv;

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

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



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

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

	public function get_action_map(MediaURL $media_url, &$plugin_cookies)
	{
		$actions = array();
		$actions[GUI_EVENT_KEY_ENTER] = UserInputHandlerRegistry::create_action($this, 'enter');
		$actions[GUI_EVENT_KEY_PLAY] = ActionFactory::tv_play();
		$open_settings = ActionFactory::open_folder(FreeTvSetupScreen::get_media_url_str());
		$open_settings['caption'] = 'Настройки';
		$actions[GUI_EVENT_KEY_B_GREEN] = $open_settings;

		$add_action = ActionFactorySafe::open_folder(TvChannelListScreen::get_media_url_str(14), 'Корзина');//UserInputHandlerRegistry::create_action($this, 'show_bin');
		$add_action['caption'] = "Корзина";
		$actions[GUI_EVENT_KEY_C_YELLOW] = $add_action;

		$popup_menu_action = UserInputHandlerRegistry::create_action($this, "popup_menu");
		$actions[GUI_EVENT_KEY_POPUP_MENU] = $popup_menu_action;


		return $actions;
	}

	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		if ($user_input->control_id == 'enter')
		{
			if (stripos($user_input->selected_media_url, 'vod_category_list') && !MEDIATEKA_AVAILABLE)
				return FreeTvConfig::show_warning_mediateka_not_available();

			return
				ActionFactory::open_folder();
		}
		else if ($user_input->control_id == 'popup_menu')
		{
			if (!isset($user_input->selected_media_url))
				return null;
			$media_url = MediaURL::decode($user_input->selected_media_url);
			if ($media_url->screen_id != "tv_channel_list")
				return null;
			$group_id = strval($media_url->group_id);
			$group_title=$this->tv->get_group($group_id)->get_title($plugin_cookies);
			if ($group_title == "Корзина" || $group_id == "__all_channels")
				return null;

			$hide_category_action = UserInputHandlerRegistry::create_action($this, "hide_category");
			$hide_category_caption = "Удалить в корзину все каналы категории $group_title";
			$menu_items[] = array(
							GuiMenuItemDef::caption => $hide_category_caption,
							GuiMenuItemDef::action => $hide_category_action);

			$hide_category_action = UserInputHandlerRegistry::create_action($this, "cancel");
			$menu_items[] = array(
							GuiMenuItemDef::caption => "Отмена",
							GuiMenuItemDef::action => $hide_category_action);

			return
				ActionFactory::show_popup_menu($menu_items);
		}

		else if ($user_input->control_id == 'hide_category')
		{
			if (!isset($user_input->selected_media_url))
				return null;
			$media_url = MediaURL::decode($user_input->selected_media_url);
			$group_id = strval($media_url->group_id);
			$group_title=$this->tv->get_group($group_id)->get_title($plugin_cookies);
			//каналы на удаление
			$channels=$this->tv->get_group($group_id)->get_channels($plugin_cookies);

			foreach ($channels as $k)
			{
				$channel_id=$k->get_id($plugin_cookies);
				$exclude_channel_ids = $this->tv->get_exclude_channel_ids($plugin_cookies);
				array_push($exclude_channel_ids, $channel_id);
				$this->tv->set_exclude_channel_ids($plugin_cookies, $exclude_channel_ids);
			}

			return
				ActionFactorySafe::invalidate_folders(array(self::get_media_url_str()));
		}

		return null;
	}

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

	public function get_all_folder_items(MediaURL $media_url, &$plugin_cookies)
	{
		$this->tv->folder_entered($media_url, $plugin_cookies);
		$this->tv->ensure_channels_loaded($plugin_cookies);
		$exclude_channel_ids = $this->tv->get_exclude_channel_ids($plugin_cookies);
		$items = array();
		//$channels_in_group = array();

		foreach ($this->tv->get_groups() as $group)
		{
			$group_id = $group->get_id();

			if (strval($group->get_title()) == "Корзина")
				continue;

			//if (!isset($plugin_cookies->show_bin))
			//	$plugin_cookies->show_bin = "yes";

			//if (($plugin_cookies->show_bin == "no") && (strval($group->get_title()) == "Корзина"))
			//	continue;

			if (($group_id != "__favorites")/* && ($group_id != "__all_channels")*/)
			{
				$channels_in_group = array();
				$channels = $group->get_channels($plugin_cookies);

				foreach($channels as $c)
					$channels_in_group[] = $c->get_id();

				if (!count(array_diff($channels_in_group, $exclude_channel_ids)))
					continue;
			}

			//if ($group_id == $this->tv->get_all_channel_group_id())
			//	continue;

			$items[] =
				array
				(
					PluginRegularFolderItem::media_url => $group->is_favorite_channels() ? TvFavoritesScreen::get_media_url_str() : TvChannelListScreen::get_media_url_str($group->get_id()),
					PluginRegularFolderItem::caption => $group->get_title(),
					PluginRegularFolderItem::view_item_params =>
						array
						(
							ViewItemParams::icon_path => $group->get_icon_url(),
							ViewItemParams::item_detailed_icon_path => $group->get_icon_url()
						)
				);
		}

		$fw = get_parsed_firmware_ver();

		if ($fw['branch_num'] >= 11)
			//if (MEDIATEKA_INPUT_SHOW && ($plugin_cookies->show_vod_ep == 'no'))
			if (MEDIATEKA_INPUT_SHOW)
				$this->tv->add_special_groups($items);

		return $items;
	}

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

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