# -*- coding: utf-8 -*-
import xbmc,xbmcaddon,xbmcgui,xbmcplugin,os,sys,xbmcvfs,time,routing,requests, codecs, scripts
from xbmcgui import ListItem
from xbmcplugin import addDirectoryItem as add
from xbmcplugin import endOfDirectory as end
addon = xbmcaddon.Addon()
plugin = routing.Plugin()
dialog = xbmcgui.Dialog()
PY2 = sys.version_info[0] == 2
if PY2:
	from xbmc import translatePath
	from urllib import urlretrieve
else:
	from xbmcvfs import translatePath
	from urllib.request import urlretrieve
	
guisettings ={
	"lookandfeel.skin": '"skin.mimic.lr"',
	"services.webserverpassword": '"kodi"',
	"locale.language": '"resource.language.de_de"',
	"locale.keyboardlayouts": '"German QWERTZ"',
	"locale.activekeyboardlayout": '"German QWERTZ"',
	"locale.country": '"Deutschland"',
	"filelists.showparentdiritems": "false",
	"filelists.ignorethewhensorting": "false",
	"filelists.showhidden": "true",
	"filelists.allowfiledeletion": "true",
	"myvideos.selectaction": 2,
	"epg.epgupdate": 120,
	"epg.futuredaystodisplay": 2,
	"myvideos.usetags": "true",
	"myvideos.stackvideos": "true",
	"videolibrary.groupmoviesets": "true",
	"videolibrary.showemptytvshows": "false",
	"videolibrary.tvshowsselectfirstunwatcheditem": 2,
	"videoplayer.autoplaynextitem": '"1,2"',
	"videoplayer.seeksteps": '"-60,-30,-10,10,30,60"',
	"videoplayer.adjustrefreshrate": 2,
	"videoplayer.usedisplayasclock": "true",
	"videoplayer.teletextenabled": "false",
	"locale.audiolanguage": '"default"',
	"videoplayer.preferdefaultflag": "false",
	"locale.subtitlelanguage": '"forced_only"',
	"subtitles.languages": '"German"',
	"subtitles.downloadfirst": "true",
	"subtitles.tv": '"service.subtitles.opensubtitles"',
	"subtitles.movie": '"service.subtitles.opensubtitles"',
	"pvrmanager.usebackendchannelnumbers": "true",
	"pvrmanager.preselectplayingchannel": "true",
	"pvrplayback.confirmchannelswitch": "false",
	"pvrplayback.signalquality": "false",
	"pvrplayback.enableradiords": "false",
	"services.webserver": "true",
	"services.esallinterfaces": "true",
	"audiooutput.streamsilence": 0,
	"audiooutput.streamnoise": "false",
	"audiooutput.guisoundmode": 0,
	"audiooutput.guisoundvolume": 0,
	"powermanagement.shutdowntime": 30,
	"addons.unknownsources": "true",
	"system.playlistspath": '"special://profile/playlists/"',
	"general.settinglevel": 3,
	"addons.updatemode": 1}

pvrsettings ={
	"m3uUrl": "https://michaz1988.github.io/tv.m3u",
	"m3uCache": "false",
	"epgUrl": "https://michaz1988.github.io/guide.xml.gz",
	"epgCache": "false"}

@plugin.route('/')
def index():
	add(plugin.handle, plugin.url_for(bundle),ListItem("Build installieren"))
	#add(plugin.handle, plugin.url_for(setup),ListItem("Pvr update"))
	add(plugin.handle, plugin.url_for(renew),ListItem("Premium update"))
	add(plugin.handle, plugin.url_for(set_settings),ListItem("Kodi-Einstellugen setzen"))
	add(plugin.handle, plugin.url_for(ftp),ListItem("FTP SERVER"))
	add(plugin.handle, plugin.url_for(speedtest), ListItem("Speedtest"))
	add(plugin.handle, plugin.url_for(showIp), ListItem("IP anzeigen"))
	add(plugin.handle, plugin.url_for(repotest),ListItem("Repos testen"))
	add(plugin.handle, plugin.url_for(beenden),ListItem("Beenden"))
	add(plugin.handle, plugin.url_for(buffer), ListItem("Buffer fix"))
	add(plugin.handle, plugin.url_for(cleanthumbs, False), ListItem("Thumbs entfernen"))
	add(plugin.handle, plugin.url_for(skinrestore),ListItem("Skin restore"))
	#add(plugin.handle, plugin.url_for(proxy),ListItem("Proxy"))
	#add(plugin.handle, plugin.url_for(kodiIp), ListItem("IP renew"))
	add(plugin.handle, plugin.url_for(ReloadSkin), ListItem("ReloadSkin"))
	add(plugin.handle, plugin.url_for(setmenu), ListItem("ALLE-EINSTELLUNGEN"), True)
	end(plugin.handle)

