import tkinter as tk
import sys

from tkinter import messagebox
import time as Time

from config import Config
from page01 import Page01
from game_stat import Game_Stat
from board import Board
from ship import Ship 
from player import Player 
from page02 import Page02


class Window(tk.Tk):

	def __init__(self, Game):
		self.game = Game # battleship
		self.config = Game.config

		super().__init__()
		self.title(self.config.title)
		self.geometry(self.config.screen)

		self.create_container()

		self.pages = {}
		self.create_page02()
		self.create_board()
		self.create_page01()
		
	def change_page(self, page) :
		page = self.pages[page]
		page.tkraise()

	def auth_login(self):
		username = self.pages['page01'].var_username.get()

		print(username)

		if username:
			self.change_page("board")
		else:
			info = messagebox.showinfo("Username Confirmation", "Please input USERNAME.")

	def create_container(self):
		self.container = tk.Frame(self, bg="white")
		self.container.pack(fill="both", expand=True)

	def create_page02(self) :
		self.pages["page02"] = Page02(self.container, self.game)
	
	def create_board(self):
		self.pages["board"] = Board(self.container, self.game)

	def create_page01(self) :
		self.pages["page01"] = Page01(self.container, self.game)

class Battleship:

	def __init__(self):
		self.config = Config()
		self.game_stat = Game_Stat()
		self.ship = Ship(self)
		self.player = Player()
		self.window = Window(self)

	def check_location(self) :
		ship = self.ship.location 
		player = self.player.location
		if ship == player :
			return True
		return False

	def window_button_clicked(self, pos_x, pos_y):
		#print(pos_x, pos_y)
		self.player.current_location(pos_x, pos_y)
		win = self.check_location()
		self.window.pages['board'].change_image_btn(pos_x, pos_y, win)

		if self.window.pages['board'].change_image_btn(pos_x, pos_y, win):
			self.game_stat.steps += 1

		if win :
			Time.sleep(1)
			self.window.change_page("page02")

	def run(self):
		self.window.mainloop()

	def main_menu_game(self):
		self.game_stat.level == 0
		self.config.row == 0
		self.config.column == 0

		self.ship.setup_location()
		self.window.create_board()

		self.window.change_page("page01")

	def next_level_game(self):
		self.game_stat.level += 1
		self.config.row += 1 
		self.config.column += 1

		self.ship.setup_location()
		self.window.create_board()
		self.window.change_page("board")

	def preview_game(self) :
		self.window.change_page("board")

	def replay_game(self) :
		self.ship.setup_location()
		self.window.create_board()
		self.window.change_page("board")

	def close_game(self) :
		sys.exit()

if __name__ == '__main__':
	my_battleship = Battleship()
	my_battleship.run()