# -*- coding: utf-8 -*-
# system modules
 
from __future__ import print_function
#from Plugins.Extensions.NeoBoot.__init__ import _  
import codecs
from enigma import getDesktop
from Components.ActionMap import ActionMap
from Components.Label import Label
from Components.ScrollLabel import ScrollLabel
from Components.Pixmap import Pixmap
from Components.Sources.List import List
from Components.ConfigList import ConfigListScreen
from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmapAlphaTest
from Components.config import getConfigListEntry, config, ConfigYesNo, ConfigText, ConfigSelection, NoSave
from Plugins.Extensions.NeoBoot.plugin import Plugins, PLUGINVERSION, UPDATEVERSION
from Plugins.Plugin import PluginDescriptor
from Screens.Standby import TryQuitMainloop
from Screens.MessageBox import MessageBox
from Screens.Screen import Screen
from Tools.LoadPixmap import LoadPixmap
from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE, SCOPE_CURRENT_SKIN, fileExists, pathExists, createDir
from os import system, listdir, mkdir, chdir, getcwd, rename as os_rename, remove as os_remove, popen
from os.path import dirname, isdir, isdir as os_isdir
from enigma import eTimer
from Plugins.Extensions.NeoBoot.files.stbbranding import getNeoLocation, getImageNeoBoot, getKernelVersionString, getBoxHostName, getCPUtype, getBoxVuModel, getTunerModel, getCPUSoC, getImageATv 
import os
import time
import sys
import struct, shutil
if fileExists('/etc/vtiversion.info') or fileExists('/usr/lib/python3.8') or fileExists('/usr/lib/python3.8') or fileExists('/usr/lib/python3.9') and fileExists('/.multinfo'):   
    from Screens.Console import Console                   
else:
    from Plugins.Extensions.NeoBoot.files.neoconsole import Console
LinkNeoBoot = '/usr/lib/enigma2/python/Plugins/Extensions/NeoBoot'                                              


class NBIChoose2(Screen):
    skin = """ \n\t<screen position="center,center" size="768,621" title="Neoboot - Restart helper">\n\t\t
     <widget name="lab1" position="20,10" size="725,45" font="Regular;24" halign="center" valign="center" transparent="1" />\n\t\t
     <widget name="lab2" position="20,60" size="725,45" font="Regular;22" halign="center" valign="center" transparent="1" />\n\t\t
     <widget source="list" render="Listbox" position="21,107" zPosition="1" size="726,460" scrollbarMode="showOnDemand" transparent="1">\n\t\t\t
     <convert type="StringList" />\n\t\t</widget>\n\t\t
     <widget name="key_red" position="17,580" zPosition="2" size="226,40" font="Regular;20" halign="center" valign="center" backgroundColor="green" transparent="1" foregroundColor="red" />\n\t\t
     <widget name="key_green" position="245,580" zPosition="2" size="287,40" font="Regular;20" halign="center" valign="center" backgroundColor="green" transparent="1" foregroundColor="green" />\n\t\t
     <widget name="key_blue" position="535,580" zPosition="2" size="230,40" font="Regular;20" halign="center" valign="center" backgroundColor="blue" transparent="1" foregroundColor="blue" />\n\t
     </screen>"""

    def __init__(self, session):
        Screen.__init__(self, session)
        self['device_icon'] = Pixmap()        
        self['lab1'] = Label('')
        self['lab2'] = Label('')
        self['key_red'] = Label(_('Force reboot'))
        self['key_green'] = Label(_('Boot Image'))
        self['key_blue'] = Label(_('Help'))
        self['list'] = List([])
        self['actions'] = ActionMap(['WizardActions', 'ColorActions'], {'back': self.close,
         'ok': self.bootImage,
         'red': self.forceBoot,
         'green': self.bootImage,
         'blue': self.mytools})
        self.availablespace = 0
        self.curimage = ''
        self.onShow.append(self.updateInfo)
        
    def updateInfo(self):
        self.list = []
        pluginpath = '' + LinkNeoBoot + ''
        f = open(pluginpath + '/.location', 'r')
        mypath = f.readline().strip()
        f.close()
        
        curimage = 'Flash'
        if fileExists('/.multinfo'):
            f = open('/.multinfo', 'r')
            curimage = f.readline().strip()
            f.close()
        strview = _('Current Running Image: ') + curimage
        self.curimage = curimage
        self['lab1'].setText(strview)
        
        linesdevice = open('' + LinkNeoBoot + '/.location', 'r').readlines()
        deviceneo = linesdevice[0][0:-1]
        device = deviceneo
        devicelist = ['cf',
         'hdd',
         'card',
         'usb',                            
         'usb2']
        for d in listdir('' + getNeoLocation() +''):
            if d == 'ImageBoot':
                continue
            test = '' + getNeoLocation() +'' + d + '/.neonextboot'
            if fileExists(test):
                device = d
                
        strview = _('NeoBoot Installed on: ') + device
        self['lab2'].setText(strview)
        
        imageslist = ['Flash']
        for fn in sorted(listdir('' + getNeoLocation() + '/ImageBoot'), key=lambda x: x.lower()):
            dirfile = '' + getNeoLocation() + '/ImageBoot/' + fn
            if os_isdir(dirfile):
                imageslist.append(fn)
                
        self['list'].list = imageslist
        
    def mytools(self):
            from Plugins.Extensions.NeoBoot.files.tools import MBTools
            self.session.open(MBTools)
            
    def bootImage(self):
        newimage = self['list'].getCurrent()
        if newimage:
            self.rebootimage = newimage.strip()
            message = _('Are you sure you want to Boot Image:\n ') + newimage + '?'
            ybox = self.session.openWithCallback(self.restStb, MessageBox, message, MessageBox.TYPE_YESNO)
            ybox.setTitle(_('Reboot Confirmation'))
            
    def restStb(self, answer):
        if answer is True:
            newimage = self['list'].getCurrent()
            if newimage:
                out = open('' + getNeoLocation() +'/ImageBoot/.neonextboot', 'w')
                out.write(self.rebootimage)
                out.close()
                try:
                    from Plugins.Extensions.NeoBoot.run import StartImage
                    self.session.open(StartImage)
                except:
                    os.system('reboot -f')

    def forceBoot(self):                                            
        self.session.open(ForceReboot)

