extends Node2D


var grid = []
var grid_width = 13
var grid_height = 15
var cycletimer = 0
var scoreblock = 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
	

	

# 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
		
		# 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]):
								grid[nX][nY-5] = 0
								scoreblock = scoreblock+1
								
							grid[nX][nY-4] = 0
							scoreblock = scoreblock+1
								
						grid[nX][nY-3] = 0
						scoreblock = scoreblock+1
					grid[nX][nY] = 0
					grid[nX][nY-1] = 0
					grid[nX][nY-2] = 0
					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]):
						grid[nX-3][nY] = 0
						scoreblock = scoreblock+1
						if(grid[nX][nY] == grid[nX-4][nY]):
							grid[nX-4][nY] = 0
							scoreblock = scoreblock+1
							if(grid[nX][nY] == grid[nX-5][nY]):
								grid[nX-5][nY] = 0
								scoreblock = scoreblock+1
					grid[nX][nY] = 0
					grid[nX-1][nY] = 0
					grid[nX-2][nY] = 0
					scoreblock = scoreblock+3
							
		nodeB.frame = grid[nX][nY]
		a = a +1
		
	if(grid[4][0] != 0):
		$Column.gameover = true
		$Control/LabelGameover.visible = true
		print("Game over man")
		
	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(grid[x][y+1] == 0 and y+1 < 9):
		$Column.moveDown()
	else:
		landing(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_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()
