extends Node

var player : int
var turn_player : int
var board_size : int
var cell_size : int
var grid_pos : Vector2i 
var grid_data : Array # matrice 8 pe 8
var start_pos : Vector2i # retine primul click dat / piesa pe care se doreste a muta
var start_piesa : int
# Called when the node enters the scene tree for the first time.
func _ready():
	board_size = $Board.texture.get_width()
	# imparte board_size la 8 pentru a obtine dimensiunea fiecarei celule
	cell_size = board_size / 8
	turn_player = 1
	new_game()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	
func _input(event):
	if event is InputEventMouseButton :
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed :
			#verifica daca mouse-ul e pe tabla de jo
			if event.position.x < board_size:
				#convertire pozitie mouse in locatie grid
				grid_pos = Vector2i(event.position / cell_size) # grid_pos memoreza [coloana, linie]
				
				#verificare daca in chenar este piesa player-ului care a dat click:
				#pt player = 1
				if grid_data[grid_pos.y][grid_pos.x] > 0 and player == 1 :
					start_piesa = grid_data[grid_pos.y][grid_pos.x]
					start_pos = grid_pos
					print("player", player, "a apasat pe chenarul ", grid_pos )
					print("a fost selectata piesa ", start_piesa)
					turn_player = 2
				# verificare daca al doi-Lea click este pe o pozitie libera
				if grid_pos != start_pos and player == 1:
						print("trece de primul if")
						if grid_data[grid_pos.y][grid_pos.x] == 0:
							#marcare noua pozitie a piesei
							grid_data[grid_pos.y][grid_pos.x] = start_piesa
							#demarcare vechea pozitie a piesei
							grid_data[start_pos.y][start_pos.x] = 0
							print(grid_pos)
							print(grid_data)
							turn_player = 3
				
				# pt player 2
				if player == -1 and grid_data[grid_pos.y][grid_pos.x] < 0 :
					start_piesa = grid_data[grid_pos.y][grid_pos.x]
					start_pos = grid_pos
					print("player", player, "a apasat pe chenarul ", grid_pos )
					print("a fost selectata piesa ", start_piesa)
				# verificare daca al doi-Lea click este pe o pozitie libera
				if grid_pos != start_pos and player == -1:
						print("trece de primul if")
						if grid_data[grid_pos.y][grid_pos.x] == 0:
							#marcare noua pozitie a piesei
							grid_data[grid_pos.y][grid_pos.x] = start_piesa
							#demarcare vechea pozitie a piesei
							grid_data[start_pos.y][start_pos.x] = 0
							print(grid_pos)
							print(grid_data)
							
				#schimbare player						
							
func new_game():
	player = 1
	grid_data = [
		[0, 6, 0, 0, 0, 0, -6, 0],
		[0, 6, 0, 0, 0, 0, -6, 0], 
		[0, 6, 0, 0, 0, 0, -6, 0],
		[0, 6, 0, 0, 0, 0, -6, 0],
		[0, 6, 0, 0, 0, 0, -6, 0],
		[0, 6, 0, 0, 0, 0, -6, 0],
		[0, 6, 0, 0, 0, 0, -6, 0],
		[0, 6, 0, 0, 0, 0, -6, 0]
		]
		

