extends Node2D


var grid = []
var grid_width = 13
var grid_height = 15
var cycletimer = 0
var scoreblock = 0

var warning = false

# 0 = down, 1 = right
var bdir = 0


var hiscore = 0
var block = preload("res://Block.tscn")

var score = 0

const SCOREX3 = 10
const SCOREX4 = 15
const CELLSIZE = 64
const CYCLEL = 10
const BLOCKMAX = 81







func _ready():
	#hi score file loading and cheking
	var hiscorefile = File.new()
	var doFileExists = hiscorefile.file_exists("user://savefile.save")
	print(doFileExists)
	
	if(doFileExists):
		hiscorefile.open("user://savefile.save", File.READ)
		hiscore = int(hiscorefile.get_as_text())
		print(hiscorefile.get_as_text())

	
	for i in grid_width:
		grid.append([])
		for j in grid_height:
			grid[i].append(0) # Set a starter value for each position
	






# set column blocks to the board
func landing(x, y, b1, b2, b3):
	grid[x][y] = b1
	grid[x][y-1] = b2
	grid[x][y-2] = b3
	bdir = 1
func landing2(x, y, b1, b2, b3):
	grid[x][y] = b1
	grid[x-1][y] = b2
	grid[x-2][y] = b3
	bdir = 0
	

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	
	$Control/HiScore.text = String(hiscore)
	$Control/Score.text = String(score)
	#not to be mistaken for inter column cycling, this is the game ticking until the block should move down again
	var a = 1
	while(a < BLOCKMAX+1):
		var nodeB = get_node("B"+ str(a))	
		var nX = (nodeB.position.x-32)/CELLSIZE
		var nY = (nodeB.position.y-32)/CELLSIZE
		
		var rXY = false
		var rX1Y = false
		var rX2Y = false
		var rXY1 = false
		var rXY2 = false
		var rX3Y = false
		var rX4Y = false
		var rX5Y = false
		var rXY3 = false
		var rXY4 = false
		var rXY5 = false
		
		# drop block down
		if(grid[nX][nY+1] == 0 and grid[nX][nY] != 0 and nY+1 < 9):
			grid[nX][nY+1] = grid[nX][nY]
			grid[nX][nY] = 0
		
		if(grid[(nodeB.position.x-32)/CELLSIZE][(nodeB.position.y-32)/CELLSIZE] > 4):
			grid[(nodeB.position.x-32)/CELLSIZE][(nodeB.position.y-32)/CELLSIZE] = 4
			print("grid", (nodeB.position.x-32)/CELLSIZE, (nodeB.position.y-32)/CELLSIZE, " is ", grid[(nodeB.position.x-32)/CELLSIZE][(nodeB.position.y-32)/CELLSIZE])
		
		
		if(grid[nX][nY] != 0):
			if(grid[nX][nY] == grid[nX][nY+1]):
				if(grid[nX][nY] == grid[nX][nY+2]):
					if(grid[nX][nY] == grid[nX][nY+3]):
						
						if(grid[nX][nY] == grid[nX][nY+4]):
							
							if(grid[nX][nY] == grid[nX][nY+5]):
								rXY5 = true
								scoreblock = scoreblock+1
								
							rXY4 = true
							scoreblock = scoreblock+1
								
						rXY3 = true
						scoreblock = scoreblock+1
					rXY = true
					rXY1 = true
					rXY2 = true
					scoreblock = scoreblock+3
		if(grid[nX][nY] != 0):
			if(grid[nX][nY] == grid[nX+1][nY]):
				if(grid[nX][nY] == grid[nX+2][nY]):
					if(grid[nX][nY] == grid[nX+3][nY]):
						rX3Y = true
						scoreblock = scoreblock+1
						if(grid[nX][nY] == grid[nX+4][nY]):
							rX4Y = true
							scoreblock = scoreblock+1
							if(grid[nX][nY] == grid[nX+5][nY]):
								rX5Y = true
								scoreblock = scoreblock+1
					
					rXY = true
					rX1Y = true
					rX2Y = true
					scoreblock = scoreblock+3
							
		
		if(rXY):
			grid[nX][nY] = 0
		if(rXY1):
			grid[nX][nY+1] = 0
		if(rXY2):
			grid[nX][nY+2] = 0
		if(rXY3):
			grid[nX][nY+3] = 0
		if(rXY4):
			grid[nX][nY+4] = 0
		if(rXY5):
			grid[nX][nY+5] = 0
			
			
		if(rX1Y):
			grid[nX+1][nY] = 0
		if(rX2Y):
			grid[nX+2][nY] = 0
		if(rX3Y):
			grid[nX+3][nY] = 0
		if(rX4Y):
			grid[nX+4][nY] = 0
		if(rX5Y):
			grid[nX+5][nY] = 0
		
		
		nodeB.frame = grid[nX][nY]
		a = a +1
		
	if(grid[4][0] != 0 or grid[0][4] != 0):
		if(warning):
			$Column.gameover = true
			$Control/LabelGameover.visible = true
			print("Game over man")
		else:
			warning = true
	else:
		warning = false
	
		
	if(scoreblock > 0 and $Column.gameover != true):
		if(scoreblock > 3):
			score = score + scoreblock*SCOREX4
		else:
			score = score + scoreblock*SCOREX3
		
		scoreblock = 0
		
		
		#$Coins.text = String(coinvalue)
		print(score)
		
		
	if(Input.is_action_just_pressed("Reset")):
		if(score > hiscore):
			hiscore = score
			save_score()
		get_tree().reload_current_scene()




func _on_Column_checkifsolid(x, y, b1, b2, b3):
	if(bdir == 0):
		if(grid[x][y+1] == 0 and y+1 < 9):
			$Column.moveDown()
		else:	
			landing(x, y, b1, b2, b3)
			$Column.reset()
	elif(bdir == 1):
		if(grid[x+1][y] == 0 and x+1 < 9):
			$Column.moveRight()
		else:	
			landing2(x, y, b1, b2, b3)
			$Column.reset()	


func _on_Column_checkifsolid2(x, y, b1, b2, b3, dir):
	if(dir == 0):
		#Check left
		if(grid[x-1][y] == 0 and grid[x-1][y-1] == 0 and grid[x-1][y-2] == 0 and x > 0):
			$Column.moveLeft()
			pass
	elif(dir == 1):
		#Check left
		if(grid[x+1][y] == 0 and grid[x+1][y-1] == 0 and grid[x+1][y-2] == 0 and x < 8):
			$Column.moveRight()
			pass

func _on_Column_checkifsolid3(x, y, b1, b2, b3, dir):
	print("Checking", dir)
	if(dir == 0):
		#Check UP
		if(grid[x][y-1] == 0 and grid[x-1][y-1] == 0 and grid[x-2][y-1] == 0 and y > 0):
			$Column.moveUp()
			pass
	elif(dir == 1):
		#Check dpwm
		if(grid[x][y+1] == 0 and grid[x-1][y+1] == 0 and grid[x-2][y+1] == 0 and y < 8):
			$Column.moveDown()
			pass



func _on_Column_tellblocks(b1, b2, b3):
	$WColumn/S1.frame = b1
	$WColumn/S2.frame = b2
	$WColumn/S3.frame = b3
	
func save_score():
	print("Saving...")

	var save_file = File.new()
	save_file.open("user://savefile.save", File.WRITE)
	save_file.store_line(String(score))
	save_file.close()
