import xbmc, xbmcaddon, xbmcgui, xbmcplugin,os,errno,base64,sys,xbmcvfs
import shutil
import urllib2,urllib
import re
import extract
import downloader
import time
from addon.common.addon import Addon
from addon.common.net import Net
###THANK YOU TO THOSE THAT MADE THE BASE OF THIS WIZARD GREAT WORK### If you would like your name here, if it is your code base please just ask ###


USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'
addon_id = 'plugin.video.iVuewiz'
ADDON = xbmcaddon.Addon(id=addon_id)
AddonTitle="iVue Wizard" 
net = Net()
U = ADDON.getSetting('User')
FANART = xbmc.translatePath(os.path.join('special://home/addons/' + addon_id , 'fanart.jpg'))
ICON = xbmc.translatePath(os.path.join('special://home/addons/' + addon_id, 'icon.png'))
ART = xbmc.translatePath(os.path.join('special://home/addons/' + addon_id + '/resources/art/'))
VERSION = "0.0.1"
DBPATH = xbmc.translatePath('special://database')
TNPATH = xbmc.translatePath('special://thumbnails');
PATH = "iVue Wizard"            
BASEURL = "http://ivuetvguide.com/toolbox/"
H = 'http://'
keep = 'settings.xml','master.db'

def INDEX():
    addDir('iVue TOOLS',BASEURL,2,ART+'ivuewizard.png',FANART,'')
    addDir('MAINTENANCE',BASEURL,3,ART+'MAINTENANCE.png',FANART,'')
    addDir('Channel Matching Subs',BASEURL,88,BASEURL+'subs.jpg',FANART,'')
    setView('movies', 'MAIN')

def CUSTOMiVue():
    addDir('INSTALL iVue TV GUIDE & Associated Addons',BASEURL+'/ivuetvguide.zip',5,BASEURL+'ivueinstall.jpg',FANART,'')
    addDir('INSTALL iVue RECOMENDED SETTINGS',BASEURL+'/settings.xml',17,BASEURL+'settings.jpg',FANART,'')
    addDir('SOFT RESET this does not delete your Database Safer','url',50,BASEURL+'softreset.jpg',FANART,'')
    addDir('HARD RESET Completely wipe all settings for iVue TV GUIDE ','url',18,BASEURL+'hardreset.jpg',FANART,'')
    addDir('Fix for daylight saving changes iVue TV GUIDE',BASEURL+'/xmlfix.zip',7,BASEURL+'FIXXML.jpg',FANART,'')
	
    setView('movies', 'MAIN')
	
def MAINTENANCE():
    addDir('DELETE CACHE','url',4,BASEURL+'deletecache.jpg',FANART,'')
    addDir('FORCE REFRESH','url',10,BASEURL+'repoupdate.png',FANART,'Force Refresh All Repos')
    setView('movies', 'MAIN')

def SUBSNAMES():
    addDir('Download Subscription name changes for creator iVue TV GUIDE',BASEURL+'/names.zip',8,BASEURL+'subs.jpg',FANART,'')
    setView('movies', 'MAIN')
	
################################
###    FIX REPOS&ADDONS      ###
################################

def UPDATEREPO():
	dialog = xbmcgui.Dialog()
	message = 'Checking Addon Updates'
	xbmc.executebuiltin('XBMC.Notification(%s, %s, 2000, %s)' % (addon_id, message, ICON))
	xbmc.executebuiltin("XBMC.UpdateAddonRepos()")
	dialog.ok("iVue Wizard", '',' CHECKING FOR REPO UPDATES SUCCESSFUL :)', " [COLOR gold]Brought To You By iVue Wizard[/COLOR]")
	return
    
#################################
###  iVue  Maintenance ###
#################################

def CUSTOMSET(url,name):
    dialog = xbmcgui.Dialog()
    if dialog.yesno("iVue Wizard", '                               Install Recomended Settings'):
        print '###'+AddonTitle+' - CUSTOM iVue SETTINGS###'
        path = xbmc.translatePath(os.path.join('special://masterprofile/addon_data/script.ivueguide',''))
        advance=os.path.join(path, 'settings.xml')
        link=net.http_GET(url).content
        a = open(advance,"w") 
        a.write(link)
        a.close()
        print '=== iVue Wizard - WRITING RECOMENDED    '+str(advance)+'    ==='
        dialog = xbmcgui.Dialog()
        dialog.ok(AddonTitle, "                               Done Adding RECOMENDED Settings")  
    return

