# -*- coding: utf-8 -*-


import os           # access operating system commands
import xbmc         # the base xbmc functions, pretty much every add-on is going to need at least one function from here
import xbmcaddon    # pull addon specific information such as settings, id, fanart etc.
import xbmcplugin   # contains functions required for creating directory structure style add-ons (plugins)

# The following are often used, we are not using them in this particular file so they are commented out

# import re           # allows use of regex commands, if you're intending on scraping you'll need this
# import xbmcgui      # gui based functions, contains things like creating dialog pop-up windows

from koding import route, Addon_Setting, Add_Dir, Find_In_Text, Open_URL, OK_Dialog
from koding import Open_Settings, Play_Video, Run, Text_File, Show_Busy
from resources.lib.modules.videograb import parseVidInfo
# from resources.lib.modules import default2

#------------------------------------------------------------

"""
    SECTION 4:
    These are our global variables, anything we set here can be accessed by any of
    our functions later on. Please bare in mind though that if you change the value
    of a global variable from inside a function the value will revert back to the
    value set here once that function has completed.
"""
debug = Addon_Setting(setting='debug')       # Grab the setting of our debug mode in add-on settings
addon_id = xbmcaddon.Addon().getAddonInfo('id')  # Grab our add-on id

LiveFile = "https://www.dropbox.com/s/ykwuibi3alwztz0/OGTV_Sport_Live.txt?dl=1"
ChannelFile = "https://www.dropbox.com/s/zi4i96pdl9z5drr/OGTV_Sport_Channels.txt?dl=1"
PlaylistFile = "https://www.dropbox.com/s/unoh1u93r1e4nkx/OGTV_Sport_Playlists.txt?dl=1"
VideoFile = "https://www.dropbox.com/s/785vfk5twum24b8/OGTV_Sport_Videos.txt?dl=1"

# Set the base plugin url you want to hook into
BASE = "plugin://plugin.video.youtube/playlist/"
BASE2 = "plugin://plugin.video.youtube/channel/"

ART_URL = "https://archive.org/download/ogmcapp_gmail_Ogt/Artwork/"
NOURL = ' '

#----------------------------------------------------------------


################################################################################
################################################################################
#####                           Main Menu                                  #####
################################################################################
################################################################################
#----------------------------------------------------------------
# This is the main menu we open into

# Icon Links
icon_spfl = 'https://images-eu.ssl-images-amazon.com/images/I/61RwkDPuqpL.png'
icon_epl = "http://thetoptenner.com/wp-content/uploads/2013/12/Premier-League.png"


@route(mode='menu_OGtv_SPORT')
def Sport_Menu():
    Show_Busy(status=True)
    Add_Dir(name='[COLORlimegreen]OGtv:[/COLOR] %s' % ("[COLORgold]Sport[/COLOR]"), url=NOURL, mode='container_REFRESH2', folder=False, icon=ART_URL + 'ogtv.png')
    Add_Dir(name='-------------------', url=NOURL, mode="", folder=False, icon=ART_URL + 'ogtv.png')
    Add_Dir(name=ct("white", "Scottish Football"), url=NOURL, folder=True, icon=icon_spfl, mode='menu_OGtv_FOOTY_SCOT')
    Add_Dir(name=ct("white","English Football"), url=NOURL, folder=True, icon=icon_epl, mode='menu_OGtv_FOOTY_ENG')
    Add_Dir(name='-------------------', url=NOURL, mode="", folder=False, icon=ART_URL + 'ogtv.png')
    parseVidInfo(LiveFile, 'SportLive.txt')
    parseVidInfo(ChannelFile, 'SportChannels.txt')
    parseVidInfo(PlaylistFile, 'SportPlaylists.txt')
    parseVidInfo(VideoFile, 'SportVids.txt')
    Add_Dir(name='-------------------', url=NOURL, mode="", folder=False, icon=ART_URL + 'ogtv.png')
    #Add_Dir(name=ct("darkgray", "Clear OGtv Cache"), url=" ", mode='OGtv_Clean_Cache', icon=ART_URL + 'ogtv.png', fanart=' ', folder=False)
    Show_Busy(status=False)


