#include <WindowsConstants.au3>
#include <WinAPIEx.au3>

Opt("GUIOnEventMode", 1)

Global Const $GUI_EVENT_CLOSE = -3
Global Const $GUI_ENABLE = 64
Global Const $GUI_DISABLE = 128

Global $hGUI1, $s1, $hGUI2 = 9999, $hButton2, $hButton3 = 9999 ; Predeclare the variables with dummy values to prevent firing the Case statements
Global $MenuItem1, $MenuItemExit, $exit = 0
Local  $ContextMenu, $separator1, $InputLocation, $l, $UserLocation
Local Const $sFilePath = "C:\Users\Home\OneDrive\AutoIt3\EarthWind\EWConfig.ini"

Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
Local $fDiff = 3700000

Local $hGui, $sURL = "https://earth.nullschool.net/#current/wind/surface/level/orthographic=-27.12,35.50,554 http://www.google.com"
Local $iLeft = 0, $iTop = 0, $iWidth = @DesktopWidth, $iHeight = @DesktopHeight - 1

$hGUI1 = GUICreate('',(@DesktopWidth),(@DesktopHeight - 1),0,1,$WS_POPUP)
Local $aMain = ChromeCreatePseudoEmbedded($iLeft, $iTop, $iWidth, $iHeight, $hGUI1, $sURL)
$Input1 = GUICtrlCreateInput("$UserLocation", 16, 8, 121, 21)
GUISetState(@SW_SHOW)
$ContextMenu = GUICtrlCreateContextMenu()
$MenuItem1 = GUICtrlCreateMenuItem("Set Default Location", $ContextMenu)
GUICtrlSetOnEvent($MenuItem1, "gui2")
$separator1 = GUICtrlCreateMenuItem("", $ContextMenu) ; create a separator line
$MenuItemExit = GUICtrlCreateMenuItem("Exit", $ContextMenu)
GUICtrlSetOnEvent($MenuItemExit, "On_Close")
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
GUISetState()

Local $ProgramFilesDir = EnvGet('ProgramFiles(x86)') ; for 64bit Win it will return a valid path.
if not $ProgramFilesDir then $ProgramFilesDir = @ProgramFilesDir ; for 32bit Win this will "repair" the broken return from above.

While 1
    If $fDiff > 3600000 Then
        Send("{F5}")
        $hTimer = TimerInit()
    EndIf
    $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
    Consolewrite($fDiff & @CRLF)
    Sleep(1000)
WEnd


 Func gui2()
    $hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
    $InputLocation = GUICtrlCreateInput($l, 16, 8, 121, 21)
    GUICtrlSetData(-1, IniRead($sFilePath, "Location", "key1", "Oxford" ))
    $hButton3 = GUICtrlCreateButton("Cancel",  20, 100, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    $hButton2 = GUICtrlCreateButton("OK",  110, 100, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    GUISetState()
 EndFunc   ;==>gui2


 Func On_Close()
    Switch @GUI_WINHANDLE ; See which GUI sent the CLOSE message
         Case $hGUI1
            WinClose($aMain[1]) ; Chrome kiosk window
            WinClose($hGui1) ; Own gui
            ProcessWaitClose($aMain[0]) ; Chrome executable
            Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<<
         Case $hGUI2
            GUIDelete($hGUI2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<
            GUICtrlSetState($hButton2, $GUI_ENABLE)
    EndSwitch
 EndFunc


 Func On_Button()
     Switch @GUI_CTRLID ; See which button sent the message
         ;Case $hButton1
             ;MessageBox(1) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<<
         Case $hButton2
             $s1 = GUICtrlRead($InputLocation, $l) ; Read input 1 until the text has been set in input 2; because it's in a while loop the process will be executed until the script has exited
             IniWrite($sFilePath, "Location", "key1", $s1)
             ConsoleWrite(IniRead($sFilePath, "Location", "key1", "Error") & @CRLF)
             GUICtrlSetState($hButton2, $GUI_DISABLE)
             On_Close()
         Case $hButton3
             ConsoleWrite($s1 & @CRLF)
             GUICtrlSetState($hButton2, $GUI_DISABLE)
             On_Close()
     EndSwitch
 EndFunc

 Func ChromeCreatePseudoEmbedded($i_Left, $i_Top, $i_Width, $i_Height, $h_Parent, $sURL)
;http://www.autoitscript.com/forum/topic/152173-pseudo-embed-google-chrome/

    Local $acMain[2]
    $acMain[0] = Run(@ProgramFilesDir & "\Google\Chrome\Application\chrome.exe --chrome-frame -kiosk" & $sURL)
    If @error then
        $acMain[0] = Run("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --chrome-frame -kiosk " & $sURL)
        If @error Then
            MsgBox("", "Error", "Please install Google Chrome")
            Exit
        EndIf
    Endif

    Local $ah_HWND
    Do
        $ah_HWND = _WinAPI_EnumProcessWindows($acMain[0])
        Sleep(10)
    Until IsArray($ah_HWND)

    $acMain[1] = HWnd($ah_HWND[1][0])

    _WinAPI_SetParent($acMain[1], $h_Parent)
    _WinAPI_SetWindowLong($acMain[1], $GWL_STYLE, $WS_POPUP + $WS_VISIBLE)

    ;ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 2)
    ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 30)
    GUISetState(@SW_SHOW, $h_Parent)
    WinActivate($acMain[1])

    Return $acMain

EndFunc   ;==>ChromeCreatePseudoEmbedded