def WIZARD(name,url,description):
    path = xbmc.translatePath(os.path.join('special://home/addons','packages'))
    dp = xbmcgui.DialogProgress()
    dp.create("iVue Wizard","Downloading ",'', 'Please Wait')
    lib=os.path.join(path, name+'.zip')
    try:
       os.remove(lib)
    except:
       pass
    downloader.download(url, lib, dp)
    addonfolder = xbmc.translatePath(os.path.join('special://','home'))
    time.sleep(2)
    dp.update(0,"", "Extracting Zip Please Wait")
    print '======================================='
    print addonfolder
    print '======================================='
    extract.all(lib,addonfolder,dp)
    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Wizard", "iVue 2 Installation complete please exit kodi to activate the changes","[COLOR yellow]Brought To You By iVue Guide[/COLOR]")

def WIZARD2(name,url,description):
    path = xbmc.translatePath(os.path.join('special://home/addons','packages'))
    dp = xbmcgui.DialogProgress()
    dp.create("iVue Wizard","Downloading ",'', 'Please Wait')
    lib=os.path.join(path, name+'.zip')
    try:
       os.remove(lib)
    except:
       pass
    downloader.download(url, lib, dp)
    addonfolder = xbmc.translatePath(os.path.join('special://','home'))
    time.sleep(2)
    dp.update(0,"", "Extracting Zip Please Wait")
    print '======================================='
    print addonfolder
    print '======================================='
    extract.all(lib,addonfolder,dp)
    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Wizard", "iVue XML fix has run please try openign iVue again","[COLOR yellow]Brought To You By iVue Guide[/COLOR]")	
	
def WIZARD3(name,url,description):
    path = xbmc.translatePath(os.path.join('special://home/addons','packages'))
    dp = xbmcgui.DialogProgress()
    dp.create("iVue Wizard","Downloading ",'', 'Please Wait')
    lib=os.path.join(path, name+'.zip')
    try:
       os.remove(lib)
    except:
       pass
    downloader.download(url, lib, dp)
    addonfolder = xbmc.translatePath(os.path.join('special://','home'))
    time.sleep(2)
    dp.update(0,"", "Extracting Zip Please Wait")
    print '======================================='
    print addonfolder
    print '======================================='
    extract.all(lib,addonfolder,dp)
    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Wizard", "Channel name changes have been added please press ok to update links","[COLOR yellow]Brought To You By iVue Guide[/COLOR]")	
    xbmc.executebuiltin('RunPlugin(plugin://plugin.video.IVUEcreator/update)')
#################################
###addon Move File to guide ###
#################################

def CUSTOMSET1(url,name):
    dialog = xbmcgui.Dialog()
    if dialog.yesno("Move File to guide data", ' Install Latest addon.ini'):
        print '###'+AddonTitle+' - iptv SETTINGS###'
        path = xbmc.translatePath(os.path.join('special://masterprofile/addon_data/script.ivueguide',''))
        advance=os.path.join(path, 'addons.ini')
        link=net.http_GET(url).content
        a = open(advance,"w") 
        a.write(link)
        a.close()
        print '=== iVue Wizard - WRITING RECOMENDED    '+str(advance)+'    ==='
        dialog = xbmcgui.Dialog()
        dialog.ok(AddonTitle, "Recommended settings have now been applied.")  
    return

#################################
###  iVue delete database ###
#################################

def DELETEiVueDB():
    iVue_cache_path = os.path.join(xbmc.translatePath('special://profile/addon_data/script.ivueguide/'), '')
    if os.path.exists(iVue_cache_path)==True:    
        for root, dirs, files in os.walk(iVue_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete iVue database Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
				

    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Maintenance", "  All guide Files have been removed, Please start iVue and once loaded exit ivue and then open iVue again to activate all the changes", "        [COLOR yellow]Brought To You By iVue wizard[/COLOR]")
	
def DELETEiVueDB1():
    iVue_cache_path = os.path.join(xbmc.translatePath('special://profile/addon_data/script.ivueguide/'), '')
    if os.path.exists(iVue_cache_path)==True:
		try:
			os.remove(iVue_cache_path+"addons.ini")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"guides.ini")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"prostrm.xml")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"prosmartup.xml")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"sports.xml")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"stalker1.xml")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"guide.xml")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"master.xml")
		except OSError:
			pass
		try:
			os.remove(iVue_cache_path+"amylist.xml")
		except OSError:
			pass
			
		try:
			os.remove(iVue_cache_path+"teamexpat.xml")
		except OSError:
			pass
			
		try:
			os.remove(iVue_cache_path+"otttv.xml")
		except OSError:
			pass
			
		try:
			os.remove(iVue_cache_path+"guide2.xml")
		except OSError:
			pass
			
		try:
			os.remove(iVue_cache_path+"uk3.xml")
		except OSError:
			pass
			
		try:
			os.remove(iVue_cache_path+"guide3.xmltv")
		except OSError:
			pass
			
    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Soft Reset", "  Soft rest has been completed, Please start iVue and once loaded, exit ivue and then open iVue again to activate all the changes", "        [COLOR yellow]Brought To You By iVue wizard[/COLOR]")

