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

require_once 'lib/vod/vod.php';
require_once 'lib/abstract_preloaded_regular_screen.php';
require_once 'i24h_session.php';

class VodSeriesListScreen extends AbstractPreloadedRegularScreen implements UserInputHandler
{
    const ID = 'vod_series';

    private $session;

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


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

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

    private $vod;

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

        parent::__construct(self::ID, $this->get_folder_views());

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

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

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

#        hd_print("get_action_map ".print_r($media_url,true));


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

      //  hd_print("VOD_INFO :".print_r($this->vod->get_vod_info($media_url, $plugin_cookies)));

        return array
        (
            GUI_EVENT_KEY_ENTER => $play_action,//ActionFactory::tv_play(),
            GUI_EVENT_KEY_PLAY  => $play_action//ActionFactory::tv_play(),
        );
    }

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

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

#        hd_print("get_all_folder_items media_url: ".print_r($media_url,true));

        $this->vod->folder_entered($media_url, $plugin_cookies);

        if ($this->session->get_movie_need_reloading()){
            $this->vod->clear_movie_cache();
            $this->session->set_movie_need_reloading(false);
        }

        $movie = $this->vod->get_loaded_movie($media_url->movie_id, $plugin_cookies);
        if ($movie === null)
        {
            // TODO: dialog?
            return array();
        }

        $items = array();

        //vld
        $i = 0;
        foreach ($movie->series_list as $series)
        {

         //   hd_print("movie->series_list ".print_r($series,true));


            $pb = MediaURL::decode($series->playback_url);




            $items[] = array
            (
                PluginRegularFolderItem::media_url =>
                    MediaURL::encode(
                        array
                        (
                            'screen_id' => self::ID,
                            'movie_id' => $movie->id,
                            'series_id'  => $i,
                            'channel_id' => $pb->channel_id
                        )),
                PluginRegularFolderItem::caption => $series->name,
                PluginRegularFolderItem::view_item_params => array
                (
                    ViewItemParams::icon_path => isset($series->icon) ? $series->icon : 'gui_skin://small_icons/folder.aai',
                ),
            );
            $i++;
        }

        return $items;
    }

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

    private function get_folder_views()
    {
        return array(
            array
            (
                PluginRegularFolderView::view_params => array
                (
                    //ViewParams::num_cols => 1,
                    //ViewParams::num_rows => 12,
                    ViewParams::num_cols => 1,
                    ViewParams::num_rows => 4,

                    ViewParams::background_path => I24HSettings::$BG_PIC,  // 24h.tv
                    ViewParams::background_width => 1920,
                    ViewParams::background_height => 1080,
                    ViewParams::background_order => 'before_all'
                ),

                PluginRegularFolderView::base_view_item_params => array
                (
                    ViewItemParams::item_layout => HALIGN_LEFT,
                    ViewItemParams::icon_valign => VALIGN_CENTER,
                    ViewItemParams::icon_dx => 10,
                    ViewItemParams::icon_dy => -5,
                    ViewItemParams::icon_sel_dx => 10,
                    ViewItemParams::icon_sel_dy => -5,
                    ViewItemParams::item_caption_dx => 300,
                    ViewItemParams::item_caption_dy => -30,
                    ViewItemParams::item_caption_sel_dy => -30,
                    ViewItemParams::item_caption_wrap_enabled => true,
                    ViewItemParams::icon_scale_factor => 0.8,
                    ViewItemParams::icon_path => 'gui_skin://small_icons/folder.aai'
                ),

                PluginRegularFolderView::not_loaded_view_item_params => array (),
            ),
        );
    }

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

public function handle_user_input(&$user_input, &$plugin_cookies)
{
    if ($user_input->control_id == 'play_vod') {

#        hd_print('VOD: handle_user_input:');
#        foreach ($user_input as $key => $value)
#            hd_print("  $key => $value");

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

 #       hd_print("channel id = " . $channel_url->channel_id);

        $movie = $this->vod->get_loaded_movie($channel_url->movie_id, $plugin_cookies);
        //$movie = $this->vod->get_channel($channel_url->channel_id);

  #      hd_print("channel_info " . print_r($movie->series_list[$user_input->sel_ndx], true));

        $playback_url = $movie->series_list[$user_input->sel_ndx]->playback_url;
        $channel = MediaURL::decode($playback_url);
        $defs = array();
        $tarifs = implode(", ", $channel->tariffs);
        if (($channel->streaming_url == 'passive') || !($this->session->get_has_tariffs())) {
            ControlFactory::add_label($defs, null, 'К сожалению, у Вас нет доступа к архиву канала "' . $channel->channel_name . '"!');
            ControlFactory::add_vgap($defs, 40);
            ControlFactory::add_label($defs, null, 'Для доступа к архиву канала "' . $channel->channel_name . '" зайдите в личный кабинет');
            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(''),'Управление тарифами');
    }

}

}

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