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

require_once 'lib/screen.php';
require_once 'lib/vod/vod.php';
require_once 'lib/vod/vod_series_list_screen.php';

class VideoMovieScreen implements Screen, UserInputHandler
{
    const ID = 'video_movie';

    public static function get_media_url_str($prev_screen_id,$movie_id,$background = null)
    {

        self::$BACKGROUND = empty($background)?I24hConfig::BG_PIC:$background;
    	//hd_print(" !! $movie_id");
        return MediaURL::encode(
            array(
                'prev_screen_id' => $prev_screen_id,
                'screen_id' => self::ID,
                'movie_id' => $movie_id));
    }

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

    private $serial_info;
    public static $BACKGROUND;

    public function __construct(vod $serial_info)
    {
        $this->serial_info = $serial_info;
        UserInputHandlerRegistry::get_instance()->
            register_handler($this);
    }

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

    public function get_action_map(MediaURL $media_url, &$plugin_cookies)
    {
//        $actions = array();
//        $add_action = UserInputHandlerRegistry::create_action($this, 'setup');
//        $add_action['caption'] = 'Настройки';
//        $actions[GUI_EVENT_KEY_C_YELLOW] = $add_action;
//        $actions[GUI_EVENT_KEY_SETUP] = $add_action;
//        $add_action = UserInputHandlerRegistry::create_action($this, 'whats_new');
//        $add_action['caption'] = 'Изменения в v' . HD::ver();
//        $actions[GUI_EVENT_KEY_B_GREEN] = $add_action;
//        $actions[GUI_EVENT_KEY_CLEAR] = UserInputHandlerRegistry::create_action($this, 'hide_id_apply');

//        $actions[GUI_EVENT_KEY_ENTER] = UserInputHandlerRegistry::create_action($this, 'add_actions');

        $play_action =
            UserInputHandlerRegistry::create_action(
                $this, 'play_video');
        return array
        (
            GUI_EVENT_KEY_ENTER => $play_action,
            GUI_EVENT_KEY_PLAY => $play_action
        );

//        $popup_menu_action = UserInputHandlerRegistry::create_action($this, 'popup_menu');
//        $actions[GUI_EVENT_KEY_POPUP_MENU] = $popup_menu_action;
//        return  $actions;
    }


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

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

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

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

//        hd_print("get_folder_view media_url:".print_r($media_url,true));
        $this->serial_info->folder_entered($media_url, $plugin_cookies);

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

        $left_button_caption = $this->serial_info->get_movie_left_button_caption(
            $media_url, $plugin_cookies);
      //  hd_print("series url: ".$movie->series_list[0]->playback_url);
        //$pl = MediaURL::decode($movie->series_list[0]->playback_url);

        /*$left_button_action = (count($movie->series_list) > 1) ?
        	$this->vod->get_movie_left_button_action($media_url, $plugin_cookies) :

            array
        	(
        	    GuiAction::handler_string_id => PLUGIN_TV_PLAY_ACTION_ID,
        	    GuiAction::data =>
        	    array
        	    (
        	    	PluginOpenFolderActionData::media_url => $movie->series_list[0]->playback_url
        	    	)
        	    );


        */
        $left_button_action = $this->get_movie_left_button_action($media_url, $plugin_cookies);
        	//$this->serial_info->get_movie_left_button_action($media_url, $plugin_cookies);




        //$this->vod->get_movie_left_button_action(
        //    $media_url, $plugin_cookies);

        $has_right_button = false;//$this->serial_info->is_favorites_supported();
        $right_button_caption = null;
        $right_button_action = null;
        if ($has_right_button)
        {
            $this->serial_info->ensure_favorites_loaded($plugin_cookies);

            $is_favorite = $this->serial_info->is_favorite_movie_id($plugin_cookies,$movie->id);
            $right_button_caption = $is_favorite ?
                'Удалить из избранного' : 'Добавить в избранное';
            $right_button_action = UserInputHandlerRegistry::create_action(
                $this, 'favorites',
                array('movie_id' => $movie->id));
        }

        $s_mediaurl = MediaURL::encode(
            array(
                'screen_id' => $media_url->prev_screen_id,
                'serial_id' => $movie->id
            ));


//        hd_print(print_r($movie->get_movie_array(),true));

        $movie_folder_view = array
        (
            PluginMovieFolderView::movie => $movie->get_movie_array(),
//            PluginMovieFolderView::left_button_caption => 'Просмотр',
            PluginMovieFolderView::left_button_action => $left_button_caption,//UserInputHandlerRegistry::create_action($this, 'button_actions'),
//            PluginMovieFolderView::has_right_button => $has_right_button,
//            PluginMovieFolderView::right_button_caption => $right_button_caption,
//            PluginMovieFolderView::right_button_action => $right_button_action,
            PluginMovieFolderView::params => array
            (
                    PluginFolderViewParams::background_url => self::$BACKGROUND //I24hConfig::BG_PIC,  // 24h.tv
            ),
            PluginMovieFolderView::has_multiple_series =>
                true,//(count($movie->series_list) > 1),
            PluginMovieFolderView::series_media_url =>
                //VodSeriesListScreen::get_media_url_str($movie->id),
                $s_mediaurl
            
        );

        return array
        (
            PluginFolderView::multiple_views_supported  => true,
            PluginFolderView::archive                   => null,
            PluginFolderView::view_kind                 => PLUGIN_FOLDER_VIEW_MOVIE,
            PluginFolderView::data                      => $movie_folder_view,
        );
    }

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

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

        $defs = array();
        ControlFactory::add_button_close($defs, $this, array('button_caption_centered'=>true), 'cancel', null, 'OK',300);
        return ActionFactory::show_dialog('Введен неверный пароль родительского контроля!', $defs);

    }



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

        if ($user_input->control_id == 'favorites')
        {
            $movie_id = $user_input->movie_id;

            $is_favorite = $this->serial_info->is_favorite_movie_id($plugin_cookies,$movie_id);
            if ($is_favorite)
                $this->serial_info->remove_favorite_movie($movie_id, $plugin_cookies);
            else
                $this->serial_info->add_favorite_movie($movie_id, $plugin_cookies);

            $message = $is_favorite ?
                'Фильм удален из избранного' :
                'Фильм добавлен в избранное';

            return ActionFactory::show_title_dialog($message,
                ActionFactory::invalidate_folders(
                    array(
                        self::get_media_url_str($movie_id),
                        VodFavoritesScreen::get_media_url_str(),
                    )));
        }

        return null;
    }
}

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