#SingleInstance force
SetTitleMatchMode, 2
SetWorkingDir, %A_MyDocuments%\Documentation
; These next 3 options make it so that it's always on top, it there are no buttons, and doesn't show up in the alt-tab or taskbar
Gui,+AlwaysOnTop
Gui, -SysMenu
Gui, +ToolWindow

Holder = %ClipboardAll% ; That way you can get what you had in your clipboard back
Email = ""
Temp = Temp.txt ; Location for the file where documentation is done

UTCTimestamp := A_NowUTC    ; Grab the current time and date
UTCFormatStr := "yyyy-MM-dd"    ; This is what we want it to look like
FormatTime, DateStr, %UTCTimestamp%, %UTCFormatStr% ; format it to make it look like the Date we want
UTCFormatStr := "H:mm'Z'"   ; The time, with a Z at the end to denote the "Zulu" timezone
FormatTime, TimeStr, %UTCTimestamp%, %UTCFormatStr% ; Format the string to be better

Gui, Add, Edit, x12 y43 w55 h20 vXID, x ; xID field
Gui, Add, Text, x105 y46 w100 h20, Win+F3 to hang up

Gui, Add, Radio, x12 y3 w60 h20 Group vLang Checked, &English   ; First group of Radio buttons
Gui, Add, Radio, x12 y23 w60 h20, &German

;   The & in front of the letters lets you hold alt and press the first letter instead of having to click on it. That is why its "Mail", not "E-Mail"

Gui, Add, Radio, x82 y3 w60 h20 Group vContact Checked, &Mail   ; Second group of Radio buttons
Gui, Add, Radio, x82 y23 w60 h20, &Call

Gui, Add, Radio, x142 y3 w80 h20 Group vAct, &Warrant
Gui, Add, Radio, x142 y23 w80 h20 , &Demo

Gui, Add, Button, x22 y43 w70 h20 Default vHidden, Ok   ; That way you don't have to have an actual button, it just happens when you press enter.
Gui, Show, h74 w210, Contact
GuiControl, Hide, Hidden    ; Hide the button to be hidden
Return



ButtonOk:
    Gui, submit
    If RegExMatch(XID, ".......")   ; each dot represents a character, xIDs are always only that long.
    {   WinActivate, CASE TEMPLATE
        ;Click, 475, 95  ; Click on the xID Field
        Send, %XID% ; Sends the xID
        Sleep, 100
        Send, {Tab} {Enter}  ; Press the "Verify" button
        Sleep, 250

        WinActivate, Dealer Info    ; Make sure the Dealer info window is focused
        Send, {Tab}
        Sleep, 100
        Send, {CtrlDown}a{CtrlUp}   ; Select All
        Sleep, 100
        Send, {CtrlDown}c{CtrlUp}   ; Copy
        XID = %Clipboard%   ; Set the XID variable to what was just copied to the clipboard
        Send, {Tab} ; Next Field
        Sleep, 100
        Send, {CtrlDown}a{CtrlUp}   ; Select All
        Sleep, 100
        Send, {CtrlDown}c{CtrlUp}   ; Copy
        CTname = %Clipboard%  ; Set the XID variable to what was just copied to the clipboard
        Send, {Tab} ; Next Field
        Sleep, 100
        Send, {CtrlDown}a{CtrlUp}   ; Select All
        Sleep, 100
        Send, {CtrlDown}c{CtrlUp}   ; Copy
        DID = %Clipboard%   ; Set the XID variable to what was just copied to the clipboard
        WinActivate, Dealer Info    ; Make sure to have the right window in focus, for the next command
        Send, !{F4} ; Alt+F4
        
        WinActivate, CASE TEMPLATE  ; Switch back to the Case Template
        SendInput, +{Tab 57} {Enter} ; Tab to the CCMS button, clicking at a certain pixel is too inconsistent, and this is just as fast
        
        WinActivate, windCCMSQuery  ; make sure that window is active
        Sleep, 1000  ; makes sure ou can read it
        Send, {Esc} ; had to change the exit  this script to Shift+Escape for this cause this line was causing the program to exit...
        Sleep, 100 
        SendInput, +{Tab} {Enter} ; Tab to the Clear button
        Sleep, 100

        SendInput, {Tab 57} ; Tab to the xID field, clicking at a certain pixel is too inconsistent, and this is just as fast

        WinMinimize, CASE TEMPLATE  ; minimize the case template cause its ugly as all hecc

        Email := GetEmail(XID)
        name := GetName(xID)
        If (name <> ""){
            NAME := name
        }
        Else {
            NAME := CTname
        }
        Goto, Write
    }
    Else    ; This is if it does not match the RegEx all the way up there
    MsgBox, Wrong, Invalid xID: xID entered was not 7 characters.

