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

require_once 'lib/user_input_handler_registry.php';

class MyPlugIN_EntryHandler
    implements UserInputHandler
{
    private $session;
    private $tv;

    public function __construct($session,$tv)
    {
        $this->session = $session;
	$this->tv = $tv;

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

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

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

    public function handle_user_input(&$user_input, &$plugin_cookies)
    {

        hd_print('Entry handler: handle_user_input:');
        foreach ($user_input as $key => $value)
            hd_print("  $key => $value");
		
		// force clear archive cache if we do not have archive in file system
        #$archive_dir = preg_replace('/\/plugins_data\//', '/plugins_archive/',
        #   DuneSystem::$properties['data_dir_path']) . "/ico";
        $open_folder_action = ActionFactory::open_folder();
        #if (!file_exists($archive_dir))
        #{
        #    hd_print('Clear archive is forced');
         #   $open_folder_action =
         #       ActionFactory::clear_archive_cache(
         #           null, $open_folder_action);
	#		
	#		if ($this->session->is_logged_in() === false)
	#		{
	#			hd_print('It is not logged in jet');
	#			hd_print('Clear cookies...');
	#			foreach ($plugin_cookies as $key => $value)
	#			{
	#				unset($plugin_cookies->$key);
	#			}
	#				
	#		}
	#		else
	#		{
	#			hd_print('It is logged, thereby no need to clean cookie');
	#		}
        #}
	
        if (!isset($user_input->entry_id))
            return null;

        $add_params = array(
            'entry_id' => $user_input->entry_id);
        #if ($user_input->entry_id === 'setup' ||
        #    $user_input->entry_id === 'tv')
        if ($user_input->entry_id === 'tv')
        {
            $res = $this->session->apply_sub(
                $plugin_cookies, $user_input);
            if ($res !== false)
            {
				if (!isset($res['action']))
                {
                    return ActionFactory::close_dialog_and_run(
                        $open_folder_action);
                }

                return $res['need_close_dialog'] ?
                    ActionFactory::close_dialog_and_run($res['action']) :
                    $res['action'];
            }
            else
            {
                if ($this->session->is_logged_in() == 1)
		{
                    return $open_folder_action;
		}
		if(!isset($plugin_cookies->is_login_correct))
		{
			$plugin_cookies->is_login_correct = 0;
		}
                if ($this->session->is_logged_in() === false && $plugin_cookies->is_login_correct == 1)
		{
			hd_print("unloаd channеls<");
			$this->tv->unload_channels();
		}
                if (!isset($plugin_cookies->is_login_correct) ||
                    $plugin_cookies->is_login_correct == 0)
                {
					try
					{
						$this->session->login_mylogin($plugin_cookies);
					}
					catch (Exception $e)
					{
						return $this->session->do_get_edit_subscription_action(
							$plugin_cookies, $this, $add_params);
					}
                }

                return $open_folder_action;
            }
        }
	else 
	{
		hd_print("www");
		#return $open_folder_action;
		return ActionFactory::open_folder();
	}	
        return null;
    }
}

///////////////////////////////////////////////////////////////////////////
?>