@plugin.route('/setmenu')
def setmenu():
	home_path = translatePath('special://home/addons')
	xbmc_path = translatePath('special://xbmc/addons')
	addonids=[]
	for dirname in os.listdir(home_path):
		if os.path.isfile(os.path.join(home_path,dirname,"resources","settings.xml")):
			addonids.append(dirname)
	for addonid in os.listdir(xbmc_path):
		if os.path.isfile(os.path.join(xbmc_path,dirname,"resources","settings.xml")):
			addonids.append(dirname)
	for newaddonid in addonids:
		add(plugin.handle, plugin.url_for(settings, id=newaddonid), ListItem(str(newaddonid.lower()+'-EINSTELLUNGEN')))
	end(plugin.handle)
	
@plugin.route('/settings/<id>')
def settings(id):
	xbmcaddon.Addon(id).openSettings()

@plugin.route('/speedtest')
def speedtest():
	xbmc.executebuiltin('Runscript("special://home/addons/plugin.video.tools/lib/speedtest.py")')
	
@plugin.route('/repotest')
def repotest():
	import repotest

@plugin.route('/ReloadSkin')
def ReloadSkin():
	xbmc.executebuiltin('ReloadSkin')
	
@plugin.route('/buffer')
def buffer():
	scripts.advancedSettings()
		
@plugin.route('/ftp')
def ftp():
	scripts.show()
    
@plugin.route('/beenden')
def beenden():
	os._exit(1)
	
@plugin.route('/showIp')
def showIp():
	a = requests.get('https://api.myip.com/').json()
	ext_ip = a['ip']
	ok = dialog.ok('Externe IP-Adresse (WAN)', ext_ip)

@plugin.route('/cleanthumbs/<yesnowindow>')
def cleanthumbs(yesnowindow):
	force = False
	if not yesnowindow:
		yesnowindow = dialog.yesno('TOOLS', "Clean all Thumbs")
	if yesnowindow:
		force = True
		for root, dirs, files in os.walk(translatePath('special://profile/Thumbnails/')):
			for name in files:
				os.remove(os.path.join(root, name))
		for root, dirs, files in os.walk(translatePath('special://home/addons/packages/')):
			for name in files:
				os.remove(os.path.join(root, name))
		if not force:ok = dialog.ok('', 'Fertig')
			
@plugin.route('/proxy')
def proxy():
	scripts.check_proxy()
			
@plugin.route('/bundle')
def bundle():
	yesnowindow = dialog.yesno('TOOLS', 'Build installieren?')
	if yesnowindow:
		scripts.get_packages()
		
@plugin.route('/kodiIp')
def kodiIp():
	yesnowindow = dialog.yesno('TOOLS', "Neue Ip?")
	if yesnowindow:
		a = requests.get('https://api.myip.com/').json()
		alt_ip = a['ip']
		con = scripts.connect_mobile
		dis = scripts.disconnect_mobile
		ctx = scripts.quick_login(addon.getSetting('user'), addon.getSetting('passw'))
		dis(ctx)
		con(ctx)
		time.sleep(10)
		try:
			a = requests.get('https://api.myip.com/').json()
			neu_ip = a['ip']
			ok = dialog.ok('Tool', 'Alte IP = '+alt_ip, 'Neue IP = '+neu_ip)
		except:
			try:
				time.sleep(5)
				a = requests.get('https://api.myip.com/').json()
				neu_ip = a['ip']
				ok = dialog.ok('Tool', 'Alte IP = '+alt_ip, 'Neue IP = '+neu_ip)
			except:
				time.sleep(5)
				a = requests.get('https://api.myip.com/').json()
				neu_ip = a['ip']
				ok = dialog.ok('Tool', 'Alte IP = '+alt_ip, 'Neue IP = '+neu_ip)
		
