<?php

require_once 'lib/default_dune_plugin.php';
require_once 'lib/utils.php';
require_once 'lib/tv/tv_group_list_screen.php';
require_once 'lib/tv/tv_favorites_screen.php';
require_once 'lib/vod/vod_list_screen.php';
require_once 'lib/vod/vod_movie_screen.php';
require_once 'lib/vod/video_movie_screen.php';
require_once 'lib/vod/vod_series_list_screen.php';
require_once 'lib/vod/vod_favorites_screen.php';
require_once 'lib/vod/vod_search_screen.php';
require_once 'i24h_config.php';
require_once 'i24h_exception.php';
require_once 'i24h_tv.php';
require_once 'i24h_m3u_tv.php';
require_once 'i24h_vod.php';
require_once 'i24h_setup_screen.php';
require_once 'i24h_tv_channel_list_screen.php';
require_once 'i24h_vod_category_list_screen.php';
require_once 'i24h_vod_list_screen.php';
require_once 'i24h_session.php';
require_once 'i24h_entry_handler.php';
require_once 'i24h_top_screen.php';
require_once 'i24h_topcat_list_screen.php';
require_once 'i24h_video_list_screen.php';
require_once 'i24h_amedia_list_screen.php';
require_once 'i24h_ctprimera_list_screen.php';
require_once 'i24h_megogo_list_screen.php';
require_once 'i24h_megogo_kids_list_screen.php';
require_once 'i24h_private_screen.php';
require_once 'i24h_add_packets.php';
require_once 'i24h_device_list.php';
require_once 'i24h_search.php';
require_once 'i24h_vod_series_list.php';
require_once 'i24h_video_serial_info.php';

if (!empty(I24HSettings::$UTV_ID)) {
    require_once 'i24h_utv_list_screen.php';
}


class I24hPlugin extends DefaultDunePlugin
{
    private $session;
    private $entry_handler;
    private $amedia;
    private $ctprimera;
    private $megogo;
    private $serial_info;

    public function __construct()
    {
        $this->session = new I24hSession($this);
        $this->entry_handler = new I24hEntryHandler($this->session);
        $this->tv = new I24hTv($this->session);
        $this->vod = new I24hVod($this->session, $this->tv);
        $this->serial_info = new I24hVideoSerialInfo($this->session, $this->tv);

        $this->tv->vod = $this->vod;
        if (!I24hConfig::USE_M3U_FILE)
        {
            $this->add_screen(new TvGroupListScreen($this->tv,
                    I24hConfig::GET_TV_GROUP_LIST_FOLDER_VIEWS()));
        }
        $this->add_screen(new I24hTvChannelListScreen($this->session, $this->tv,
                I24hConfig::GET_TV_CHANNEL_LIST_FOLDER_VIEWS()));
        $this->add_screen(new TvFavoritesScreen($this->tv,
                I24hConfig::GET_TV_CHANNEL_LIST_FOLDER_VIEWS()));
        $this->add_screen(new I24hSetupScreen($this, $this->session));
        $this->add_screen(new I24hVodListScreen($this->vod, $this->session, $this->tv));
        $this->add_screen(new VodMovieScreen($this->vod));
        $this->add_screen(new VideoMovieScreen($this->serial_info));
        $this->add_screen(new VodSeriesListScreen($this->session,$this->vod));
        $this->add_screen(new VodSearchScreen($this->vod));
        
        $this->add_screen(new I24hTopcatListScreen($this->session));
        $this->add_screen(new I24hPrivateScreen($this));
        $this->add_screen(new I24hAddPacketScreen($this));
        $this->amedia = new I24hAmediaListScreen($this->session,'amedia_list');
        $this->add_screen($this->amedia);
        $this->ctprimera = new I24hCtprimeraListScreen($this->session,'ctprimera_list');
        $this->add_screen($this->ctprimera);
        $this->megogo = new I24hMegogoListScreen($this->session,'megogo_list');
        $this->add_screen($this->megogo);
        if (!empty(I24HSettings::$MEGOGO_KIDS_VS_ID)) {
            $this->megogokids = new I24hMegogoKidsListScreen($this->session, 'megogo_kids_list');
            $this->add_screen($this->megogokids);
        }
        if (!empty(I24HSettings::$UTV_ID)) {
            $this->utv = new I24hUtvListScreen($this->session, 'utv_list');
            $this->add_screen($this->utv);
        }

        $this->add_screen(new I24hTopScreen($this->session, $this->amedia, $this->vod));
        $this->add_screen(new I24hSearchScreen($this->session, $this->amedia, $this->vod));
        $this->add_screen(new I24hDeviceListScreen($this,$this->session));
        $this->add_screen(new I24hVodSeriesListScreen($this->session, $this->amedia, $this->vod));
    }

    public function unset_login()
    {
        $this->session->unset_login();
        $this->tv->unload_channels();
    }
    public function get_session($plugin_cookies)
    {
    	return $this->session;
    }
    public function get_tv()
    {
    	return $this->tv;
    }
    public function get_vod()
    {
    	return $this->vod;
    }
    public function get_amedia()
    {
    	return $this->amedia;
    }
    
    public function get_vod_info($media_url_str, &$plugin_cookies)
    {

//        hd_print("PLUGINS get_vod_info");

        if (is_null($this->vod))
            throw new Exception('VOD is not supported');

//        hd_print(" >> ".$media_url_str);
        $media_url = MediaURL::decode($media_url_str);
        return $this->amedia->get_vod_info($media_url, $plugin_cookies);
    }

    public function get_tv_info($media_url_str, &$plugin_cookies)
    {
        if (is_null($this->tv))
            throw new Exception('TV is not supported');
        $media_url = MediaURL::decode($media_url_str);
        $ret = $this->tv->get_tv_info($media_url, $plugin_cookies);
        if (($media_url->screen_id == "vod_movie") || ($media_url->screen_id == "vod_series")) {
            $movie = $this->vod->get_loaded_movie($media_url->movie_id, $plugin_cookies);
            if (isset($movie)) {
            	$this->tv->save_history($media_url->movie_id, $plugin_cookies);
            }
        }
        return $ret;
    }
    
}

?>