/*
Contact
Mail: 1
Call: 2

Lang
English: 1
German: 2

Act
Warrant: 1
Demo: 2
*/
; Email
Write:
FileRead, NewLine, %Temp%     ; Read the documentation file
If (NewLine != "")              ; If there is something in the file, this would return a string
{
    FileAppend,`n`n`n`n, %Temp%      ; Inserts a new line so that it isn't just appended directly to the end
}
NewLine := ""                   ; Free up Memory cause this could be quite a bit of stuff

If (Contact = "1")
{
    If (Lang = "1")     ; English
    {
        FileAppend, Name: %NAME%`nxID: %XID%`nDealer `#: %DID%`nEmail: %Email%`n, %Temp%

        If (Act = "0")      ; Not an activation
        {
            FileAppend,`nDealer Emailed: "", %Temp%
        }
        
        If (Act = "1")      ; Warrant
        {
            FileAppend,`nDealer Emailed for warrant transfer: ""`n`nOld SN: `nNew SN: `nChallenge Code:, %Temp%
        }

        If (Act = "2")      ; Demo
        {
            FileAppend,`nDealer Emailed: ""`n`nSN: `nChallenge Code: `n`n, %Temp%
        }
    }

    Else If (Lang = "2") ; German
    {
        FileAppend, Name: %NAME%`nxID: %XID%`nHändler `#: %DID%`nEmail: %Email%`n, %Temp%
        

        If (Act = "0")      ; Not an activation
        {
            FileAppend,`nHändler Emailte: "", %Temp%
        }

        Else If (Act = "1") ; Warrant
        {
            FileAppend,`nHändler Emailte für Garantieübertragung`n`nAlte SN: `nNeue SN: `nAuthentisierungscode:, %Temp%
        }

        Else If (Act = "2") ; Demo
        {
            FileAppend,`nHändler Emailte: ""`n`nSN: `nAuthentisierungscode: `n`n, %Temp%
        }
    }
}

; Call
Else If (Contact = "2")
{
    If (Lang = "1") ; English
    {
        FileAppend, Call received on %DateStr% at %TimeStr%`nName: %NAME%`nxID: %XID%`nDealer `#: %DID%`nEmail: %Email%`n, %Temp%
        If (Act = "0")     ; Not an activation
        {
            FileAppend,`nDealer called for, %Temp%
        }

        Else If (Act = "1")     ; Warrant
        {
            FileAppend,`nDealer called for Warrant transfer`nOld SN: `nNew SN: `nChallenge Code: `n`n, %Temp%
        }

        Else If (Act = "2")     ; Demo
        {
            FileAppend,`nDealer called for Demo`nSN: `nChallenge Code: `n`n, %Temp%
        }
    }

    Else If (Lang    = "2")   ; German
    {
        FileAppend, Anruf wurde am %DateStr% um %TimeStr% entgegengenommen`nName: %NAME%`nxID: %XID%`nHändler `#: %DID%`nEmail: %Email%`n, %Temp% 

        If (Act = "0")     ; Not an activation
        {
            FileAppend,`nHändler rief an mit, %Temp% 
        }

        Else If (Act = "1")     ; Warrant
        {
            FileAppend,`nHändler rief an für Garantieübertragung`nAlte SN: `nNeue SN: `nAuthentisierungscode: `n`n, %Temp%
        }

        Else If (Act = "2")     ; Demo
        {
            FileAppend,`nHändler rief an für Demo`nSN: `nAuthentisierungscode: `n`n, %Temp%
        }
    }
}


WinActivate, Temp.txt   ; Switch to the txt file where the notes are being taken
Clipboard = %Holder%    ; Gives you back what you had in your Clipboard
ExitApp

+esc::ExitApp   ; Shift+Escape will cancel it at any time before its typing it out into the txt file

/*
Functions
*/

GetEmail(xID){
    Loop, Read, %A_MyDocuments%\Documentation\emails.csv    ; read every line of "emails.csv"
        {
            Array := StrSplit(A_LoopReadLine,",")	; split the line at its separating commas to get separate "columns" AKA elements
            if InStr(Array[1],xID)					; if at the specified element (1) the specified content (dealer's xID) has been found,
            Break                                   ; stop searching
        }
    Return % Array[2]								; return the content of the specified element (2), which is the email
    }

GetName(xID){
    Loop, Read, %A_MyDocuments%\Documentation\emails.csv    ; read every line of "emails.csv"
        {
            Array := StrSplit(A_LoopReadLine,",")	; split the line at its separating commas to get separate "columns" AKA elements
            if InStr(Array[1],xID)					; if at the specified element (1) the specified content (dealer's xID) has been found,
            Break                                   ; stop searching
        }
    Return % Array[3]								; return the content of the specified element (3) which is the name of the dealer in proper capitalization
    }

/*
If there are any other columns added to the CSV, just copy the above and change the # in "Return % Array[#]" to match its Column Number.
Different name for it too of course.
*/
