# Flexlay - A Generic 2D Game Editor
# Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from flexlay import ObjectLayer, EditorMap, ToolContext
from flexlay.util import get_value_from_tree, SExprWriter

from .util import load_lisp
from .tilemap import SuperTuxTileMap
from .worldmap_object import create_worldmapobject_from_data


class WorldMap:

    def __init__(self, arg1, arg2=None):
        self.name = "No Name"
        self.music = ""
        self.intro_filename = ""
        self.start_pos_x = 0
        self.start_pos_y = 0
        self.filename = None
        self.width = 0
        self.height = 0
        self.objects = ObjectLayer()
        self.editormap = None

        if arg2 is not None:
            # New Level
            (width, height) = arg1, arg2

            self.width = width
            self.height = height
            self.tilemap = SuperTuxTileMap.from_size(self.width, self.height)
        else:
            # Load Level from file
            self.filename = arg1

            tree = load_lisp(self.filename, "supertux-worldmap")

            data = tree[1:]
            self.parse(data)

    def parse(self, data):
        for i in data:
            name, data = i[0], i[1:]

            if name == 'properties':
                self.name = get_value_from_tree(["name", "_"], data, "No Name")
                print("Name:", self.name)
                self.music = get_value_from_tree(["music", "_"], data, "salcon.ogg")
                self.intro_filename = get_value_from_tree(["intro-filename", "_"], data, "")
                self.start_pos_x = get_value_from_tree(["start_pos_x", "_"], data, 0)
                self.start_pos_y = get_value_from_tree(["start_pos_y", "_"], data, 0)
            elif name == 'tilemap':
                self.tilemap = SuperTuxTileMap.from_sexpr(data)
            else:
                create_worldmapobject_from_data(self.objects, name, data)

    def save(self, filename):
        with open(filename, "w") as f:
            writer = SExprWriter(f)

            writer.write_comment("Generated by Flexlay Editor")
            writer.begin_list("supertux-worldmap")

            writer.begin_list("properties")
            writer.write_string("name", self.name, True)
            if self.intro_filename != "":
                writer.write_string("intro-filename", self.intro_filename)
            writer.write_string("music", self.music)
            writer.write_int("start_pos_x", self.start_pos_x)
            writer.write_int("start_pos_y", self.start_pos_y)
            writer.end_list("properties")

            self.tilemap.write(writer)

            for o in self.objects.get_objects():
                o.get_data().write(writer)
            writer.end_list("supertux-worldmap")

    def activate(self, workspace):
        # FIXME: All wrong and obsolote
        self.editormap = EditorMap()
        self.editormap.add_layer(self.tilemap.tilemap_layer)
        self.editormap.add_layer(self.objects)
        self.editormap.metadata = self

        workspace.set_map(self.editormap)
        ToolContext.current.tilemap_layer = self.tilemap.tilemap_layer
        ToolContext.current.object_layer = self.objects
        from .gui import SuperTuxGUI
        self.editormap.sig_change.connect(SuperTuxGUI.current.on_map_change)


# EOF #
