#=
move_user_move.jl <filename> <xsource> <ysource> <xtarget> <ytarget> <promote>
-moves the piece at the source into the target area
-if promote is ”T”, promote the piece after completed the move
-do not attempt to validate the move
=#
module move_user_move
push!(LOAD_PATH,pwd())
using SQLite
using functions

using databases
#connect to database
db = SQLite.DB("database.db")

#######################################################################################################################################################################################
#sets the board according to the given database
function database_to_board(board)
  query1 = "SELECT * FROM Player WHERE player = 1;"
  p1_table=SQLite.query(db,query1)
  l1=size(p1_table)[1]
  query2 = "SELECT * FROM Player WHERE player = 0;"
  p2_table=SQLite.query(db,query2)
  l2=size(p2_table)[1]
  for i = 1:l1
    x = get(p1_table[2][i])
    y = get(p1_table[3][i])
    status = get(p1_table[4][i])
    promo = get(p1_table[5][i])
    piece = get(p1_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),1)
      else
        board[x,y] = chess(x,y,status,promo,piece,1)
      end
	end
  end
  
  for i = 1:l2
    x = get(p2_table[2][i])
    y = get(p2_table[3][i])
    status = get(p2_table[4][i])
    promo = get(p2_table[5][i])
    piece = get(p2_table[6][i])[1]
    if status == 1
      if promo == 1
        board[x,y] = chess(x,y,status,promo,uppercase(piece),0)
      else
        board[x,y] = chess(x,y,status,promo,piece,0)
      end
    end
 end
end

#######################################################################################################################################################################################




########################################################################################################################################################################################

#check if piece exist at source x y and set up board

function Take_inputs(db,gamesize,now_play)
  results = check_win(db)
  if results == 1
	println("\nGame Ended!\n")
	include("play.jl")
  end
  print("Please enter source x: ")
  s_x = readline(STDIN)
  s_x=parse(s_x)
  print("Please enter source y: ")
  s_y = readline(STDIN)
  s_y=parse(s_y)
  print("Please enter target x: ")
  t_x = readline(STDIN)
  t_x=parse(t_x)
  print("Please enter target y: ")
  t_y = readline(STDIN)
  t_y=parse(t_y)
  
  if board[s_x,s_y].view == '.'
    println("Error: There is no chess piece on the source position!")
	println("Enter valid input")
	quit()
  end
  
  if board[s_x,s_y].view != '.' && board[s_x,s_y].player != now_play
    println("Error: You are moving another player's piece")
	println("Enter valid input")
	quit()
  end
  
  if board[t_x,t_y].player == now_play
	println(board[t_x,t_y].view)
	println("Error: You are killing your own player")
	println("Enter valid input")
	println(now_play)
	quit()
  end
  t_x2=0
  t_y2=0
  
  if gamesize == 12
	print("Please enter target x 2:")
	t_x2 = readline(STDIN)
	t_x2 = parse(t_x2)
	if typeof(t_x2) == Void
		t_x2 = 0
	end
	print("Please enter target y 2:")
	t_y2 = readline(STDIN)
	t_y2 = parse(t_y2)
	if typeof(t_y2) == Void
		t_y2 = 0
	end
	if t_x2 != 0 && t_y2!=0
		if board[t_x2,t_y2].player == now_play
			println("Error: You are killing your own player")
			println("Enter valid input")
			println(now_play)
			quit()
		end
	end
  end
  return [s_x,s_y,t_x,t_y,t_x2,t_y2]
end

################################################################################################################################################################################

now_play = (Length_Of_Tables(db,"moves")+1)%2 

meta_row = Access_By_key(db,"type")
if meta_row[2] == "standard"
  gamesize = 9
elseif meta_row[2] == "minishoghi"
  gamesize = 5
else
  gamesize = 12
end

board = Matrix{chess}(gamesize,gamesize)

