class Editor(State):
    def __init__(self):
        State.__init__(self)
        self.buttons = {}

        #tileset area
        self.tileset_area = StaticWidget((1020, 280))
        self.tileset_area.set_pos((2, 485))
        self.tileset_area.pos = (2, 485)
        ca = ClickArea(pygame.Rect([2, 485, 1024, 280]), self.choose_tiletype)
        self.widgets.append(ca)
        self.tileset_id = "ts_tileset"
        self.marked_type = None
        self.load_tileset()

        #set clan
        buttons = [
            "wood", "day", "fire", "life", "earth", "night", "metal", "water",
            "death"
        ]

        x, y = 900, 50
        for clan in buttons:
            btn = ImgButton(GLOBALS["signets"][clan][1], \
                self.set_clan, clan=clan)
            btn.set_pos((x, y))
            self.widgets.append(btn)
            self.buttons[clan] = btn
            btn.draw(self.background)
            y = y + btn.get_size()[1] + 5
        self.clan = "fire"

        #Camera
        self.camera = Camera(None)

        #load empty map
        self.filename = ""
        self.load_map(new=True)

        #map area
        self.map_area = StaticWidget((640, 480))
        self.map_area.set_pos((0, 0))
        self.map_area_rect = pygame.Rect([0, 0, 640, 480])
        self.widgets.append(ca)

        #grid overlay for map area
        w, h = self.map_area.get_size()
        self.grid = StaticWidget((w, h))
        self.grid.fill((255, 0, 255))
        self.grid.set_colorkey((255, 0, 255))
        self.grid.set_alpha(100)
        for row in xrange(h / tsize):
            for col in xrange(w / tsize):
                pygame.draw.line(self.grid, (255, 255, 255), (col * 32, 0),
                                 (col * 32, h))
                pygame.draw.line(self.grid, (255, 255, 255), (0, row * 32),
                                 (w, row * 32))
        self.grid.set_pos(self.map_area.get_pos())

        #buttons
        buttons = [(" NEW ", self.new_map), (" LOAD ", self.open_file),
                   (" SAVE AS ", self.save_file), (" SAVE ", self.save_map)]

        x, y = 650, 1
        for b in buttons:
            name, callback = b
            btn = ImgButton(TextLine(name), callback)
            btn.set_pos((x, y))
            btn.draw_border((255, 255, 255))
            self.widgets.append(btn)
            btn.draw(self.background)
            x = x + btn.get_size()[0] + 4

        #add objects
        buttons = [
            (" delete obj", "delete"),
            (" add start ", "start"),
            (" add exit ", "exit"),
            (" add enemy ", "enemy"),
            (" add dialog-trigger ", "dtrigger"),
        ]

        x, y = 650, 50
        for b in buttons:
            name, cmd = b
            btn = ImgButton(TextLine(name, fontsize=15), \
                self.select_add_object, cmd=cmd)
            btn.set_pos((x, y))
            self.widgets.append(btn)
            btn.draw(self.background)
            y = y + btn.get_size()[1] + 5
            self.buttons[cmd] = btn

        #TEST button :)
        btn = ImgButton(TextLine(" TEST ", fontsize=25), self.test)
        btn.set_pos((900, 420))
        btn.draw_border((255, 255, 255))
        self.widgets.append(btn)
        btn.draw(self.background)

        #keyboard events
        pygame.key.set_repeat(10, 20)

        #scroll map
        self.add_kbevent(KEYDOWN, K_UP, self.scroll_map, vy=-1)
        self.add_kbevent(KEYDOWN, K_DOWN, self.scroll_map, vy=1)
        self.add_kbevent(KEYDOWN, K_LEFT, self.scroll_map, vx=-1)
        self.add_kbevent(KEYDOWN, K_RIGHT, self.scroll_map, vx=1)

    def update_screen(self):
        #draw map
        self.lvl_layer.set_colorkey(None)
        self.lvl_layer.fill((255, 0, 255))
        self.lvl_layer.blit(self.lvl_img, (0, 0))
        self.lvl_layer.set_colorkey((255, 0, 255))
        self.map_area.fill((0, 0, 0))
        self.map_area.blit(self.lvl_layer, (0, 0),
                           area=[self.camera.pos, GLOBALS["screen_size"]])
        self.map_area.draw_border((255, 255, 255))

        #draw map area
        self.map_area.draw(self.background)
        self.grid.draw(self.background)

        State.update_screen(self)

    def test(self):
        self.save_map()
        if self.filename:
            self.pause()
            self.next = Level(self.filename, testing=True)
            self.quit()

    def set_clan(self, clan):
        #unmark previous selection
        self.buttons[self.clan].draw_border((0, 0, 0))
        self.buttons[self.clan].draw(self.background)
        #mark current selection
        self.clan = clan
        self.buttons[self.clan].draw_border((255, 255, 255))
        self.map["options"]["clan"] = self.clan
        self.buttons[self.clan].draw(self.background)

    def set_option(self, param):
        old_value = ""
        if self.map["options"].has_key(param):
            old_value = self.map["options"][param]

        entry = tkSimpleDialog.askstring(title="Set " + param,
                                         prompt="Enter " + param + ":",
                                         initialvalue=old_value)
        print param, ":", entry
        if entry is not None:
            self.map["options"][param] = entry

    def select_add_object(self, cmd):
        #dehighlight old button
        if self.buttons.has_key(self.marked_type):
            self.buttons[self.marked_type].draw_border((0, 0, 0))
            self.buttons[self.marked_type].draw(self.background)
        elif self.marked_type:
            self.mark_tile(None)  #deselect tile

        #highlight button
        self.buttons[cmd].draw_border((255, 255, 255))
        self.buttons[cmd].draw(self.background)
        self.marked_type = cmd

    def load_map(self, filename="", new=False):
        if not new:
            try:
                new_map = Map(filename)
            except:
                print "Could not load map"
                return
        else:
            new_map = Map()

        self.filename = filename
        pygame.display.set_caption(filename)
        self.map = new_map
        self.lvl_map = self.map["tilemap"]
        self.lvl_img, blocks = self.map.draw(editing=True)
        self.lvl_layer = pygame.Surface(self.lvl_img.get_size())
        self.camera.set_pos((0, tsize * 20))
        self.load_tileset(self.map["tset"])
        self.set_clan(self.map["options"]["clan"])

    def scroll_map(self, vx=0, vy=0):
        x, y = self.camera.pos
        self.camera.set_pos((x + vx * tsize, y + vy * tsize))

    def open_file(self):
        filename = tkFileDialog.askopenfilename(title="Open",
                                                initialfile=map_path)
        if filename:
            self.load_map(filename)

    def save_file(self):
        self.asksaveasfilename()
        if self.filename:
            self.save_map()

    def asksaveasfilename(self):
        filename = tkFileDialog.asksaveasfilename(title="Save as",
                                                  initialfile=map_path)
        if filename:
            self.filename = filename

    def save_map(self):
        if not self.filename:
            self.asksaveasfilename()
            if self.filename:
                self.map.save_map(path=self.filename)
        else:
            self.map.save_map(path=self.filename)

    def new_map(self):
        ok = tkMessageBox.askyesno(title="Just asking..",
                                   message="Really create a new map?")
        if ok:
            self.load_map(new=True)
            self.load_tileset()

    def choose_tiletype(self):
        x, y = pygame.mouse.get_pos()
        off_x, off_y = self.tileset_area.pos
        x, y = (x - off_x) / (tsize + 2), (y - off_y) / (tsize + 2)
        if GLOBALS[self.tileset_id].has_key((x, y)):
            self.mark_tile((x, y))

    def mark_tile(self, coord):
        def draw_tile_border(coord, color=(255, 255, 255)):
            x, y = coord[0] * (tsize + 2), coord[1] * (tsize + 2)
            r = pygame.Rect([x - 1, y - 1, tsize + 2, tsize + 2])
            pygame.draw.rect(self.tileset_area, color, r, 1)

        if self.marked_type:
            if GLOBALS[self.tileset_id].has_key(self.marked_type):
                draw_tile_border(self.marked_type, (0, 0, 0))
            elif self.buttons.has_key(self.marked_type):
                self.buttons[self.marked_type].draw_border((0, 0, 0))
                self.buttons[self.marked_type].draw(self.background)

        if coord:
            draw_tile_border(coord)
        self.marked_type = coord
        self.tileset_area.draw(self.background)

    def load_tileset(self, tset="tileset"):
        ts_id = "ts_" + tset
        if not GLOBALS.has_key(ts_id):
            ts_id = "ts_tileset"
        self.marked_type = None
        self.tileset_area.fill((0, 0, 0))
        self.tileset_id = ts_id
        tiles = GLOBALS[ts_id]
        keys = tiles.keys()
        keys.sort()
        for k in keys:
            x, y = k[0] * (tsize + 2), k[1] * (tsize + 2)
            self.tileset_area.blit(tiles[k], (x, y))
        self.tileset_area.draw(self.background)

    def check_collisions(self):
        #check if map was clicked -> put tiles on map
        mpos = pygame.mouse.get_pos()
        pressed = pygame.mouse.get_pressed()
        lbutton, rbutton = pressed[0], pressed[2]

        if not self.map_area_rect.collidepoint(mpos) or \
            (not rbutton and not (lbutton and self.marked_type)):
            return

        #calculate map coordinates
        off_x, off_y = self.camera.pos
        x, y = (mpos[0] + off_x) / tsize, (mpos[1] + off_y) / tsize

        #set tile info
        if rbutton and self.lvl_map.has_key((x, y)):
            self.lvl_map.pop((x, y))
        elif lbutton and GLOBALS[self.tileset_id].has_key(self.marked_type):
            #place tile
            self.lvl_map[x, y] = self.marked_type
        elif lbutton and self.buttons.has_key(self.marked_type):
            #place object
            if self.marked_type == "delete" and self.map["objects"].has_key(
                (x, y)):
                self.map["objects"].pop((x, y))
            else:
                params = None
                t = self.marked_type
                if t == "dtrigger":
                    params = tkSimpleDialog.askstring(
                        title="Set dialog id",
                        prompt="Enter dialog ID:",
                    )
                    if params is None:  #didn't want to add that trigger anyway
                        return
                    if not dialog.has_key(params):
                        print "Warning: Dialog id not found... just for your information. ^^"
                self.map["objects"][x, y] = (self.marked_type, params)

                if params:
                    self.mark_tile(None)

        self.lvl_img, blocks = self.map.draw(editing=True)
        self.lvl_layer = pygame.Surface(self.lvl_img.get_size())
