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

require_once 'lib/utils.php';
require_once 'lib/folder_view_params.php';
require_once 'lib/default_dune_plugin.php';
require_once 'lib/control_factory_ext.php';
require_once 'lib/osd_component_factory.php';
require_once 'lib/tv/tv_group_list_screen.php';
require_once 'lib/tv/tv_channel_list_screen.php';
require_once 'lib/tv/tv_favorites_screen.php';
require_once 'lib/vod/vod_movie_screen.php';
require_once 'lib/vod/vod_season_list_screen.php';
require_once 'lib/vod/vod_favorites_screen.php';
require_once "lib/vod/vod_history_screen.php";
require_once 'freetv_channel.php';
require_once 'freetv_config.php';
require_once 'freetv_tv.php';
require_once 'freetv_vod.php';
require_once "freetv_vod_category.php";
require_once "freetv_vod_category_list_screen.php";
require_once 'freetv_setup_screen.php';
require_once 'freetv_search_screen.php';

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

class PluginInputHandler implements UserInputHandler
{
	public $plugin_instance;

	public function __construct(&$instance)
	{
		$this->plugin_instance = $instance;
		UserInputHandlerRegistry::get_instance()->register_handler($this);
	}

	public function get_handler_id()
	{ return 'main_handler'; }


	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		static $load_once;
		if (!isset($plugin_cookies->show_vod_ep))
			//$plugin_cookies->show_vod_ep = 'no';
			$plugin_cookies->show_vod_ep = 'yes';
			
		if (!isset($plugin_cookies->show_vod_section))
			//$plugin_cookies->show_vod_section = 'no';
			$plugin_cookies->show_vod_section = 'yes';

		if ($plugin_cookies->show_vod_ep == 'yes')
		{
			if ((!MEDIATEKA_INPUT_SHOW && ($plugin_cookies->show_vod_section <> 'no')) ||
				(MEDIATEKA_INPUT_SHOW && ($plugin_cookies->show_vod_section <> 'yes')))
			{
				$plugin_cookies->show_vod_section = MEDIATEKA_INPUT_SHOW? 'yes' : 'no';

				return
					ActionFactory::launch_media_url("plugin_launcher://{name=" . DuneSystem::$properties['plugin_name'] . "}{ep_media_url={$user_input->action_id}}");
			}
		}
		else if ($plugin_cookies->show_vod_section <> 'no')
			$plugin_cookies->show_vod_section == 'no';

		if (!isset($user_input->control_id))
			return null;

		switch ($user_input->control_id)
		{
			case 'plugin_entry':
				if (isset($user_input->action_id) && ($user_input->action_id == 'open_setup'))
					return ActionFactory::open_folder(FreeTvSetupScreen::get_media_url_str());

				if (isset($user_input->action_id) && ($user_input->action_id == 'launch_vod'))
					if (!MEDIATEKA_AVAILABLE)
						return FreeTvConfig::show_warning_mediateka_not_available();

				$fw = get_parsed_firmware_ver();

				if (file_exists("/system/dunehd/init") && $fw['branch_num'] >= 21)
				{
				echo ("BEGIN ACTIVATE EXTRA");
					$successful_action = UserInputHandlerRegistry::create_action($this, 'pf_check_successful', array('action_id' => $user_input->action_id));
					$failed_action = UserInputHandlerRegistry::create_action($this, 'pf_check_failed', array('action_id' => $user_input->action_id));

					return
						ActionFactory::run_builtin('pf_check', array('extra_text1' => 'Для воспроизведения стримов части ТВ-каналов требуется функционал Dune Extra!'), $successful_action, $failed_action);
				}

			case 'pf_check_successful':
			case 'pf_check_failed':
				return UserInputHandlerRegistry::create_action($this, $user_input->action_id);

			case 'launch_tv':
				$media_url = TvGroupListScreen::get_media_url_str();
				$this->plugin_instance->tv->load_channels($plugin_cookies);
				
				if (isset($plugin_cookies->show_tv) && ($plugin_cookies->show_tv == 'yes'))
					return ActionFactorySafe::tv_play();
				else
					return ActionFactory::open_folder($media_url);

			case 'launch_vod':
				if (empty($media_url))
					$media_url = VodCategoryListScreen::get_media_url_str();
				if (empty($load_once))
				{
					$this->plugin_instance->tv->load_channels($plugin_cookies);
					$load_once = true;
				}
				return ActionFactory::open_folder($media_url);
		}
	}
}

class FreeTvPlugin extends DefaultDunePlugin
{
	public $tv;
	public $vod;

	public function __construct()
	{
		$this->add_screen(new FreeTvSetupScreen());

		$this->tv = new DemoTv();
		$this->add_screen(new TvGroupListScreen($this->tv, FreeTvConfig::GET_TV_GROUP_LIST_FOLDER_VIEWS()));
		$this->add_screen(new TvChannelListScreen($this->tv, FreeTvConfig::GET_TV_CHANNEL_LIST_FOLDER_VIEWS()));
		$this->add_screen(new TvFavoritesScreen($this->tv, FreeTvConfig::GET_TV_CHANNEL_LIST_FOLDER_VIEWS()));

		$this->vod = new DemoVod();
		$this->add_screen(new VodSearchScreen($this->vod));
		$this->add_screen(new VodFavoritesScreen($this->vod));
		$this->add_screen(new VodCategoryListScreen());
		$this->add_screen(new VodListScreen($this->vod));
		$this->add_screen(new VodMovieScreen($this->vod));
		$this->add_screen(new VodSeriesListScreen($this->vod));
		$this->add_screen(new VodSeasonListScreen($this->vod));
		$this->add_screen(new VodOthetScreen($this->vod));
		$this->add_screen(new VodBioScreen($this->vod));
		$this->add_screen(new VodHistoryScreen($this->vod));

		new PluginInputHandler($this);
	}

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

	private function get_tv_folder_views()
	{
		return FreeTvConfig::GET_TV_CHANNEL_LIST_FOLDER_VIEWS();
	}
}

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