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

require_once 'lib/abstract_controls_screen.php';

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

class FreeTvSetupScreen extends AbstractControlsScreen
{
	const ID = 'setup';

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

	public function __construct()
	{
		parent::__construct(self::ID);
	}

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

	public function do_get_control_defs(&$plugin_cookies)
	{
		$defs = array();
		$clear_epg_cache = true;
		$show_over_proxy = isset($plugin_cookies->show_over_proxy)? $plugin_cookies->show_over_proxy : '';
		$show_tv = isset($plugin_cookies->show_tv)? $plugin_cookies->show_tv : 'no';
		$show_vod_ep = isset($plugin_cookies->show_vod_ep)? $plugin_cookies->show_vod_ep : 'yes';
		$ext_epg = isset($plugin_cookies->ext_epg)? $plugin_cookies->ext_epg : 'yes';
		$epg_shift_peerstv = isset($plugin_cookies->epg_shift_peerstv)? $plugin_cookies->epg_shift_peerstv : '0';
		$epg_shift_vsetv = isset($plugin_cookies->epg_shift_vsetv)? $plugin_cookies->epg_shift_vsetv : '0';
		$epg_font_size = isset($plugin_cookies->epg_font_size)? $plugin_cookies->epg_font_size : 'SMALL';
		$pass_sex = isset($plugin_cookies->pass_sex)? $plugin_cookies->pass_sex : '0000';

		for ($i = -12; $i<13; $i++)
			$shift_ops[$i*3600] = $i;

		ControlFactory::add_vgap($defs, -20);
		$this->add_label($defs, 'Версия плагина:', strval(simplexml_load_string(file_get_contents(DuneSystem::$properties['install_dir_path'] . "/dune_plugin.xml"))->version));
		ControlFactory::add_vgap($defs, -10);
		$this->add_label($defs,  'Разработка:','andy555 & ddaaff ');
		ControlFactory::add_vgap($defs, 30);


		//$this->add_combobox($defs, 'show_tv', 'Автозапуск воспроизведения:', $show_tv, $show_ops, 0, true);
		ControlFactory::add_button($defs, $this, array('show_tv' => ($show_tv == 'yes')? 'no' : 'yes'), 'show_tv', 'Автозапуск воспроизведения ТВ:', ($show_tv == 'yes')? 'Включено' : 'Выключено', 0);
		$fw = get_parsed_firmware_ver();

		if ($fw['branch_num'] >= 11)
			ControlFactory::add_button($defs, $this, array('show_vod_ep' => ($show_vod_ep == 'yes')? 'no' : 'yes'), 'show_vod_ep', 'Отображать иконку фильмотеки вне приложения:', ($show_vod_ep == 'yes')? 'Включено' : 'Выключено', 0);

		ControlFactory::add_vgap($defs, 30);
		ControlFactory::add_button($defs, $this, array('ext_epg' => ($ext_epg == 'yes')? 'no' : 'yes'), 'ext_epg', 'Показывать расширенный EPG:', ($ext_epg == 'yes')? 'Включено' : 'Выключено', 0);
		ControlFactory::add_button($defs, $this, array('epg_font_size' => ($epg_font_size == 'SMALL')? 'NORMAL' : 'SMALL'), 'epg_font_size', 'Размер шрифта EPG:', ($epg_font_size == 'SMALL')? 'Уменьшенный' : 'Обычный', 0);
				
		$this->add_combobox($defs, 'epg_shift_peerstv', 'Коррекция программы от PeersTV (час):     ', $epg_shift_peerstv, $shift_ops, 0, true);
		$this->add_combobox($defs, 'epg_shift_vsetv', 'Коррекция программы от VseTV (час):', $epg_shift_vsetv, $shift_ops, 0, true);
		$this->add_button ( $defs,'clear_epg_cache', '', 'Очистить кэш EPG', 0 );
		ControlFactory::add_vgap($defs, 30);
		$this->add_button ( $defs,'restart', '', 'Перезагрузить плеер', 0 );

		return $defs;
	}

	public function get_control_defs(MediaURL $media_url, &$plugin_cookies)
	{
		return $this->do_get_control_defs($plugin_cookies);
	}

	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		if ($user_input->control_id == 'restart')
		{
			return ActionFactorySafe::stb_shell_reboot();
		}
		else if ($user_input->control_id == 'clear_epg_cache')
		{
			array_map("unlink", glob(DuneSystem::$properties['tmp_dir_path']."/*"));
			$action = null;
			$msg = "Кэш телепрограммы полностью очищен.";

			return  ActionFactory::show_title_dialog($msg, $action);
		}

		if (($user_input->action_type == 'confirm') || ($user_input->action_type == 'apply'))
		{
			$control_id = $user_input->control_id;
			$new_value = $user_input->{$control_id};

			hd_print("Setup: changing $control_id value to $new_value");

			if ($control_id == 'show_tv')
				$plugin_cookies->show_tv = $new_value;
			else if ($control_id == 'show_vod_ep')
			{
				$plugin_cookies->show_vod_ep = $new_value;

				if ($plugin_cookies->show_vod_ep == 'no')
					$plugin_cookies->show_vod_section = 'no';
				else
					$plugin_cookies->show_vod_section = MEDIATEKA_INPUT_SHOW? 'yes' : 'no';
			}
			else if ($control_id == 'ext_epg')
				$plugin_cookies->ext_epg = $new_value;
			else if ($control_id == 'media_proxy')
				$plugin_cookies->media_proxy = $new_value;
			else if ($control_id == 'epg_shift_peerstv')
				$plugin_cookies->epg_shift_peerstv = $new_value;
			else if ($control_id == 'epg_shift_vsetv')
				$plugin_cookies->epg_shift_vsetv = $new_value;
			else if ($control_id == 'epg_font_size')
				$plugin_cookies->epg_font_size = $new_value;
		}

		return ActionFactory::reset_controls($this->do_get_control_defs($plugin_cookies));
	}



}

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