################################################################################
################################################################################
#####                       Scottish Football Menu                         #####
################################################################################
################################################################################


# Icon Links
icon_scotcup = 'https://yt3.ggpht.com/-0eVKl-vDiRE/AAAAAAAAAAI/AAAAAAAAAAA/AbF8ZIClHFg/s900-c-k-no-mo-rj-c0xffffff/photo.jpg'
icon_scotnat = 'https://upload.wikimedia.org/wikipedia/en/thumb/5/50/Scotland_national_football_team_logo_2014.svg/316px-Scotland_national_football_team_logo_2014.svg.png'
icon_aberdeen = 'http://www.thesportsdb.com/images/media/team/badge/xuvwys1447597299.png'
icon_celtic = 'http://www.thesportsdb.com/images/media/team/badge/tswuut1447597436.png'
icon_dundee = 'http://www.thesportsdb.com/images/media/team/badge/wwvwuw1447597572.png'
icon_dundeeutd = 'http://www.thesportsdb.com/images/media/team/badge/vwyxux1447597718.png'
icon_dunfermline = 'http://www.thesportsdb.com/images/media/team/badge/uuvrvr1473539630.png'
icon_falkirk = 'http://www.thesportsdb.com/images/media/team/badge/utyrsp1429974076.png'
icon_hamilton = 'http://www.thesportsdb.com/images/media/team/badge/xqrvrx1447597873.png'
icon_hearts = 'http://www.thesportsdb.com/images/media/team/badge/twqvyt1447597939.png'
icon_hibs = 'http://www.thesportsdb.com/images/media/team/badge/rqyqtv1447604221.png'
icon_kilmarnock = 'http://www.thesportsdb.com/images/media/team/badge/xssqtu1447596951.png'
icon_motherwell = 'http://www.thesportsdb.com/images/media/team/badge/vsysqx1447598301.png'
icon_rangers = 'http://www.thesportsdb.com/images/media/team/badge/tvtwqx1447604370.png'
icon_stjohnstone = 'http://www.thesportsdb.com/images/media/team/badge/tstpqq1447598786.png'
icon_stmirren = 'http://www.thesportsdb.com/images/media/team/badge/xvtuvv1447604452.png'

# Channels
YT_CH_SPFL = "UCakRszbIjjGYtFrDPeg5Ieg"
YT_CH_SCOTCUP = "UClPCjayqAxV1ANqfACWdZqA"
YT_CH_SCOTNAT = "UCsaS5aZjlJPJCzNwjEc3waw"
YT_CH_ABERDEEN = "UCPBQJlE7us7HqJHmgQgWw3Q"
YT_CH_CELTIC = "UCBN-bb-hE7jYlcp4exwXRsQ"
YT_CH_DUNDEE = "UCQH-6ugaAYlSnjS-Gph5wQA"
YT_CH_DUNDEEUTD = "UCk7C7hsc6ACmRtuFC_szpRQ"
YT_CH_DUNFERMLINE = "UCjFLikoSg3NmpAcSZCxiZEQ"
YT_CH_FALKIRK = "UC2t_CS5GvuE3rna9hvWFJOA"
YT_CH_HAMILTON = "UCRyF_Fp9X7U_kVGpa8zBBOA"
YT_CH_HEARTS = "UCUMDz4A44VglneE3z1cBpAA"
YT_CH_HIBS = "UCXKLs14yjDc5s7GJ1txkWxw"
YT_CH_KILLIE = "UCEVINLCnqV-wlRg-wf_yMOw"
YT_CH_MOTHERWELL = "UC5uMEPupAjUESVnMQ6gXw-Q"
YT_CH_RANGERS = "UCVaGyBPoEAZItDjlFPsRcSA"
YT_CH_STJ = "UCkaWfoUwzkEwImZtEVbKdnw"
YT_CH_STM = "UC5MyVD6A1dbyq0-LoxDjxWQ"