#################################
###  iVue install  ###
#################################

def WIZARD(name,url,description):
    path = xbmc.translatePath(os.path.join('special://home/addons','packages'))
    dp = xbmcgui.DialogProgress()
    dp.create("iVue Wizard","Downloading ",'', 'Please Wait')
    lib=os.path.join(path, name+'.zip')
    try:
       os.remove(lib)
    except:
       pass
    downloader.download(url, lib, dp)
    addonfolder = xbmc.translatePath(os.path.join('special://','home'))
    time.sleep(2)
    dp.update(0,"", "Extracting Zip Please Wait")
    print '======================================='
    print addonfolder
    print '======================================='
    extract.all(lib,addonfolder,dp)
    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Wizard", "iVue 2 Installation complete please exit kodi to activate the changes","[COLOR yellow]Brought To You By iVue Guide[/COLOR]")

#################################
###  iVue Skins install  ###
#################################
	
def WIZARD1(name,url,description):
    path = xbmc.translatePath(os.path.join('special://home/addons/script.ivueguide/resources',''))
    dp = xbmcgui.DialogProgress()
    dp.create("iVue Wizard","Downloading ",'', 'Please Wait')
    lib=os.path.join(path, name+'.zip')
    try:
       os.remove(lib)
    except:
       pass
    downloader.download(url, lib, dp)
    addonfolder = xbmc.translatePath(os.path.join('special://home/addons/script.ivueguide/resources',''))
    time.sleep(2)
    dp.update(0,"", "Extracting Zip Please Wait")
    print '======================================='
    print addonfolder
    print '======================================='
    extract.all(lib,addonfolder,dp)
    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Wizard", "Please restart kodi//xbmc for changes To Take Effect","[COLOR yellow]Brought To You By iVue Guide[/COLOR]")

    ############################################################        STANDARD CACHE          ###############################################################  
	
