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

require_once 'lib/abstract_preloaded_regular_screen.php';

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

class TvChannelListScreen extends AbstractPreloadedRegularScreen
    implements UserInputHandler
{
    const ID = 'tv_channel_list';

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

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

    protected $tv;

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

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

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

    public function get_action_map(MediaURL $media_url, &$plugin_cookies)
    {

        $play_action =
            UserInputHandlerRegistry::create_action(
                $this, 'play_tv');

        $actions = array();
        $actions[GUI_EVENT_KEY_ENTER] = $play_action;//ActionFactory::tv_play();
        $actions[GUI_EVENT_KEY_PLAY] = $play_action;//ActionFactory::tv_play();

        if ($this->tv->is_favorites_supported())
        {
            $add_favorite_action =
                UserInputHandlerRegistry::create_action(
                    $this, 'toggle_favorite');
            $add_favorite_action['caption'] = 'Избранное';

            $popup_menu_action =
                UserInputHandlerRegistry::create_action(
                    $this, 'popup_menu');

            $actions[GUI_EVENT_KEY_D_BLUE] = $add_favorite_action;
            $actions[GUI_EVENT_KEY_POPUP_MENU] = $popup_menu_action;
        }

        $actions[GUI_EVENT_KEY_INFO] =
            UserInputHandlerRegistry::create_action(
                $this, 'info');

        return $actions;
    }

    public function get_handler_id()
    { return self::ID; }

    private function get_sel_item_update_action(&$user_input, &$plugin_cookies)
    {
        $parent_media_url = MediaURL::decode($user_input->parent_media_url);
        $sel_ndx = $user_input->sel_ndx;
        $group = $this->tv->get_group($parent_media_url->group_id);
        $channels = $group->get_channels($plugin_cookies);

        $items[] = $this->get_regular_folder_item($group,
            $channels->get_by_ndx($sel_ndx), $plugin_cookies);
        $range = HD::create_regular_folder_range($items,
            $sel_ndx, $channels->size());

        return ActionFactory::update_regular_folder($range, false);
    }

    public function handle_user_input(&$user_input, &$plugin_cookies)
    {
#        hd_print('Tv favorites: handle_user_input:');
#        foreach ($user_input as $key => $value)
#            hd_print("  $key => $value");

        if ($user_input->control_id == 'info')
        {
            if (!isset($user_input->selected_media_url))
                return null;

            $media_url = MediaURL::decode($user_input->selected_media_url);
            $channel_id = $media_url->channel_id;

            $channels = $this->tv->get_channels();
            $c = $channels->get($channel_id);
            $id = $c->get_id();
            $title = $c->get_title();

            return ActionFactory::show_title_dialog("ТВ-канал '$title' (id=$id)");
        }
        else if ($user_input->control_id == 'play_tv')
        {
            $media_url = MediaURL::decode($user_input->selected_media_url);
            $channel_id = $media_url->channel_id;
            $channels = $this->tv->get_channels();
            $c = $this->tv->get_channel($channel_id);

            if ($c->get_streaming_url() == "passive"){

//                hd_print("Check tv to passive");

                $channels = $this->tv->get_channels();
                $c = $channels->get($channel_id);
                $title = $c->get_title();

                $defs = array();
                $tarifs = implode(", ", $c->get_tariffs($channel_id));

                ControlFactory::add_label($defs, null, 'К сожалению, у Вас нет доступа к каналу "'.$title.'"!');
                ControlFactory::add_vgap($defs, 40);
                ControlFactory::add_label($defs, null, 'Для доступа к каналу "'.$title.'" зайдите в личный кабинет');
                ControlFactory::add_label($defs, null, 'и выберите один из следующих тарифов:');
                ControlFactory::add_vgap($defs, 40);
                ControlFactory::add_label($defs, null, $tarifs);
                ControlFactory::add_vgap($defs, 40);
                ControlFactory::add_label($defs, null, 'Для выбора тарифа, нажмите кнопку "Подключить".');
                ControlFactory::add_vgap($defs, 40);
                ControlFactory::add_button_close($defs, $this, array('button_caption_centered'=>true), 'to_tariffs', null, ' Подключить', 300);
                ControlFactory::add_button_close($defs, $this, array('button_caption_centered'=>true), 'cancel', null, '    Отмена', 300);

                return ActionFactory::show_dialog('Внимание!', $defs);

            }else{
                return ActionFactory::tv_play();
            }

        }
        else if ($user_input->control_id == 'to_tariffs'){
            return ActionFactory::open_folder(I24hPrivateScreen::get_media_url_str(''),'Управление тарифами');
        }
        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);
            $channel_id = $media_url->channel_id;

            $is_favorite = $this->tv->is_favorite_channel_id(
                $channel_id, $plugin_cookies);
            $caption = $is_favorite ?
                'Удалить из избранного' : 'Добавить в избранное';
            $add_favorite_action =
                UserInputHandlerRegistry::create_action(
                    $this, 'toggle_favorite');
            $menu_items[] = array(
                GuiMenuItemDef::caption => $caption,
                GuiMenuItemDef::action => $add_favorite_action);

            return ActionFactory::show_popup_menu($menu_items);
        }
        else if ($user_input->control_id == 'toggle_favorite')
        {
            if (!isset($user_input->selected_media_url))
                return null;

            $media_url = MediaURL::decode($user_input->selected_media_url);
            $channel_id = $media_url->channel_id;

            $is_favorite = $this->tv->is_favorite_channel_id(
                $channel_id, $plugin_cookies);
            if ($is_favorite)
            {
                $this->tv->change_tv_favorites(PLUGIN_FAVORITES_OP_REMOVE,
                    $channel_id, $plugin_cookies);

                return ActionFactory::show_title_dialog(
                    'Канал удален из избранного',
                    $this->get_sel_item_update_action(
                        $user_input, $plugin_cookies));
            }
            else
            {
                $this->tv->change_tv_favorites(PLUGIN_FAVORITES_OP_ADD,
                    $channel_id, $plugin_cookies);

                return ActionFactory::show_title_dialog(
                    'Канал добавлен в избранное',
                    $this->get_sel_item_update_action(
                        $user_input, $plugin_cookies));
            }
        }

        return null;
    }

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

    private function get_regular_folder_item($group, $c, &$plugin_cookies)
    {
        return array
        (
            PluginRegularFolderItem::media_url =>
                MediaURL::encode(
                    array(
                        'channel_id' => $c->get_id(),
                        'group_id' => $group->get_id())),
            PluginRegularFolderItem::caption => $c->get_title(),
            PluginRegularFolderItem::view_item_params => array
            (
                ViewItemParams::icon_path => $c->get_icon_url(),
                ViewItemParams::item_detailed_icon_path => $c->get_icon_url(),
            ),
            PluginRegularFolderItem::starred =>
                $this->tv->is_favorite_channel_id(
                    $c->get_id(), $plugin_cookies),
        );
    }

    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);

        $group = $this->tv->get_group($media_url->group_id);

        $items = array();

        foreach ($group->get_channels($plugin_cookies) as $c)
        {
            $tvc = $this->get_regular_folder_item(
                $group, $c, $plugin_cookies);
            $items[] = $tvc;
//            hd_print(print_r($tvc,true));
        }

        return $items;
    }

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

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