#
#      Copyright (C) 2016 Richard Dean (write2dixie@gmail.com)
#

import os
import xbmc
import dixie
import resources

from resources.pastebin_python import PastebinPython
from resources.pastebin_python.pastebin_exceptions   import PastebinBadRequestException, PastebinNoPastesException, PastebinFileException
from resources.pastebin_python.pastebin_constants    import PASTE_PUBLIC, EXPIRE_10_MIN, EXPIRE_1_MONTH, EXPIRE_1_WEEK, PASTE_UNLISTED
from resources.pastebin_python.pastebin_formats      import FORMAT_NONE, FORMAT_PYTHON, FORMAT_HTML


def LogUploader():
    pastebin = PastebinPython(api_dev_key='82ab1f8053f13f84beea0d1c933f48f2')
    filename = os.path.abspath(getLogfile())

    dixie.log('------------ UPLOAD LOG ------------')
    dixie.log(filename)

    try:
        pastebin.createAPIUserKey('OnTappNetworks', 'ontapp123')
        paste   = pastebin.createPasteFromFile(filename, 'On-Tapp-Networks Logfiles', FORMAT_HTML, PASTE_UNLISTED, EXPIRE_1_WEEK)
        logcode = paste.rsplit('/', 1)[-1]
        dixie.log(logcode)
 
        dixie.log(paste)
        dixie.DialogOK('This is your log code: [COLOR orange][B]' + logcode + '[/B][/COLOR]', 'Write it down and send it to us via our forum.', 'Or email it to us with your support question: support@on-tapp.tv')

    except PastebinBadRequestException as e:
        showError(e.message)
    except PastebinFileException as e:
        showError(e.message)


def showError(error):
    dixie.log(error)
    dixie.DialogOK('Sorry, the following error occurred:', '[COLOR orange][B]' + error + '[/B][/COLOR]', 'Restart Kodi and try again.')

def getLogfile():
    path = xbmc.translatePath('special://logpath')
    old  = os.path.join(path, 'xbmc.log')
    kodi = os.path.join(path, 'kodi.log')
    spmc = os.path.join(path, 'spmc.log')

    if os.path.isfile(old):
        dixie.log('======= On-Tapp.TV log path is =======')
        dixie.log(old)
        return old

    if os.path.isfile(kodi):
        dixie.log('======= On-Tapp.TV  log path is =======')
        dixie.log(kodi)
        return kodi

    if os.path.isfile(spmc):
        dixie.log('======= On-Tapp.TV  log path is =======')
        dixie.log(spmc)
        return spmc


if __name__ == '__main__':
    dixie.ShowBusy()
    LogUploader()
    dixie.CloseBusy()
