<?php

require_once 'lib/user_input_handler_registry.php';

class I24hEntryHandler
    implements UserInputHandler
{
    private $session;

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

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

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

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

        if (!empty($user_input->control_id)) {

            if ($user_input->handler_id === 'entry' && $user_input->control_id === 'get_sms') {

                $res = $this->session->get_password($plugin_cookies, $user_input, $this);
                return $res['action'];
            } elseif ($user_input->handler_id === 'entry' && $user_input->control_id == 'do_login_user_by_username') {
                $res = I24HSettings::get_password($this->session,$plugin_cookies, $user_input, $handler);
                return $res['action'];
            } elseif ($user_input->handler_id === 'entry' && $user_input->control_id === 'apply_subscription') {
                $res = $this->session->apply_subscription($plugin_cookies, $user_input,$this);
                return $res['action'];
            }

        }

        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' ||
            $user_input->entry_id === 'top')
        {
            $res = $this->session->apply_subscription(
                $plugin_cookies, $user_input,$this);

            if (!$res) {
                try
                {
                    $res = $this->session->get_password(
            		$plugin_cookies, $user_input, $this);
                }
                catch (DuneException $e)
                {
                    return $e->get_error_action();
                }
            }

            if ($res !== false)
            {
                if (!isset($res['action']))
                {
                    return ActionFactory::close_dialog_and_run(
                        ActionFactory::open_folder());
                }

                return $res['need_close_dialog'] ?
                    ActionFactory::close_dialog_and_run($res['action']) :
                    $res['action'];
            }
            else
            {
                if ($this->session->is_logged_in())
                    return ActionFactory::open_folder();

                if (!$this->session->is_logged_in())
                {
                    $this->session->verify_device($plugin_cookies);

                    if (!$this->session->is_logged_in())
	                    return $this->session->show_registration_form(
//	                    return $this->session->do_get_edit_subscription_action(
	                    	$plugin_cookies, $this, $add_params);
                }
                return ActionFactory::open_folder();
            }
        }
        return null;
    }
}

?>
