import pygame
import gfx, gui, playgame, ticker

images = []


class SimpleDialog:
    def __init__(self, title, logo, description, buttons):
    	width, height = size = 280, 160
	self.backc = 30, 30, 50
	self.forec = 200, 180, 30
    	colorkey = 255, 0, 255
	
	self.image = pygame.Surface((width, height))
	self.image.fill(self.backc)
	self.rect = self.image.get_rect()
	gui.fill_corners(self.image, colorkey, self.rect)
	r = self.rect.inflate(-10, -10)
	r.height = 50
	gui.draw_titlebar(self.image, self.forec, self.backc, r, title, logo)

    	r = pygame.Rect(10, 40, 210, 100)
	gui.render_textrect(self.image, description, gui.tiptitle_font, 
	    	    r, (200,200,200), self.backc, 0)
		    	
	self.rect.center = gfx.res.center
	self.image.set_colorkey(colorkey)
	self.image = gfx.optimize(self.image)
	playgame.thegame.popup = self

    	bwidth = 0
    	for b in buttons:
	    bwidth += b.rect.width + 30
	posx = self.rect.centerx - bwidth/2
	posy = self.rect.bottom - 10
    	self.ui_all = []
	for b in buttons:
	    self.ui_all.append(b)
	    b.rect.bottomleft = posx, posy
	    posx += b.rect.width + 30
    	

    def draw(self, screen):
    	screen.blit(self.image, self.rect)
    	for b in self.ui_all:
	    b.draw(screen) 
    def update(self, time):
    	for b in self.ui_all:
	    b.disabled = playgame.thegame.village.gold < b.goldneeded

    def mousemoved(self, pos):
    	for ui in self.ui_all:
	    ui.mousemoved(pos)
    
    def mousepushed(self, pos):
	for ui in self.ui_all:
	    ui.mousepushed(pos)
	return self.rect.collidepoint(pos)
    
    def mousereleased(self, pos):
    	any = 0
	for ui in self.ui_all:
	   any = ui.mousereleased(pos) or any
	if any:
	    self.close()
	    return 0
	return self.rect.collidepoint(pos)
    	
    def close(self):
    	playgame.thegame.popup = None