@route(mode='menu_OGtv_FOOTY_SCOT')
def ScotFoot_Menu():
    Add_Dir(name='[COLORlimegreen]OGtv:[/COLOR] %s' % ("[COLORgold]Scottish Football[/COLOR]"), url=NOURL, mode='container_REFRESH2', folder=False, icon=ART_URL + 'ogtv.png')
    Add_Dir(name='-------------------', url=NOURL, mode="", folder=False, icon=ART_URL + 'ogtv.png')
    Add_Dir(name=ct("white", "SPFL - Official Channel"), url=BASE2 + YT_CH_SPFL + "/", folder=True, icon=icon_spfl)
    Add_Dir(name=ct("white", "Scottish Cup - Official Channel"), url=BASE2 + YT_CH_SCOTCUP + "/", folder=True, icon=icon_scotcup)
    Add_Dir(name=ct("white", "Scotland National Team - Official Channel"), url=BASE2 + YT_CH_SCOTNAT + "/", folder=True, icon=icon_scotnat)
    Add_Dir(name='-------------------', url=NOURL, mode="", folder=False, icon=ART_URL + 'ogtv.png')
    Add_Dir(name=ct("white", "Aberdeen - Official Channel"), url=BASE2 + YT_CH_ABERDEEN + "/", folder=True, icon=icon_aberdeen)
    Add_Dir(name=ct("white", "Celtic - Official Channel"), url=BASE2 + YT_CH_CELTIC + "/", folder=True, icon=icon_celtic)
    Add_Dir(name=ct("white", "Dundee - Official Channel"), url=BASE2 + YT_CH_DUNDEE + "/", folder=True, icon=icon_dundee)
    Add_Dir(name=ct("white", "Dundee Utd - Official Channel"), url=BASE2 + YT_CH_DUNDEEUTD + "/", folder=True, icon=icon_dundeeutd)
    Add_Dir(name=ct("white", "Dunfermline - Official Channel"), url=BASE2 + YT_CH_DUNFERMLINE + "/", folder=True, icon=icon_dunfermline)
    Add_Dir(name=ct("white", "Falkirk - Official Channel"), url=BASE2 + YT_CH_FALKIRK + "/", folder=True, icon=icon_falkirk)
    Add_Dir(name=ct("white", "Hamilton - Official Channel"), url=BASE2 + YT_CH_HAMILTON + "/", folder=True, icon=icon_hamilton)
    Add_Dir(name=ct("white", "Hearts - Official Channel"), url=BASE2 + YT_CH_HEARTS + "/", folder=True, icon=icon_hearts)
    Add_Dir(name=ct("white", "Hibs - Official Channel"), url=BASE2 + YT_CH_HIBS + "/", folder=True, icon=icon_hibs)
    Add_Dir(name=ct("white", "Kilmarnock - Official Channel"), url=BASE2 + YT_CH_KILLIE + "/", folder=True, icon=icon_kilmarnock)
    Add_Dir(name=ct("white", "Motherwell - Official Channel"), url=BASE2 + YT_CH_MOTHERWELL + "/", folder=True, icon=icon_motherwell)
    Add_Dir(name=ct("white", "Rangers - Official Channel"), url=BASE2 + YT_CH_RANGERS + "/", folder=True, icon=icon_rangers)
    Add_Dir(name=ct("white", "St Johnstone - Official Channel"), url=BASE2 + YT_CH_STJ + "/", folder=True, icon=icon_stjohnstone)
    Add_Dir(name=ct("white", "St Mirren - Official Channel"), url=BASE2 + YT_CH_STM + "/", folder=True, icon=icon_stmirren)


################################################################################
################################################################################
#####                       English Football Menu                         #####
################################################################################
################################################################################

