#include <File.au3>
#include <Array.au3>

$count = 0
$count = 0
$count2 = 0
$double = 0
$start = 0
$cDifferences = 0
$cYzush = 0
$cut = 0
$cONE = 0
$cTWO = 0
Local $a1, $b1, $a2, $b2, $c1, $c2, $c3, $newLine, $akt, $aXY, $mR, $sX, $sY, $look, $Set
Local $aNxy[1][8]
Local $cC[1][2]
Opt('MouseClickDownDelay', 1)
Opt('MouseClickDelay', 1)
;Koordinaten in Array lesen
If Not _FileReadToArray(@ScriptDir & '\coords.txt', $aXY) Then
	Dim $nofile = 0
	MsgBox(4096, "Fehler", "Keine Koordinaten vorhanden.     error:" & @error)
ElseIf _FileReadToArray(@ScriptDir & '\coords.txt', $aXY) Then
	$nofile = 1
EndIf
If Not _FileCreate(@ScriptDir & '\midCoords.txt') Then
	MsgBox(4096, "Fehler", "Fehler beim Erstellen/Zurücksetzen der txt.-Datei: " & @error)
EndIf
;Ausgabedatei
$fNewCoords = FileOpen(@ScriptDir & '\midCoords.txt', 9)
If $fNewCoords = -1 Then
	MsgBox(0, "Fehler", "Die Datei konnte nicht geöffnet werden.")
	Exit
EndIf
Local $aCoords[($aXY[0] + 1)][7]
$aNxy[0][0] = 'Nr.'
$aNxy[0][1] = 'x'
$aNxy[0][2] = 'y'
$aNxy[0][3] = 'Times'
$aNxy[0][4] = '1st X,y'
$aNxy[0][5] = 'mid x'
$aNxy[0][6] = 'last x,y'
$aNxy[0][7] = 'mid-y'
$aCoords[0][0] = 'Nr.'
$aCoords[0][1] = 'x'
$aCoords[0][2] = 'y'
$aCoords[0][3] = 'Times'
$aCoords[0][4] = 'cDiff'
$aCoords[0][5] = 'Y-zus'
$aCoords[0][6] = 'Y-Punkt' ; Punkt auf Strich

For $n = 1 To $aXY[0]
	ReDim $cC[UBound($cC) + 1][2]
	$split = StringSplit($aXY[$n], ',', 1)
	$cC[$n][0] = $split[1]
	$cC[$n][1] = $split[2]
	;Wenn nebenliegendes Y auch vorhanden
	If $split[2] - 1 = $cC[$n - 1][1] Then
		$cYzush = $cYzush + 1
	Else
		$cYzush = 0
	EndIf
	If $n - 1 = 0 Then
		$aCoords[$n][0] = $n
		$aCoords[$n][1] = $split[1]
		$aCoords[$n][2] = $split[2]
		$aCoords[$n][3] = 1
		$aCoords[$n][4] = $cDifferences
		$aCoords[$n][6] = 1
		$count = $count + 1
	Else
		If $split[1] = $aCoords[($n - 1)][1] Then
			$count = $count + 1
			$aCoords[$n][0] = $n
			$aCoords[$n][1] = $split[1]
			$aCoords[$n][2] = $split[2]
			$aCoords[$n][3] = $count
			$aCoords[$n - 1][4] = $cDifferences + 1
			;$aCoords[$n][5] = $YConn ;Y-conn
			$sXold = $aCoords[$n][1]
			$sYold = $aCoords[$n][2]
			If $count = 2 Then
				$aCoords[$n][6] = 1
			EndIf
		ElseIf $split[1] <> $aCoords[($n - 1)][1] And $n > 1 Then
			$cDifferences = $cDifferences + 1
			$aCoords[$n][0] = $n ;Stelle
			$aCoords[$n][1] = $split[1] ;X Coord
			$aCoords[$n][2] = $split[2] ;Y Coord
			$aCoords[$n][3] = 1 ;x n-mal
			$aCoords[$n - 1][4] = $cDifferences ;diff n-mal
			;$aCoords[$n][5] = $YConn ;Y-conn
			$aCoords[$n - 2][6] = 1
			ReDim $aNxy[UBound($aNxy) + 1][8]
			$aNxy[0][0] = 'Nr.'
			$aNxy[0][1] = 'M-x'
			$aNxy[0][2] = 'M-y'
			$aNxy[0][3] = 'Times'
			$aNxy[0][4] = '1st X,y'
			$aNxy[0][5] = 'mid x,y'
			$aNxy[0][6] = 'last x,y'
			$aNxy[0][7] = 'mid-y'
			;Declare X-diff Array
			$aNxy[$cDifferences][0] = $cDifferences ;Stelle
			$aNxy[$cDifferences][3] = $count ;Times
			$aNxy[$cDifferences][4] = $aXY[$n - $count] ;1st X,y
			$aNxy[$cDifferences][5] = $aXY[$n - Ceiling($count / 2)] ;mid X,y
			$aNxy[$cDifferences][6] = $aXY[$n - 1] ;last X,y
			$count = 1
		EndIf
		If $split[1] = $aCoords[($n - 1)][1] And $split[2] <> ($aCoords[($n - 1)][1] + 2) And $count > 2 And $aCoords[$n][6] = 1 Then
			$aCoords[$n - 2][6] = 1
		EndIf
	EndIf
	;Wenn 2 Y nebeneinander
	If $cYzush >= 2 Then
		$YConn = 1
	Else
		$YConn = 0
	EndIf
	If $cYzush = 0 Then
		$aCoords[$n - 1][5] = $YConn ;Y-conn
	EndIf
	;Wenn Y mehr/gleich 2 nebeneinanderliegende
	If $YConn = 0 Then
		$aNxy[$cDifferences][7] = 1 ;Y verbunden
	Else
		$aCoords[$n - 1][5] = $YConn ;Y-conn
		$aNxy[$cDifferences][7] = 0 ;Y getrennt
	EndIf
	$aCoords[$n][5] = $YConn ;Y-conn
	If $aCoords[$n][2] = ($aCoords[$n - 1][2] + 1) And $n > 2 Then
		$cXzush = 1
	Else
		$cXzush = 0
	EndIf
	If $aCoords[$n][5] = 1 And $aCoords[$n - 1][5] = 0 Then
		$start = $aCoords[$n - 1][0]
	EndIf
	If $aCoords[$n][5] = 0 And $aCoords[$n - 1][5] = 1 Then
		$end = $aCoords[$n][0]
		$mid = Ceiling(Round(($end - $start) / 2))
		$aCoords[$n - $mid][6] = 1
	EndIf
	If $aCoords[$n][1] = $aCoords[$n - 1][1] And $aCoords[$n][2] <> ($aCoords[$n - 1][2] + 1) Then
		$cutA = $aCoords[$n][0]
		$aCoords[$n - 2][6] = 1
	EndIf
