# -*- coding: utf-8 -*-
"""
    Velocity Kodi Addon
    Copyright (C) 2016 blazetamer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from url
lib2 import Request, urlopen
import urllib2,urllib,re,os
import sys
import urlresolver
import xbmc
plugin,xbmcgui,xbmc, xbmcaddon, downloader, extract, time
import tools
from libs import kodi,trakt_auth,trakt
xbmc.log("NAMESPACE ONE "+str(dir(kodi)))
from libs import dl_control,cache_stat,watched_cache
from libs import log_utils,message
from libs import window_utils
import datetime
from libs import viewsetter
from libs import image_scraper
from t0mm0.common.net import Net
from t0mm0.common.addon import Addon
from scrapers import main_scrape,primewire,iwatchonline,icefilms
import json




ADDON = xbmcaddon.Addon(id=kodi.addon_id)
net = Net()
addon_id=kodi.addon_id
addon = Addon(addon_id, sys.argv)
artwork = xbmc.translatePath(os.path.join('special://home','addons',addon_id,'resources','art/'))
fanart = artwork+'fanart.jpg'
messages = xbmc.translatePath(os.path.join('special://home','addons',addon_id,'resources','messages/'))
execute = xbmc.executebuiltin
trakt_api=trakt.TraktAPI()


orig_ids = addon.queries.get('orig_ids', [])


#==========NEW AUTOUPDATE FUNCTION START===============
#********************AutoUpdate******************
from autoupdate import autoupdates
#Author Info Variable

autoupdates.provider_name = "Blazetamer"

#add-on variables

autoupdates.addon_id_name ='plugin.video.velocity'
autoupdates.addon_xml_loca = "https://offshoregit.com/Blazetamer/repo/raw/master/plugin.video.velocity/addon.xml"
autoupdates.addon_name ="Velocity"
autoupdates.addon_zip_loca ="https://offshoregit.com/Blazetamer/repo/raw/master/zips/plugin.video.velocity"


#repo variables

autoupdates.repo_name = "repository.BlazeRepo"
autoupdates.repo_xml_loca ="https://offshoregit.com/Blazetamer/repo/raw/master/zips/repository.BlazeRepo/addon.xml"
autoupdates.repo_zip_loca ="https://offshoregit.com/Blazetamer/repo/raw/master/zips/repository.BlazeRepo"
autoupdates.repo_entry_version="3.1"


#********************AutoUpdate END******************




def LogNotify(title,message,times,icon):
		xbmc.executebuiltin("XBMC.Notification("+title+","+message+","+times+","+icon+")")


def OPEN_URL(url):
	req=urllib2.Request(url)
	req.add_header('User-Agent', 'Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; AFTB Build/JDQ39) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')
	response=urllib2.urlopen(req)
	link=response.read()
	response.close()
	return link


def menu():
	# active_plugin = xbmc.getInfoLabel('Container.PluginName')
	# kodi.log("RUNNING ADDON IS = "+active_plugin)
	autoupdates.STARTUP()
	kodi.addDir("Movies",'','movie_menu',artwork+'movies.png','',1,'','',fanart=fanart,description="View and Search for Movies.")
	kodi.addDir("TV Shows",'','tv_menu',artwork+'tvshows.png','',1,'','',fanart=fanart,description="View and Search for TV Shows.")
	kodi.addItem("General Settings",'','display_settings',artwork+'gen_settings.png',fanart=fanart,description="Adjust The General Addon Settings.")
	kodi.addItem("Enable Providers",'','display_scraper_settings',artwork+'scraper_settings.png',fanart=fanart,description="Adjust Your Provider/Scraper Settings.")
	kodi.addItem("Trakt Settings",'','display_trakt_settings',artwork+'trakt_settings.png',fanart=fanart,description="Adjust and Set Trakt Intergration Settings.")
	kodi.addItem("Url Resolver Settings",'','resolver_settings',artwork+'resolver_settings.png',fanart=fanart,description="Adjust and Set The Url Resolver Settings.")
	kodi.addItem("Set Download Folder",'','display_download_settings',artwork+'down_settings.png',fanart=fanart,description="Set The Path For Your Downloaded Files.")
	kodi.addDir("Manage Downloads",'','viewQueue',artwork+'manage_downloads.png','',1,'','',fanart=fanart,description="View and Manage Your Downloads.")
	#kodi.addDir("VERSION TEST ",'','get_kversion',artwork+'update.png','',1,'','',fanart=fanart)

	if  kodi.get_setting('trakt_authorized') =='true':
		kodi.addDir("Clear Trakt User ",'','de_auth',artwork+'sign_out.png','',1,'','',fanart=fanart,description="Clear and Log Out Your Trakt Account.")
	if  kodi.get_setting('trakt_authorized') =='false':
		kodi.addDir("[COLOR gold]Trakt Integration[/COLOR]",messages+'trakt_auth.txt','get_pin',artwork+'trakt.png','',1,'','',fanart=fanart,description="Authorize your Trakt Account.")

	viewsetter.set_view("sets")
	#xbmc.executebuiltin("Container.SetViewMode()")

def get_kversion():
	full_version_info = xbmc.getInfoLabel('System.BuildVersion')
	baseversion = full_version_info.split(".")
	return  baseversion[0]

def movie_menu():
		media='movies'#addon(addonname).getSetting()
		kodi.addDir("Popular",'popular','call_trakt_movies',artwork+'popular.png','',1,'','',fanart=fanart,description="Popular Movies Section.")
		kodi.addDir("Trending",'trending','call_trakt_movies',artwork+'trending.png','',1,'','',fanart=fanart,description="Trending Movies Section.")
		kodi.addDir("Most Watched",'most_watched','call_trakt_movies',artwork+'most_watched.png','',1,'','',fanart=fanart,description="Most Watched Movies Section.")
		kodi.addDir("Most Played",'most_played','call_trakt_movies',artwork+'most_played.png','',1,'','',fanart=fanart,description="Most Played Movies Section.")
		kodi.addDir("Most Collected",'most_collected','call_trakt_movies',artwork+'most_collected.png','',1,'','',fanart=fanart,description="Most Collected Movies.")
		kodi.addDir("Box Office",'box_office','call_trakt_movies',artwork+'box_office.png','',1,'','',fanart=fanart,description="Movies in the Box Office.")
		kodi.addDir("Search",'','trakt_search_movies',artwork+'search.png','',1,'','',fanart=fanart,description="Search for Movies.")
		if kodi.get_setting('trakt_authorized') == 'true':
			kodi.addDir("[COLOR gold]My Movie Collection[/COLOR]",'my_collected','call_trakt_movies',artwork+'my_collection.png','',1,'','',fanart=fanart,description="View Your own Movie Collection.")
			kodi.addDir("[COLOR gold]Recomended for me[/COLOR]",'my_recomends','call_trakt_movies',artwork+'recomended.png','',1,'','',fanart=fanart,description="Movies recomended just for you!")
			kodi.addDir("[COLOR gold]Movie Watchlist[/COLOR]",'get_watchlist_movies','call_trakt_movies',artwork+'watchlist.png','',1,'','',fanart=fanart,description="Your Own Movie Watchlist.")
			kodi.addDir("[COLOR gold]Watched Movie History[/COLOR]",'get_watched_history','call_trakt_movies',artwork+'watchhistory.png','',1,'','',fanart=fanart,description="Your Watched Movie History.")
			kodi.addDir("[COLOR gold]My Custom Movie Lists[/COLOR]",'','custom_movie_lists',artwork+'custom_lists.png','',1,'',media,fanart=fanart,description="Here you will find your Custom Movie Lists and Creation.")
			#kodi.addDir("[COLOR gold]Search Other Users Movie Lists[/COLOR]",' ','get_user_lists',artwork+'other_user_lists.png','',1,'',media,fanart=fanart)

			#kodi.addDir("[COLOR gold]Special Movie Lists[/COLOR]",'','public_lists',artwork+'special_movie_lists.png','',1,'',media,fanart=fanart)
		viewsetter.set_view("sets")

def tv_menu():

	media ='shows'
	kodi.addDir("Popular TV",'popular_tv','call_trakt_tv',artwork+'popular_tv.png','',1,'','',fanart=fanart,description="Popular TV Section.")
	kodi.addDir("Trending TV",'trending_tv','call_trakt_tv',artwork+'trending_tv.png','',1,'','',fanart=fanart,description="Trending TV Section.")
	kodi.addDir("Most 