# Icon Links
icon_Arsenal = "http://www.thesportsdb.com/images/media/team/badge/vrtrtp1448813175.png"
icon_Bournemouth = "http://www.thesportsdb.com/images/media/team/badge/xvpuuv1448813193.png"
icon_Brighton = "http://www.thesportsdb.com/images/media/team/badge/ywypts1448810904.png"
icon_Burnley = "http://www.thesportsdb.com/images/media/team/badge/sqrttx1448811003.png"
icon_Chelsea = "http://www.thesportsdb.com/images/media/team/badge/yvwvtu1448813215.png"
icon_CPalace = "http://www.thesportsdb.com/images/media/team/badge/rytxyw1448813222.png"
icon_Everton = "http://www.thesportsdb.com/images/media/team/badge/urypyx1448813231.png"
icon_Huddersfield = "http://www.thesportsdb.com/images/media/team/badge/qtusqx1420753809.png"
icon_Leicester = "http://www.thesportsdb.com/images/media/team/badge/xtxwtu1448813356.png"
icon_Liverpool = "http://www.thesportsdb.com/images/media/team/badge/uvxuqq1448813372.png"
icon_ManCity = "http://www.thesportsdb.com/images/media/team/badge/vwpvry1467462651.png"
icon_Newcastle = "http://www.thesportsdb.com/images/media/team/badge/rqvwxt1448813396.png"
icon_Southampton = "http://www.thesportsdb.com/images/media/team/badge/qusxss1448813481.png"
icon_Stoke = "http://www.thesportsdb.com/images/media/team/badge/ypywwt1448813489.png"
icon_Swansea = "http://www.thesportsdb.com/images/media/team/badge/vwxqrx1448813505.png"
icon_Tottenham = "http://www.thesportsdb.com/images/media/team/badge/rxxqtp1448813512.png"
icon_Watford = "http://www.thesportsdb.com/images/media/team/badge/rsuswy1448813519.png"
icon_WestBrom = "http://www.thesportsdb.com/images/media/team/badge/rsvuxw1448813527.png"
icon_WestHam = "http://www.thesportsdb.com/images/media/team/badge/yutyxs1467459956.png"

# Channels
YT_CH_Arsenal = "UCpryVRk_VDudG8SHXgWcG0w"
YT_CH_Bournemouth = "UCeOCuVSSweaEj6oVtJZEKQw"
YT_CH_Brighton = "UCf-cpC9WAdOsas19JHipukA"
YT_CH_Burnley = "UChvUXuSDeEFSQZS8GcPMtkg"
YT_CH_Chelsea = "UCU2PacFf99vhb3hNiYDmxww"
YT_CH_CPalace = "UCWB9N0012fG6bGyj486Qxmg"
YT_CH_Everton = "UCtK4QAczAN2mt2ow_jlGinQ"
YT_CH_Huddersfield = "UCPzdw2bYGCq6XfjEl61_iyw"
YT_CH_Leicester = "UCBkRQtxofyXr09mWtgoUUqw"
YT_CH_Liverpool = "UC9LQwHZoucFT94I2h6JOcjw"
YT_CH_ManCity = "UCkzCjdRMrW2vXLx8mvPVLdQ"
YT_CH_Newcastle = "UCywGl_BPp9QhD0uAcP2HsJw"
YT_CH_Southampton = "UCxvXjfiIHQ2O6saVx_ZFqnw"
YT_CH_Stoke = "UCmFPjHUFr0hyE6eFGvCm7IA"
YT_CH_Swansea = "UCSMZZFBE92Yn-_XYdDiiANA"
YT_CH_Tottenham = "UCEg25rdRZXg32iwai6N6l0w"
YT_CH_Watford = "UCptKljTrbdMTgmuekGKhRug"
YT_CH_WestBrom = "UCnDBNo0zLm11TTXPVXvEN1g"
YT_CH_WestHam = "UCCNOsmurvpEit9paBOzWtUg"