Next
For $n = 1 To $aXY[0]
	If $aCoords[$n][1] = $aCoords[$n - 1][1] And $aCoords[$n][2] <> ($aCoords[$n - 1][2] + 1) Then
		$newLine = 1
	EndIf
	If $aCoords[$n][1] <> $aCoords[$n - 1][1] Or $aCoords[$n][2] <> ($aCoords[$n - 1][2] + 1) Then
		$newLine = 0
		$set = 0
	EndIf
Next
For $n = 1 To $aXY[0]
	If $aCoords[$n][6] = 1 And $aCoords[$n][5] = 1 And $akt = 0 Then
		$start = $aCoords[$n - 1][0]
		;ConsoleWrite($start & ',')
		$akt = 1
	EndIf
	If $aCoords[$n][1] <> $aCoords[$n - 1][1] Or $aCoords[$n][4] <> $aCoords[$n - 1][4] Then
		$end = $aCoords[$n][0]
		$akt = 0
	EndIf
	If $aCoords[$n][5] = 0 And $akt = 1 Then
		$end = $aCoords[$n][0]
		$mid = Ceiling(Round((($end - $start) / 2)))
		$a = $aCoords[$n][0]
		If $aCoords[$end][3] > 4 Then
			If $set = 0 Then
				$aCoords[$n - $mid][6] = 2
				$a = $aCoords[$n - $mid][0]
				$set = 1
			EndIf
			If $set = 1 And $a <> $aCoords[$n - $mid][0] Then
				$aCoords[$n - $mid][6] = 3
				$set = 2
			EndIf
		Else
			$aCoords[$n - $mid][6] = 1
		EndIf
		$akt = 0
	EndIf
	If $aCoords[$n][2] <> ($aCoords[$n - 1][2] + 1) Then
		$set = 0
	EndIf
Next
For $n = 1 To $aXY[0]
	If $aCoords[$n][6] = 2 And $cTWO = 1 Then
		$aCoords[$n][6] = 3
		$cTWO = 0
	EndIf
	If $aCoords[$n][6] = 1 Then
		$cTWO = 0
	EndIf
	If $aCoords[$n][6] = 2 Then
		$cTWO = 1
	EndIf
	If $aCoords[$n - 1][6] = 1 And $aCoords[$n][6] = 1 Then
		$aCoords[$n - 1][6] = ''
	EndIf
Next

_ArrayDisplay($aCoords)
;_ArrayDisplay($aNxy)

For $i = 1 To UBound($aNxy) - 1
	FileWrite($fNewCoords, $aNxy[$i][5] & @CRLF)
Next
;_1_ende

;~ $pNotepad = Run(@WindowsDir & "\Notepad.exe")
;~ $hNP = WinGetHandle($pNotepad)
;~ WinWait($hNP)
;~ $NPSize = WinGetPos($hNP)
;~ $pX = $NPSize[0] + 150
;~ $pY = $NPSize[1] + 150
;~ If $nofile = 1 Then
;~ 	For $x = 1 To $aXY[0]
;~ 		$datasplit = StringSplit($aXY[$x], ',', 1)
;~ 		MouseMove(($datasplit[1] + $pX), ($datasplit[2] + $pY), 1)
;~ 		MouseDown('left')
;~ 		Sleep(1)
;~ 		If $datasplit[1] <> $oldXcoord Then
;~ 			MouseUp('left')
;~ 		EndIf
;~ 		If $datasplit[2] <> $oldYcoord + 1 Then
;~ 			MouseUp('left')
;~ 		EndIf
;~ 	Next
;~ EndIf
;~ MouseUp('left')