#region ;Includes
#include <Date.au3>
#include <Misc.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#endregion ;Includes

#region ;Hotkeys/Optionen
HotKeySet("{ESC}", "_kill")
Opt("MustDeclareVars", 0)
TraySetClick(0)
#endregion ;Hotkeys/Optionen

#region ;Variablen
Global Const $dir = (@ScriptDir & "\")
Global Const $prog = (@ScriptName)
Global Const $config = ("MultibleMousePos_v2.config")
Global Const $log = ("MultibleMousePos_v2.log")
$anzahl = ("")
$abstand = ("")
$count = ("")
$MPos = ("")
$record = ("")
$fc = ("")
#endregion ;Variablen

#region ;GUI1
$GUI1 = GUICreate("Koordinator_v2 @Biobaua", 400, 145, -1, -1)
GUISetCursor(2, 1)
GUISetFont(10, "", "", "Calibri")
GUISetBkColor(0xC3C3C3)

$Label1 = GUICtrlCreateLabel(" Anzahl (Klick's): ", 25, 25, 140, 20, 0x0200)
$Label2 = GUICtrlCreateLabel(" Zeitabstand (Sekunden): ", 25, 50, 140, 20, 0x0200)
$Input1 = GUICtrlCreateInput(IniRead($dir & $config, "CONFIG", "ANZAHL", ""), 170, 25, 205, 20, 0x2100)
$Input2 = GUICtrlCreateInput(IniRead($dir & $config, "CONFIG", "ABSTAND", ""), 170, 50, 205, 20, 0x2100)
$Button1 = GUICtrlCreateButton("Starten", 25, 100, 75, 25, 0x8000)
$Button2 = GUICtrlCreateButton("Log löschen", 100, 100, 100, 25, 0x8000)
$Button3 = GUICtrlCreateButton("Config löschen", 200, 100, 100, 25, 0x8000)
$Button4 = GUICtrlCreateButton("Speichern", 300, 100, 75, 25, 0x8000)

GUICtrlSetCursor($Input1, 5)
GUICtrlSetCursor($Input2, 5)
GUICtrlSetBkColor($Label1, 0xA3A3A3)
GUICtrlSetBkColor($Label2, 0xA3A3A3)
GUICtrlSetBkColor($Button1, 0xA3A3A3)
GUICtrlSetBkColor($Button2, 0xA3A3A3)
GUICtrlSetBkColor($Button3, 0xA3A3A3)
GUICtrlSetBkColor($Button4, 0xA3A3A3)

GUISetState(@SW_SHOW, $GUI1)
WinSetOnTop($GUI1, "", 1)
#endregion ;GUI1

_StartUpCheck() ;Überprüft ob programm max 1x ausgeführt wird

#region ;Schleife
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE ;Programm schließen
            _kill()
        Case $Label1 ;Fokus auf Input1
            ControlClick("", "", $Input1)
        Case $Label2 ;Fokus auf Input2
            ControlClick("", "", $Input2)
        Case $Button1 ;Aufnahme starten
            _Start(GUICtrlRead($Input1), GUICtrlRead($Input2) * 1000)
        Case $Button2 ;LOG Löschen/öffnen
            If _IsPressed("11", "user32.dll") Then
                _Open($dir & $log)
            Else
                _Delete($dir & $log)
            EndIf
        Case $Button3 ;CONFIG Löschen/öffnen
            If _IsPressed("11", "user32.dll") Then
                _Open($dir & $config)
            Else
                _Delete($dir & $config)
                GUICtrlSetData($Input1, "")
                GUICtrlSetData($Input2, "")
            EndIf
        Case $Button4 ;Config speichern
            _SaveConfig(GUICtrlRead($Input1), GUICtrlRead($Input2))
    EndSwitch

    _Check() ;Überprüft GUI Buttons
WEnd
#endregion ;Schleife

#region ;Funktionen
Func _StartUpCheck() ;OK
    If _Singleton($prog, 1) = 0 Then ;OK
        MsgBox(262160, "Fehler!", $prog & " wird bereits ausgeführt!")
        _kill()
    EndIf
EndFunc   ;==>_StartUpCheck

Func _Start($anzahl, $abstand) ;
    GUISetState(@SW_DISABLE, $GUI1)
    GUICtrlSetBkColor($Input1, 0xFFFFFF)
    GUICtrlSetBkColor($Input2, 0xFFFFFF)

    If $anzahl = ("") Or $anzahl = ("0") Then
        GUICtrlSetBkColor($Input1, 0xFF0000)
        GUISetState(@SW_ENABLE, $GUI1)
        ControlClick("", "", $Input1)
        _WriteLog("Anzahl ist leer")
        Return
    Else
        GUICtrlSetBkColor($Input1, 0x00FF00)
    EndIf

    If $abstand = ("") Or $abstand = ("0") Then
        GUICtrlSetBkColor($Input2, 0xFF0000)
        GUISetState(@SW_ENABLE, $GUI1)
        ControlClick("", "", $Input2)
        _WriteLog("Abstand ist leer")
        Return
    Else
        GUICtrlSetBkColor($Input2, 0x00FF00)
    EndIf

    $fc = 0
    Do
        $fc += 1
    Until Not FileExists("Aufnahme_" & $fc & ".ini")
    $record = ("Aufnahme_" & $fc & ".ini")

    _WriteLog("=====[Aufnahme gestartet]=====")
    _WriteLog("Datei: " & $record)
    _WriteLog("Anzahl: " & $anzahl)
    _WriteLog("Abstand: " & $abstand)

    IniWrite($dir & $record, "Info", "Datei", $record)
    IniWrite($dir & $record, "Info", "Anzahl", $anzahl)
    IniWrite($dir & $record, "Info", "Abstand", $abstand)

    HotKeySet("{ESC}")
    GUISetState(@SW_HIDE, $GUI1)
    ToolTip("")
    MsgBox(262208, "Wichtig!", "Aufnahme wird nach dieser Meldung gestartet" & @CRLF & "ESC zum abbrechen der Aufzeichung!")

    $count = ("0")
    Do
        $MPos = MouseGetPos()
        ToolTip("Klick: " & $count & "/" & $anzahl & " | X: " & $MPos[0] & " Y: " & $MPos[1])

        If _IsPressed("01", "user32.dll") Then ;Links klick
            $count += 1
            IniWrite($dir & $record, "Aufnahme" & $count, "Klick", "Links")
            IniWrite($dir & $record, "Aufnahme" & $count, "X", $MPos[0])
            IniWrite($dir & $record, "Aufnahme" & $count, "Y", $MPos[1])
            _WriteLog("Aufnahme" & $count)
            _WriteLog("Links")
            _WriteLog("|X= " & $MPos[0])
            _WriteLog("|Y= " & $MPos[1])
            Do
                Sleep(10)
            Until Not _IsPressed("01", "user32.dll")
        EndIf

        If _IsPressed("02", "user32.dll") Then ;Rechts klick
            $count += 1
            IniWrite($dir & $record, "Aufnahme" & $count, "Klick", "Rechts")
            IniWrite($dir & $record, "Aufnahme" & $count, "X", $MPos[0])
            IniWrite($dir & $record, "Aufnahme" & $count, "Y", $MPos[1])
            _WriteLog("Aufnahme" & $count)
            _WriteLog("Rechts")
            _WriteLog("|X= " & $MPos[0])
            _WriteLog("|Y= " & $MPos[1])
            Do
                Sleep(10)
            Until Not _IsPressed("02", "user32.dll")
        EndIf

        If _IsPressed("1B", "user32.dll") Then ;ESC
            Do
                Sleep(10)
            Until Not _IsPressed("1B", "user32.dll")

            ExitLoop
        EndIf

        Sleep(10)
    Until $count = $anzahl

    _WriteLog("=====[Aufnahme beendet]=====")

    ToolTip("")
    If $count = $anzahl Then
        MsgBox(262208, "Wichtig!", "Aufzeichnung abgeschlossen!")
    Else
        MsgBox(262160, "Wichtig!", "Aufzeichung abgebrochen!")
    EndIf

    GUISetState(@SW_SHOW, $GUI1)
    HotKeySet("{ESC}", "_kill")

    GUICtrlSetBkColor($Input1, 0xFFFFFF)
    GUICtrlSetBkColor($Input2, 0xFFFFFF)
    GUISetState(@SW_ENABLE, $GUI1)
EndFunc   ;==>_Start

Func _SaveConfig($anzahl, $abstand) ;OK
    GUISetState(@SW_DISABLE, $GUI1)
    GUICtrlSetBkColor($Input1, 0xFFFFFF)
    GUICtrlSetBkColor($Input2, 0xFFFFFF)

    If $anzahl = ("") Or $anzahl = ("0") Then
        GUICtrlSetBkColor($Input1, 0xFF0000)
        GUISetState(@SW_ENABLE, $GUI1)
        ControlClick("", "", $Input1)
        _WriteLog("Anzahl ist leer")
        Return
    Else
        GUICtrlSetBkColor($Input1, 0x00FF00)
    EndIf

    If $abstand = ("") Or $abstand = ("0") Then
        GUICtrlSetBkColor($Input2, 0xFF0000)
        GUISetState(@SW_ENABLE, $GUI1)
        ControlClick("", "", $Input2)
        _WriteLog("Abstand ist leer")
        Return
    Else
        GUICtrlSetBkColor($Input2, 0x00FF00)
    EndIf

    IniWrite($dir & $config, "CONFIG", "ANZAHL", $anzahl)
    IniWrite($dir & $config, "CONFIG", "ABSTAND", $abstand)
    _WriteLog("Konfiguration gespeichert!")
    GUICtrlSetBkColor($Input1, 0xFFFFFF)
    GUICtrlSetBkColor($Input2, 0xFFFFFF)
    GUISetState(@SW_ENABLE, $GUI1)
EndFunc   ;==>_SaveConfig

Func _Check() ;OK
    ;Sperrt und entsperrt Button2
    If FileExists($dir & $log) And GUICtrlGetState($Button2) = ("144") Then
        GUICtrlSetState($Button2, $GUI_ENABLE)
    ElseIf Not FileExists($dir & $log) And GUICtrlGetState($Button2) = ("80") Then
        GUICtrlSetState($Button2, $GUI_DISABLE)
    EndIf

    ;Sperrt und entsperrt Button3
    If FileExists($dir & $config) And GUICtrlGetState($Button3) = ("144") Then
        GUICtrlSetState($Button3, $GUI_ENABLE)
    ElseIf Not FileExists($dir & $config) And GUICtrlGetState($Button3) = ("80") Then
        GUICtrlSetState($Button3, $GUI_DISABLE)
    EndIf

    ;Änder Button2 Text
    If _IsPressed("11", "user32.dll") And GUICtrlRead($Button2) = ("Log löschen") And GUICtrlGetState($Button2) = ("80") Then
        GUICtrlSetData($Button2, "Log öffnen")
    ElseIf Not _IsPressed("11", "user32.dll") And GUICtrlRead($Button2) = ("Log öffnen") Then
        GUICtrlSetData($Button2, "Log löschen")
    EndIf

    ;Änder Button3 Text
    If _IsPressed("11", "user32.dll") And GUICtrlRead($Button3) = ("Config löschen") And GUICtrlGetState($Button3) = ("80") Then
        GUICtrlSetData($Button3, "Config öffnen")
    ElseIf Not _IsPressed("11", "user32.dll") And GUICtrlRead($Button3) = ("Config öffnen") Then
        GUICtrlSetData($Button3, "Config löschen")
    EndIf
EndFunc   ;==>_Check

Func _Open($data) ;OK
    GUISetState(@SW_DISABLE, $GUI1)
    If FileExists($data) Then ShellExecuteWait("C:\Windows\system32\notepad.exe", $data)
    GUISetState(@SW_ENABLE, $GUI1)
EndFunc   ;==>_Open

Func _Delete($data) ;OK
    GUISetState(@SW_DISABLE, $GUI1)
    If FileExists($data) Then FileDelete($data)
    GUISetState(@SW_ENABLE, $GUI1)
EndFunc   ;==>_Delete

Func _WriteLog($text) ;OK
    FileWriteLine($dir & $log, _NowDate() & "|" & _NowTime(5) & " > " & $text)
EndFunc   ;==>_WriteLog

Func _kill() ;OK
    Exit
EndFunc   ;==>_kill
#endregion ;Funktionen