@plugin.route('/skinrestore')
def skinrestore():
	scripts.restore()

@plugin.route('/renew')
def renew():
	scripts.premium()

"""
	urlretrieve(addon.getSetting('prem2'), translatePath('special://home/userdata.zip'))
	time.sleep(2)
	xbmc.executebuiltin('Extract("special://home/userdata.zip")')
	time.sleep(2)
	xbmcvfs.rmdir('special://home/userdata.zip', force=True)
	urlretrieve(addon.getSetting('prem'), translatePath('special://home/userdata.zip'))
	time.sleep(2)
	xbmc.executebuiltin('Extract("special://home/userdata.zip")')
	import xml.etree.ElementTree as ET
	xbmcvfs.mkdir('special://home/mist/')
	zip = translatePath('special://home/mist/backup.zip')
	urlretrieve(addon.getSetting('prem'), zip)
	xbmc.executebuiltin('Extract("special://home/mist/backup.zip")')
	time.sleep(2)
	if xbmc.getCondVisibility('System.HasAddon("plugin.video.rtlnow")'):
		data = ET.parse(translatePath('special://home/mist/userdata/addon_data/plugin.video.tvnow.premium/settings.xml')).getroot()
		tvnowuser = data.find(".//*[@id='email']").text
		tvnowpass = data.find(".//*[@id='password']").text
		xbmcaddon.Addon('plugin.video.rtlnow').setSetting('user',tvnowuser)
		xbmcaddon.Addon('plugin.video.rtlnow').setSetting('pass',tvnowpass)
	if xbmc.getCondVisibility('System.HasAddon("slyguy.disney.plus")'):
		data = ET.parse(translatePath('special://home/mist/userdata/addon_data/plugin.video.disney.plus/settings.xml')).getroot()
		disneyuser = data.find(".//*[@id='_userdata']").text
		xbmcaddon.Addon('slyguy.disney.plus').setSetting('_userdata',disneyuser)
	if xbmc.getCondVisibility('System.HasAddon("plugin.video.waipu.tv")'):
		data = ET.parse(translatePath('special://home/mist/userdata/addon_data/plugin.video.waipu.tv/settings.xml')).getroot()
		waipuuser = data.find(".//*[@id='username']").text
		waipupass = data.find(".//*[@id='password']").text
		xbmcaddon.Addon('plugin.video.waipu.tv').setSetting('username',waipuuser)
		xbmcaddon.Addon('plugin.video.waipu.tv').setSetting('password',waipupass)
	xbmcvfs.rmdir('special://home/mist/', force=True)
	xbmcvfs.rmdir('special://profile/addon_data/plugin.video.dazn', force=True)
	xbmcvfs.rmdir('special://profile/addon_data/plugin.video.joyn', force=True)
	urlretrieve("http://michaz1988.000webhostapp.com/userdata.zip", translatePath('special://home/userdata.zip'))
	xbmc.executebuiltin('Extract("special://home/userdata.zip")')
	if dialog.ok('Tool', 'Fertig!'):
	xbmcvfs.rmdir('special://home/userdata.zip', force=True)
"""

@plugin.route('/forceIp')
def forceIp():
	con = scripts.connect_mobile
	dis = scripts.disconnect_mobile
	ctx = scripts.quick_login(addon.getSetting('user'), addon.getSetting('passw'))
	dis(ctx)
	con(ctx)

@plugin.route('/set_settings')
def set_settings():
	for key, value in guisettings.items():
		xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"%s", "value":%s}, "id":1}' % (key, value))
	for key, value in pvrsettings.items():
		xbmcaddon.Addon("pvr.iptvsimple").setSetting(key, value)