@route(mode='menu_OGtv_FOOTY_ENG')
def EngFoot_Menu():
    Add_Dir(name='[COLORlimegreen]OGtv:[/COLOR] %s' % ("[COLORgold]English Football[/COLOR]"), url=NOURL, mode='container_REFRESH2', folder=False, icon=ART_URL + 'ogtv.png')
    Add_Dir(name='-------------------', url=NOURL, mode="", folder=False, icon=ART_URL + 'ogtv.png')
    # Add_Dir(name=ct("white","SPFL - Official Channel"), url=BASE2 + YT_CH_SPFL + "/", folder=True, icon=icon_spfl)
    # Add_Dir(name=ct("white","Scottish Cup - Official Channel"), url=BASE2 + YT_CH_SCOTCUP + "/", folder=True, icon=icon_scotcup)
    # Add_Dir(name=ct("white","Scotland National Team - Official Channel"), url=BASE2 + YT_CH_SCOTNAT + "/", folder=True, icon=icon_scotnat)
    # Add_Dir(name='-------------------', url=NOURL, mode="", folder=False, icon=ART_URL + 'ogtv.png')
    Add_Dir(name=ct("white", "Arsenal - Official Channel"), url=BASE2 + YT_CH_Arsenal + "/", folder=True, icon=icon_Arsenal)
    Add_Dir(name=ct("white", "Bournemouth - Official Channel"), url=BASE2 + YT_CH_Bournemouth + "/", folder=True, icon=icon_Bournemouth)
    Add_Dir(name=ct("white", "Brighton - Official Channel"), url=BASE2 + YT_CH_Brighton + "/", folder=True, icon=icon_Brighton)
    Add_Dir(name=ct("white", "Burnley - Official Channel"), url=BASE2 + YT_CH_Burnley + "/", folder=True, icon=icon_Burnley)
    Add_Dir(name=ct("white", "Chelsea - Official Channel"), url=BASE2 + YT_CH_Chelsea + "/", folder=True, icon=icon_Chelsea)
    Add_Dir(name=ct("white", "Crystal Palace - Official Channel"), url=BASE2 + YT_CH_CPalace + "/", folder=True, icon=icon_CPalace)
    Add_Dir(name=ct("white", "Everton - Official Channel"), url=BASE2 + YT_CH_Everton + "/", folder=True, icon=icon_Everton)
    Add_Dir(name=ct("white", "Huddersfield - Official Channel"), url=BASE2 + YT_CH_Huddersfield + "/", folder=True, icon=icon_Huddersfield)
    Add_Dir(name=ct("white", "Leicester - Official Channel"), url=BASE2 + YT_CH_Leicester + "/", folder=True, icon=icon_Leicester)
    Add_Dir(name=ct("white", "Liverpool - Official Channel"), url=BASE2 + YT_CH_Liverpool + "/", folder=True, icon=icon_Liverpool)
    Add_Dir(name=ct("white", "Manchester City - Official Channel"), url=BASE2 + YT_CH_ManCity + "/", folder=True, icon=icon_ManCity)
    Add_Dir(name=ct("white", "Newcastle Utd - Official Channel"), url=BASE2 + YT_CH_Newcastle + "/", folder=True, icon=icon_Newcastle)
    Add_Dir(name=ct("white", "Southampton - Official Channel"), url=BASE2 + YT_CH_Southampton + "/", folder=True, icon=icon_Southampton)
    Add_Dir(name=ct("white", "Stoke City - Official Channel"), url=BASE2 + YT_CH_Stoke + "/", folder=True, icon=icon_Stoke)
    Add_Dir(name=ct("white", "Swansea - Official Channel"), url=BASE2 + YT_CH_Swansea + "/", folder=True, icon=icon_Swansea)
    Add_Dir(name=ct("white", "Tottenham Hotspur - Official Channel"), url=BASE2 + YT_CH_Tottenham + "/", folder=True, icon=icon_Tottenham)
    Add_Dir(name=ct("white", "Watford - Official Channel"), url=BASE2 + YT_CH_Watford + "/", folder=True, icon=icon_Watford)
    Add_Dir(name=ct("white", "West Brom - Official Channel"), url=BASE2 + YT_CH_WestBrom + "/", folder=True, icon=icon_WestBrom)
    Add_Dir(name=ct("white", "West Ham Utd - Official Channel"), url=BASE2 + YT_CH_WestHam + "/", folder=True, icon=icon_WestHam)


################################################################################
#####                              Misc                                    #####
################################################################################
################################################################################


def ct(colour, text):
    return '[COLOR%s]%s[/COLOR]' % (colour, text)


#----------------------------------------------------------------

"""
    SECTION 6:
    Essential if creating list items, this tells kodi we're done creating our list items.
    The list will not populate without this. In the run command you need to set default to
    whatever route you want to open into, in this example the 'main_menu' route which opens the
    Main_Menu() function up at the top.
"""
if __name__ == "__main__":
    Run(default='menu_OGtv_SPORT')
    xbmcplugin.endOfDirectory(int(sys.argv[1]))
