import pygame
from pygame.locals import *

from locals import *

import os

import util

class Gui:
      selectarrow = pygame.image.load(os.path.join("ui", "selected.png"))
      selecthp = pygame.image.load(os.path.join("ui", "selected_healthbar.png"))

      def __init__(self, screen):
          Gui.selectarrow = Gui.selectarrow.convert_alpha()
          Gui.selecthp = Gui.selecthp.convert_alpha()
          self.bg = pygame.image.load(os.path.join("ui", "game_lower_bg.png")).convert_alpha()
          self.rect = self.bg.get_rect()
          self.charedge = pygame.image.load(os.path.join("ui", "charedge.png")).convert_alpha()
          self.charedge_hilight = pygame.image.load(os.path.join("ui", "charedge_hilight.png")).convert_alpha()
          self.charedge_rect = self.charedge.get_rect()
          self.health = pygame.image.load(os.path.join("ui", "health.png")).convert_alpha()
          self.healthbg = pygame.image.load(os.path.join("ui", "health_empty.png")).convert_alpha()
          self.health_rect = self.health.get_rect()

          self.weapon_disabled = pygame.image.load(os.path.join("ui", "game_lower_bg.png")).convert_alpha()
          self.weapon_rect = None
          self.ammocount = util.smallfont.render("0", True, COLOR1)

          self.panicbutton_down = pygame.image.load(os.path.join("ui", "panicbutton_down.png")).convert_alpha()
          self.panicbutton_rect = self.panicbutton_down.get_rect()
          self.panicbutton_rect.right = screen.get_width() - 19
          self.panicbutton_rect.bottom = screen.get_height() - 33
          self.panicbutton_state = 0

      def draw(self, screen, characters):
          self.rect.bottom = screen.get_height()
          screen.blit(self.bg, self.rect)

          i = 0

          #Rendering the character portraits
          self.charedge_rect.top = self.rect.top + 10
          for char in characters:
                self.charedge_rect.left = self.rect.left + 10 + i * (self.charedge_rect.width + 38)

                portrait_rect = char.portrait.get_rect()
                portrait_rect.top = self.charedge_rect.top + 2
                portrait_rect.left = self.charedge_rect.left + 2

                if char.selected:
                   screen.blit(self.charedge_hilight, self.charedge_rect)
                   screen.blit(char.portrait_hilight, portrait_rect)
                else:
                   screen.blit(self.charedge, self.charedge_rect)
                   screen.blit(char.portrait, portrait_rect)

                j = 0
                self.health_rect.left = self.charedge_rect.right + 2
                while j < MAX_HEALTH:
                      self.health_rect.bottom = self.charedge_rect.bottom - 1 - j * 6
                      if char.health > j:
                         screen.blit(self.health, self.health_rect)
                      else:
                         screen.blit(self.healthbg, self.health_rect)
                      j += 1

                self.weapon_rect = char.iteminuse.ui_rect
                self.weapon_rect.bottom = self.charedge_rect.bottom - 1
                self.weapon_rect.left = self.charedge_rect.right + 10
                if char.iteminuse:
                   screen.blit(char.iteminuse.ui_image, self.weapon_rect)
                   if char.iteminuse.ammo:
                      ammo = char.getammo(char.iteminuse.ammo)
                      self.ammocount = util.smallfont.render(str(ammo), True, COLOR2)
                      rect = self.ammocount.get_rect()
                      rect.bottom = self.weapon_rect.top - 1
                      rect.left = self.weapon_rect.left + 1
                      screen.blit(self.ammocount, rect)
                      if self.ammocount == 0:
                         screen.blit(self.weapon_disabled, self.weapon_rect)


                i += 1
          if self.panicbutton_state:
                screen.blit(self.panicbutton_down, self.panicbutton_rect)
                self.panicbutton_state -= 1
                panicbutton_sound = pygame.mixer.Sound("sounds/panic.wav")
                panicbutton_sound.set_volume(0.3)
                panicbutton_sound.play()

      def click(self, pos, characters):
          i = 0
          self.charedge_rect.top = self.rect.top + 10
          for char in characters:
                self.charedge_rect.left = self.rect.left + 10 + i * (self.charedge_rect.width + 38)
                if self.charedge_rect.collidepoint(pos):
                   return char
                self.weapon_rect.bottom = self.charedge_rect.bottom - 1
                self.weapon_rect.left = self.charedge_rect.right + 10
                if self.weapon_rect.collidepoint(pos):
                   char.switch_item()
                i += 1
          if self.panicbutton_rect.collidepoint(pos):
                self.panicbutton_state = 2
          return None