@plugin.route('/setup')
def setup():
	cleanthumbs(yesnowindow=True)
	renew()
	dialog.notification('TOOLS', 'Starte EPG Aktualisierung', xbmcgui.NOTIFICATION_INFO, 2000)
	m3uPath = 'special://profile/addon_data/plugin.video.tools/channels.m3u'
	xml = '<settings version="2">\n    <setting id="m3uPathType">0</setting>\n    <setting id="m3uPath">'+m3uPath+'</setting>\n    <setting id="m3uUrl" default="true" />\n    <setting id="m3uCache" default="true">true</setting>\n    <setting id="startNum" default="true">1</setting>\n    <setting id="numberByOrder" default="true">false</setting>\n    <setting id="m3uRefreshMode" default="true">0</setting>\n    <setting id="m3uRefreshIntervalMins" default="true">60</setting>\n    <setting id="m3uRefreshHour" default="true">4</setting>\n    <setting id="epgPathType">0</setting>\n    <setting id="epgPath">special://home/userdata/addon_data/plugin.video.tools/epg.xml</setting>\n    <setting id="epgUrl" default="true" />\n    <setting id="epgCache" default="true">true</setting>\n    <setting id="epgTimeShift" default="true">0</setting>\n    <setting id="epgTSOverride" default="true">false</setting>\n    <setting id="useEpgGenreText" default="true">false</setting>\n    <setting id="genresPathType" default="true">0</setting>\n    <setting id="genresPath" default="true">special://userdata/addon_data/pvr.iptvsimple/genres/genreTextMappings/genres.xml</setting>\n    <setting id="genresUrl" default="true" />\n    <setting id="logoPathType" default="true">1</setting>\n    <setting id="logoPath" default="true" />\n    <setting id="logoBaseUrl" default="true" />\n    <setting id="logoFromEpg" default="true">1</setting>\n    <setting id="timeshiftEnabled">true</setting>\n    <setting id="timeshiftEnabledAll" default="true">true</setting>\n    <setting id="timeshiftEnabledHttp" default="true">true</setting>\n    <setting id="timeshiftEnabledUdp" default="true">true</setting>\n    <setting id="timeshiftEnabledCustom">true</setting>\n    <setting id="catchupEnabled" default="true">false</setting>\n    <setting id="catchupQueryFormat" default="true" />\n    <setting id="catchupDays" default="true">5</setting>\n    <setting id="allChannelsCatchupMode" default="true">0</setting>\n    <setting id="catchupPlayEpgAsLive" default="true">false</setting>\n    <setting id="catchupWatchEpgBeginBufferMins" default="true">5</setting>\n    <setting id="catchupWatchEpgEndBufferMins" default="true">15</setting>\n    <setting id="catchupOnlyOnFinishedProgrammes" default="true">false</setting>\n    <setting id="transformMulticastStreamUrls" default="true">false</setting>\n    <setting id="udpxyHost" default="true">127.0.0.1</setting>\n    <setting id="udpxyPort" default="true">4022</setting>\n    <setting id="useFFmpegReconnect" default="true">true</setting>\n    <setting id="useInputstreamAdaptiveforHls" default="true">false</setting>\n    <setting id="defaultUserAgent" default="true" />\n    <setting id="defaultInputstream" default="true" />\n    <setting id="defaultMimeType" default="true" />\n</settings>'
	if not xbmcvfs.exists('special://profile/addon_data/pvr.iptvsimple/'):
		addon.setSetting('aktuell', 'erster Start bitte warten')
		try:
			xbmc.executeJSONRPC('{"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":"resource.language.de_de","enabled":true}}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"epg.futuredaystodisplay", "value":2}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"epg.epgupdate", "value":120}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"videoplayer.teletextenabled", "value":false}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"pvrplayback.signalquality", "value":false}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"pvrplayback.confirmchannelswitch", "value":false}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"locale.language", "value":"resource.language.de_de"}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"locale.keyboardlayouts", "value":"German QWERTZ"}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"locale.activekeyboardlayout", "value":"German QWERTZ"}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"locale.country", "value":"Deutschland"}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"pvrmanager.preselectplayingchannel", "value":true}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"filelists.showhidden", "value":true}, "id":1}')
			xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"filelists.allowfiledeletion", "value":true}, "id":1}')
		except:ok = dialog.ok('', 'Fuck irgendwas ist schiefgelaufen')
	xbmcvfs.mkdirs('special://profile/addon_data/pvr.iptvsimple/')
	set = translatePath('special://profile/addon_data/pvr.iptvsimple/settings.xml')
	with codecs.open(set, 'w', encoding='utf8') as f:
		f.write(xml)
	addon.setSetting('aktuell', 'aktualisierung in gange')
	epg.newepg()
	
def run():
    plugin.run()