for i in 1:gamesize
  for j in 1:gamesize
    board[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
  end
end

database_to_board(board)

x_in = Take_inputs(db,gamesize,now_play)
s_x=x_in[1] 
s_y=x_in[2] 
t_x=x_in[3] 
t_y=x_in[4] 
t_x2=x_in[5]
t_y2=x_in[6]
pro=0

println("now playing $now_play")

if gamesize == 5
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end	
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
    if now_play == 1 && t_x == 1
		query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x AND curr_y = $t_y"
		SQLite.execute!(SQLite.Stmt(db,query))
		pro = 1
	elseif now_play == 0 && t_x == 5
		query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x AND curr_y = $t_y"
		SQLite.execute!(SQLite.Stmt(db,query))
		pro=1
	else
		pro=0
	end
elseif gamesize == 9
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end	
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
	if now_play == 1 && (t_x == 1 || t_x == 2 || t_x == 3)
		query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x AND curr_y = $t_y"
		SQLite.execute!(SQLite.Stmt(db,query))
		pro = 1
	elseif now_play == 0 && (t_x == 9 || t_x == 8 || t_x == 7)
		query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x AND curr_y = $t_y"
		SQLite.execute!(SQLite.Stmt(db,query))
		pro=1
	else
		pro=0
	end
else
	if board[t_x,t_y].view != '.' && board[t_x,t_y].player !=now_play
		query="DELETE FROM Player WHERE curr_x= $t_x AND curr_y = $t_y;"
		stmt=SQLite.Stmt(db,query)
		SQLite.execute!(stmt)
		na = board[t_x,t_y].view
		updatePlayer(db,now_play,0, 0, 0, 0, na)
	end
	query = "UPDATE Player SET curr_x= $t_x, curr_y = $t_y WHERE curr_x = $s_x AND curr_y = $s_y"
	SQLite.execute!(SQLite.Stmt(db,query))
	if now_play == 1 && (t_x == 1 || t_x == 2 || t_x == 3)
		query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x AND curr_y = $t_y"
		SQLite.execute!(SQLite.Stmt(db,query))
		pro = 1
	elseif now_play == 0 && (t_x == 9 || t_x == 8 || t_x == 7)
		query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x AND curr_y = $t_y"
		SQLite.execute!(SQLite.Stmt(db,query))
		pro=1
	else
		pro=0
	end
	if t_x2 !=0 && t_y2!=0
		if board[t_x2,t_y2].view != '.' && board[t_x2,t_y2].player !=now_play
			query="DELETE FROM Player WHERE curr_x= $t_x2 AND curr_y = $t_y2;"
			stmt=SQLite.Stmt(db,query)
			SQLite.execute!(stmt)
			na = board[t_x2,t_y2].view
			updatePlayer(db,now_play,0, 0, 0, 0, na)
		end	
	
		query = "UPDATE Player SET curr_x= $t_x2, curr_y = $t_y2 WHERE curr_x = $t_x AND curr_y = $t_y"
		SQLite.execute!(SQLite.Stmt(db,query))
		if now_play == 1 && (t_x2 == 1 || t_x2 == 2 || t_x2 == 3)
			query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x2 AND curr_y = $t_y2"
			SQLite.execute!(SQLite.Stmt(db,query))
			pro = 1
		elseif now_play == 0 && (t_x2 == 9 || t_x2 == 8 || t_x2 == 7)
			query = "UPDATE Player SET promotion = 1 WHERE curr_x = $t_x2 AND curr_y = $t_y2"
			SQLite.execute!(SQLite.Stmt(db,query))
			pro=1
		else
			pro=0
		end
	end
end
#update moves table
if pro == 1
  updateMoves(db,Length_Of_Tables(db,"moves")+1,"move",s_x,s_y,t_x,t_y,"!",0,t_x2,t_y2)
else
  updateMoves(db,Length_Of_Tables(db,"moves")+1,"move",s_x,s_y,t_x,t_y,0,0,t_x2,t_y2)
end

board_new = Matrix{chess}(gamesize,gamesize)
for i in 1:gamesize
  for j in 1:gamesize
    board_new[i,j] = chess(i,j,0,0,'.',-1) # Setup a null chess for every cell on the board
  end
end

database_to_board(board_new)
Display_Board_test(gamesize,board_new)
end #module end
