
from pytank.Core.Elements import *
import pywinauto
from pytank.User.Buttons import *
import sys,os







def ScriptError(error):
    string = '################################################Script Failed ->' + str(os.path.basename(sys.argv[0])) + error
    raise ValueError(string)


def ConnectToConfigurator():
    print 'Connect To Configurator'
    GTools_systemname.Wait()
    sleep(3)
    GTools.ButtonControl.Click('Configurator')
    sleep(3)
    #gConnectPro.Close()


def GetListOfAllOpenedWindows():
    List = []

    def enumHandler(hwnd, lParam):
        List.append(hwnd)
    win32gui.EnumWindows(enumHandler, None)
    return List


def CloseAllWindowsNotInList(ListOfWindows):
    #gConnectPro.Close()
    print 'Closing All Windows'
    try:
        List = []
        def enumHandler(hwnd, lParam):
            List.append(hwnd)
        win32gui.EnumWindows(enumHandler, None)
        for window in List:
            if window not in ListOfWindows:
                try:
                    pwa_app = pywinauto.application.Application()
                    app = pwa_app.window_(handle=window)
                    app.Close()
                except pywinauto.controls.HwndWrapper.InvalidWindowHandle:
                    pass
    except:
        "print late on close"\
        ""
        pass



def AddDevices(button,Range,subbutton):
        button.Click()
        for i in xrange(Range):
                    subbutton.RightClickType('a')
                    AddNew_devicetype.Wait()
                    ListView.SelectByIndex(AddNew.GetHandle(),1,i)
                    Control.Click(AddNew.GetHandle(),"OK")
                    sleep(.50)
                    try:
                        Control.Click("OK")
                    except:
                        pass
                    AddNew_devicetype.WaitVanish()
                    sleep(.50)
                    ChangeAllFields()
                    sleep(2)
                    print 'Number -> ' + str(i)
                    Configurator_eventmapper.Click()
                    button.Click()


def ChangeAllFields():
    print 'Change All fields'
    ComboBox.SelectAllWithIndex(Configurator.GetHandle(),2)
    Edit.SetAllText(Configurator.GetHandle(),'Changed')
    try:
        Configurator_apply.c()
    except:
        pass



def ResetToFactory():
    print 'Reset To Factory'
    Configurator_system.Wait()
    sleep(3)
    Configurator_system.Click()
    sleep(4)
    Configurator_basicinformation.RightClickTypeThenPress('RRRR','enter')
    try:
        sleep(1)
        HlConfig.Press('enter',attemps=1)
        sleep(1)
    except:
        pass

def FactoryReset():
    Configurator_system.Click()
    sleep(1)
    Configurator_basicinformation.RightClickTypeThenPress('RRRR','enter')
    try:
        sleep(1)
        HlConfig.Press('enter',attemps=1)
        sleep(1)
    except:
        pass