class ForceReboot(Screen):
    __module__ = __name__
    skin = """<screen name="TunerInfo" title="NeoBoot - Sat Tuners " position="center,center" size="700,300" flags="wfNoBorder">
        <widget name="lab1" position="20,20" size="660,210" font="baslk;25" halign="center" valign="center" transparent="1" />
        <ePixmap position="200,250" size="34,34" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/images/red.png" alphatest="blend" zPosition="1" />
        <widget name="key_red" position="250,250" zPosition="2" size="280,35" font="baslk;30" halign="left" valign="center" backgroundColor="red" transparent="1" foregroundColor="red" />
        </screen>"""

    def __init__(self, session):
        Screen.__init__(self, session)
        self['lab1'] = Label(_('Force reboot to flash ?'))
        self['key_red'] = Label(_('To reboot press red!'))
        self['actions'] = ActionMap(['WizardActions', 'ColorActions'], {'back': self.close,
         'red': self.iNFO})
         
    def iNFO(self):
        try:
            out = open('%sImageBoot/.neonextboot' % getNeoLocation(), 'w' )
            out.write('Flash')
            out.close()        
            cmd = "echo -e '\n\n%s '" % _('Please wait, NeoBoot is working...')
            cmd1 = 'chmod 0755 ' + LinkNeoBoot + '/files/mountpoint.sh'
            cmd2 = '' + LinkNeoBoot + '/files/mountpoint.sh'
            cmd3 = "echo -e '\n\n%s '" % _('NeoBoot: Force reboot to flash image now...')            
            cmd4 = 'chmod 755 /usr/lib/enigma2/python/Plugins/Extensions/NeoReboot/backflash.sh; /usr/lib/enigma2/python/Plugins/Extensions/NeoReboot/backflash.sh'            
            self.session.open(Console, _('NeoBoot: Backu to flash!'), [cmd,
             cmd1,
             cmd2,
             cmd3,             
             cmd4])            
            self.close()

        except:
            False

def main(session, **kwargs):
        if not fileExists('' + LinkNeoBoot + '/.location'):
            pass
        else:
            if not fileExists('%sImageBoot/.version' % getNeoLocation()):
                if fileExists('' + LinkNeoBoot + '/files/neo.sh'):
                    os.system('chmod 0755 ' + LinkNeoBoot + '/files/neo.sh; ' + LinkNeoBoot + '/files/neo.sh')
                    if not fileExists('%sImageBoot/.version' % getNeoLocation()):
                        os.system('chmod 0755 ' + LinkNeoBoot + '/files/mountpoint.sh; ' + LinkNeoBoot + '/files/mountpoint.sh') 
        try:
            session.open(NBIChoose2)
        except:
            False


def startSetup(menuid):
    if menuid != 'mainmenu':
        return []
    return [(_('NeoReboot'),
      main,
      'NeoReboot',
      50)]

def Plugins(path, **kwargs):
    global plugin_path
    plugin_path = path
    list = [PluginDescriptor(name=_('NeoReboot'), description=_('Force reboot to flash.'), where=PluginDescriptor.WHERE_MENU, fnc=startSetup)]
    return list   
    