class LevelSelection(State):
    def __init__(self, new=True):
        State.__init__(self)
        self.success = False
        self.new = new  #start tutorial before showing level selection!

        self.signet_pos = {
            "wood": (83, 181),
            "day": (158, 75),
            "fire": (284, 34),
            "life": (410, 75),
            "earth": (485, 181),
            "night": (456, 307),
            "metal": (348, 368),
            "water": (220, 368),
            "death": (112, 307)
        }

        self.light_pos = {
            "wood": (239, 225),
            "day": (261, 173),
            "fire": (315, 155),
            "life": (369, 173),
            "earth": (391, 225),
            "night": (383, 271),
            "metal": (345, 307),
            "water": (285, 307),
            "death": (247, 271)
        }

        self.cur_blinking = None
        self.cur_blending = None
        self.cur_clan = None
        self.blink_timer = Timer(50)
        self.blink_count = 0
        self.blink_max = 10
        self.light = GLOBALS["ui_stuff"]["green_light"]

        r = pygame.Rect([281, 203, 78, 77])
        r.inflate(-10, -10)
        bigredbutton = ClickArea(r, self.bigred_pressed)
        self.widgets.append(bigredbutton)
        self.donotpress = pygame.Rect([267, 188, 106, 95])
        self.signets = GLOBALS["signets"]
        self.bg_image = GLOBALS["backgrounds"]["gameboard.png"].copy()
        self.ui_layer = pygame.Surface(GLOBALS["screen_size"])
        self.ui_layer.fill((255, 0, 255))
        self.ui_layer.set_colorkey((255, 0, 255))

        self.tooltip_layer = pygame.Surface(GLOBALS["screen_size"])
        self.tooltip_layer.fill((255, 0, 255))
        self.tooltip_layer.set_colorkey((255, 0, 255))
        self.tooltip = StaticWidget((80, 20))
        self.dnp_tooltip = GLOBALS["ui_stuff"]["donotpress"]

        #level selection buttons
        self.signet_buttons = []
        levels = GLOBALS["clan_levels"]
        for k in self.signet_pos.keys():
            if self.signets.has_key(k) and levels.has_key(k):
                btn = ImgButton(self.signets[k][0], self.start_level, \
                    clan=k, level=levels[k])
                btn.set_pos(self.signet_pos[k])
                btn.rect = pygame.Rect([self.signet_pos[k], btn.get_size()])
                btn.rect.inflate(-20, -20)
                btn.clan = k
                self.widgets.append(btn)
                self.signet_buttons.append(btn)

        #show savegame progress
        self.show_signet(player_data["unlocked"])

        #keyboard events
        self.add_kbevent(KEYDOWN, K_ESCAPE, self.quit)

        self.background.blit(self.bg_image, (0, 0))
        self.background.blit(self.tooltip_layer, (0, 0))
        self.background.blit(self.ui_layer, (0, 0))
        self.tooltip_layer.fill((255, 0, 255))

    def main_start(self):
        if pygame.mixer.music.get_busy():
            pygame.mixer.music.fadeout(300)

        pygame.mouse.set_visible(1)
        if self.new is True:
            self.new = "somewhere in between"
            self.cur_clan = None
            self.next = Level("tutorial.map", prev=self)
            self.pause()
            self.quit()
        elif self.new == "somewhere in between":
            self.new = False
            self.update_screen()
            self.next = ShowDialog("donotpress", self.screen)
            self.pause()
            self.quit()

    def start_level(self, clan, level):
        if clan in player_data[
                "unlocked"] or self.cur_blending or self.cur_blinking:
            #do not start finished maps and wait while blending new signet in
            return
        self.success = False
        self.cur_clan = clan
        self.next = Level(level, prev=self)
        self.pause()
        self.quit()

    def unpause(self):
        if self.success:
            if self.cur_clan:
                self.show_signet([self.cur_clan])
                player_data["unlocked"].append(self.cur_clan)  #track progress
            self.cur_clan = None
            self.autosave()  #save progress
        State.unpause(self)

    def autosave(self):
        try:
            save_object(player_data, "save.dat")
        except:
            print "Autosave failed. Uh.. guess you need to keep playing?"

    def show_signet(self, clans):
        #show recently gained signet(s)
        self.cur_blending = []
        self.cur_blinking = []
        for clan in clans:
            #signet
            signet = self.signets[clan][0]
            blend = StaticWidget(signet.get_size())
            blend.fill((255, 0, 255))
            blend.set_colorkey((255, 0, 255))
            blend.blit(signet, (0, 0))
            blend.set_pos(self.signet_pos[clan])
            blend.set_alpha(0)
            self.cur_blending.append(blend)

            #green light
            img = self.light
            blink = StaticWidget(img.get_size())
            blink.fill((255, 0, 255))
            blink.set_colorkey((255, 0, 255))
            blink.blit(img, (0, 0))
            blink.set_pos(self.light_pos[clan])
            self.cur_blinking.append(blink)
            self.blink_timer.set_interval(3)

    def update_other(self):
        if self.cur_blending:
            #fancy alpha blending effect for new signet
            alpha = self.cur_blending[0].get_alpha() + 5

            if alpha > 100:  #stop blending
                for blend in self.cur_blending:
                    blend.set_alpha(None)
                    blend.draw(self.bg_image)
                self.cur_blending = None
            else:  #keep blending
                for blend in self.cur_blending:
                    blend.set_alpha(alpha)
                    blend.draw(self.bg_image)

        elif self.cur_blinking:
            if not self.blink_timer.update():
                return

            #fancy blinking light o/
            self.blink_count += 1
            b = self.blink_count

            #simulate irregular flickering
            if b == 1:
                self.blink_timer.set_interval(2)
            elif b == 3:
                self.blink_timer.set_interval(3)
            elif b == 6:
                self.blink_timer.set_interval(1)

            #to draw or not to draw...
            if self.blink_count % 2:
                for blink in self.cur_blinking:
                    blink.draw(self.ui_layer)
            else:
                self.ui_layer.fill((255, 0, 255))

            #ok, stop flickering.
            if self.blink_count > self.blink_max:
                for blink in self.cur_blinking:
                    blink.draw(self.bg_image)
                self.ui_layer.fill((255, 0, 255))
                self.blink_count = 0
                self.cur_blinking = None
        else:
            self.check_mouseover()

    def check_mouseover(self):
        #check if mouse hovers signet and show tooltip
        mpos = pygame.mouse.get_pos()
        for b in self.signet_buttons:
            if b.rect.collidepoint(mpos):
                self.tooltip.fill((255, 255, 255))
                self.tooltip.draw_border((0, 0, 0))
                self.tooltip.blit(GLOBALS["kanji"]["clan"], (10, 3))
                self.tooltip.blit(GLOBALS["kanji"][b.clan], (60, 4))
                pos = (mpos[0] + 10, mpos[1] + 20)
                x = (b.rect.centerx - self.tooltip.get_size()[0] / 2)
                y = b.rect.top
                pos = (x, y)
                self.tooltip_layer.blit(self.tooltip, pos)
                break

        dnp = self.donotpress
        if dnp.collidepoint(mpos):
            pos = (dnp.left - 60, dnp.top - 22)
            self.tooltip_layer.blit(self.dnp_tooltip, pos)

    def bigred_pressed(self):
        if self.cur_blending or self.cur_blinking:
            return

        if len(player_data["unlocked"]) < 9:
            self.next = ShowDialog("nothinghappened", self.screen)
            self.pause()
            self.quit()
        else:
            self.next = Outro()
            self.quit()

    def update_screen(self):
        self.background.blit(self.bg_image, (0, 0))
        self.background.blit(self.tooltip_layer, (0, 0))
        self.background.blit(self.ui_layer, (0, 0))
        self.tooltip_layer.fill((255, 0, 255))
        State.update_screen(self)