def deletecachefiles(url):
    print '############################################################       DELETING STANDARD CACHE             ###############################################################'
    xbmc_cache_path = os.path.join(xbmc.translatePath('special://home'), 'cache')
    if os.path.exists(xbmc_cache_path)==True:    
        for root, dirs, files in os.walk(xbmc_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete XBMC Cache Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        try:
                            os.unlink(os.path.join(root, f))
                        except:
                            pass
                    for d in dirs:
                        try:
                            shutil.rmtree(os.path.join(root, d))
                        except:
                            pass
                        
            else:
                pass
    if xbmc.getCondVisibility('system.platform.ATV2'):
        atv2_cache_a = os.path.join('/private/var/mobile/Library/Caches/AppleTV/Video/', 'Other')
        
        for root, dirs, files in os.walk(atv2_cache_a):
            file_count = 0
            file_count += len(files)
        
            if file_count > 0:

                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete ATV2 Cache Files", str(file_count) + " files found in 'Other'", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
        atv2_cache_b = os.path.join('/private/var/mobile/Library/Caches/AppleTV/Video/', 'LocalAndRental')
        
        for root, dirs, files in os.walk(atv2_cache_b):
            file_count = 0
            file_count += len(files)
        
            if file_count > 0:

                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete ATV2 Cache Files", str(file_count) + " files found in 'LocalAndRental'", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
              # Set path to Cydia Archives cache files
                             

    # Set path to What th Furk cache files
    wtf_cache_path = os.path.join(xbmc.translatePath('special://profile/addon_data/plugin.video.whatthefurk/cache'), '')
    if os.path.exists(wtf_cache_path)==True:    
        for root, dirs, files in os.walk(wtf_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete WTF Cache Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
                
                # Set path to 4oD cache files
    channel4_cache_path= os.path.join(xbmc.translatePath('special://profile/addon_data/plugin.video.4od/cache'), '')
    if os.path.exists(channel4_cache_path)==True:    
        for root, dirs, files in os.walk(channel4_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete 4oD Cache Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
                
                # Set path to BBC iPlayer cache files
    iplayer_cache_path= os.path.join(xbmc.translatePath('special://profile/addon_data/plugin.video.iplayer/iplayer_http_cache'), '')
    if os.path.exists(iplayer_cache_path)==True:    
        for root, dirs, files in os.walk(iplayer_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete BBC iPlayer Cache Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
                
                
                # Set path to Simple Downloader cache files
    downloader_cache_path = os.path.join(xbmc.translatePath('special://profile/addon_data/script.module.simple.downloader'), '')
    if os.path.exists(downloader_cache_path)==True:    
        for root, dirs, files in os.walk(downloader_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete Simple Downloader Cache Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
                
                # Set path to ITV cache files
    itv_cache_path = os.path.join(xbmc.translatePath('special://profile/addon_data/plugin.video.itv/Images'), '')
    if os.path.exists(itv_cache_path)==True:    
        for root, dirs, files in os.walk(itv_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete ITV Cache Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
				
                # Set path to temp cache files
    temp_cache_path = os.path.join(xbmc.translatePath('special://home/temp'), '')
    if os.path.exists(temp_cache_path)==True:    
        for root, dirs, files in os.walk(temp_cache_path):
            file_count = 0
            file_count += len(files)
        
        # Count files and give option to delete
            if file_count > 0:
    
                dialog = xbmcgui.Dialog()
                if dialog.yesno("Delete TEMP dir Cache Files", str(file_count) + " files found", "Do you want to delete them?"):
                
                    for f in files:
                        os.unlink(os.path.join(root, f))
                    for d in dirs:
                        shutil.rmtree(os.path.join(root, d))
                        
            else:
                pass
				

    dialog = xbmcgui.Dialog()
    dialog.ok("iVue Maintenance", "       All Cache Files Removed", "          [COLOR yellow]Brought To You By iVue wizard[/COLOR]")
 
        
def OPEN_URL(url):
    req = urllib2.Request(url)
    req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
    response = urllib2.urlopen(req)
    link=response.read()
    response.close()
    return link
          
        
def get_params():
        param=[]
        paramstring=sys.argv[2]
        if len(paramstring)>=2:
                params=sys.argv[2]
                cleanedparams=params.replace('?','')
                if (params[len(params)-1]=='/'):
                        params=params[0:len(params)-2]
                pairsofparams=cleanedparams.split('&')
                param={}
                for i in range(len(pairsofparams)):
                        splitparams={}
                        splitparams=pairsofparams[i].split('=')
                        if (len(splitparams))==2:
                                param[splitparams[0]]=splitparams[1]
                                
        return param

N = base64.decodestring('')
T = base64.decodestring('L2FkZG9ucy50eHQ=')
B = base64.decodestring('')
F = base64.decodestring('')
def addDir(name,url,mode,iconimage,fanart,description):
        u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&iconimage="+urllib.quote_plus(iconimage)+"&fanart="+urllib.quote_plus(fanart)+"&description="+urllib.quote_plus(description)
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name, "Plot": description } )
        liz.setProperty( "Fanart_Image", fanart )
        if mode==5 :
            ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=False)
        else:
            ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
        return ok
        
                      
params=get_params()
url=None
name=None
mode=None
iconimage=None
fanart=None
description=None


try:
        url=urllib.unquote_plus(params["url"])
except:
        pass
try:
        name=urllib.unquote_plus(params["name"])
except:
        pass
try:
        iconimage=urllib.unquote_plus(params["iconimage"])
except:
        pass
try:        
        mode=int(params["mode"])
except:
        pass
try:        
        fanart=urllib.unquote_plus(params["fanart"])
except:
        pass
try:        
        description=urllib.unquote_plus(params["description"])
except:
        pass
        
        
print str(PATH)+': '+str(VERSION)
print "Mode: "+str(mode)
print "URL: "+str(url)
print "Name: "+str(name)
print "IconImage: "+str(iconimage)


def setView(content, viewType):
    # set content type so library shows more views and info
    if content:
        xbmcplugin.setContent(int(sys.argv[1]), content)
    if ADDON.getSetting('auto-view')=='true':
        xbmc.executebuiltin("Container.SetViewMode(%s)" % ADDON.getSetting(viewType) )
        
        
if mode==None or url==None or len(url)<1:
        INDEX()

elif mode==1:
        REPOMAINTENANCE()

elif mode==2:
        CUSTOMiVue()

elif mode==3:
        MAINTENANCE()

elif mode==88:
        SUBSNAMES()
		
elif mode==4:
        deletecachefiles(url)
		
elif mode==5:
        WIZARD(name,url,description)

elif mode==6:
        WIZARD1(name,url,description)

elif mode==7:
        WIZARD2(name,url,description)		

elif mode==8:
        WIZARD3(name,url,description)		
		
elif mode==10:
        UPDATEREPO()
		
elif mode==17:
        CUSTOMSET(url,name)

elif mode==18:
        DELETEiVueDB()
		
elif mode==50:
        DELETEiVueDB1()
		
elif mode==19:
        CUSTOMSET1(url,name)
elif mode==20:
        CUSTOMSET2(url,name)
        
xbmcplugin.endOfDirectory(int(sys.argv[1]))
