import pygame as pg 
import sys 
from os import path
from settings import *
from sprites import *
from tilemap import *

class Game:
  def __init__(self):
    pg.init()
    self.screen = pg.display.set_mode((WIDTH, HEIGHT))
    pg.display.set_caption(TITLE)
    self.clock = pg.time.Clock()
    self.load_data()
  def load_data(self):
    game_folder = path.dirname(__file__)
    sprites_folder = path.join(game_folder, 'Sprites')
    self.map = Map(path.join(game_folder, 'map2.txt'))
    self.player_spr = pg.image.load(path.join(sprites_folder, PLAYER_SPR)).convert_alpha()
    self.wall_spr = pg.image.load(path.join(sprites_folder, WALL_IMAGE)).convert_alpha()
    self.wall_spr = pg.transform.scale(self.wall_spr, (TILESIZE, TILESIZE))

    
  def sprite(self):
    #initialize all variables 
    self.all_sprites = pg.sprite.Group()
    self.walls = pg.sprite.Group()
    #self.player = Player(self, 10, 10)

    for row, tiles in enumerate(self.map.data):
      for col,tile in enumerate(tiles):
        if tile == '1':
          Wall(self,col,row)
        if tile == 'P':
          self.player = Player(self, col, row)
    self.camera = Camera(self.map.width, self.map.height) #Camera Object

  def run(self):
    #Run Game Loop, set self.playing = False to end game
    self.playing = True 
    while self.playing:
      self.dt = self.clock.tick(FPS)/1000
      self.controls()
      self.update()
      self.draw()

  def quit(self):
    pg.quit()
    sys.exit()



  def update(self):
    #update sprites pos 
    self.all_sprites.update()
    self.camera.update(self.player)

  def draw_grid(self):
    for x in range(0, WIDTH, TILESIZE):
      pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
    for y in range(0, HEIGHT, TILESIZE):
      pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))

  def draw(self):
    pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
    self.screen.fill(BGCOLOR)
    self.draw_grid()

    for sprite in self.all_sprites:
      self.screen.blit(sprite.image, self.camera.apply(sprite))
    pg.display.flip()
  def controls(self):
    #Player controls and actions
    for event in pg.event.get():
      if event.type == pg.QUIT:
        self.quit()
      if event.type == pg.KEYDOWN:
        if event.type == pg.K_ESCAPE:
          self.quit()
          

  def show_start_screen(self):
    pass

  def show_gameover_screen(self):
    pass
  #Creat game objects

g = Game()
g.show_start_screen()
while True:
  g.sprite()
  g.run()
  g.show_gameover_screen







