# ##### BEGIN GPL LICENSE BLOCK #####
#
#  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 2
#  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, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8-80 compliant>

bl_info = {
    "name": "JSON Mesh Export",
    "author": "Nathan Faucett, Lasse Bodilsen",
    "version": (0, 1, 0),
    "blender": (2, 90, 0),
    "location": "File > Import-Export",
    "description": "Export JSON data format",
    "doc_url": "https://github.com/LaBodilsen/Blender-JSON-Exporter",
    "support": 'OFFICIAL',
    "category": "Import-Export",
}

if "bpy" in locals():
    import importlib
    if "export_json" in locals():
        importlib.reload(export_json)

import bpy

from bpy_extras.io_utils import ExportHelper#, ImportHelper

from bpy.props import (
    CollectionProperty,
    StringProperty,
    BoolProperty,
    FloatProperty,
)
from bpy_extras.io_utils import (
    ImportHelper,
    ExportHelper,
    axis_conversion,
    orientation_helper,
)

# ################################################################
# Export JSON
# ################################################################

@orientation_helper(axis_forward='Y', axis_up='Z')
class ExportJSON( bpy.types.Operator, ExportHelper ):
    bl_idname = "export_mesh.json"
    bl_label = "Export JSON"
    bl_description = "Export as custom json"

    filename_ext = ".json"
    filter_glob: StringProperty(default="*.json", options={'HIDDEN'})

    use_selection: BoolProperty(
        name="Selection Only",
        description="Export selected objects only",
        default=False,
    )

    use_mesh_modifiers: BoolProperty(
        name="Apply Modifiers",
        description="Apply Modifiers to the exported mesh",
        default=False,
    )

    global_scale: FloatProperty(
        name="Scale",
        min=1,
        max=10000,
        default=1000,
    )

   
    # def invoke( self, context, event ):
        # return ExportHelper.invoke( self, context, event )
    
    # @classmethod
    # def poll( cls, context ):
        # return context.active_object != None
    
    def execute( self, context):
        print("Selected: " + context.active_object.name )
        from mathutils import Matrix
        from . import export_json        

        if not self.properties.filepath:
            raise Exception("filename not set")

        keywords = self.as_keywords(
            ignore=(
                "axis_forward",
                "axis_up",
#                "global_scale",
                "check_existing",
                "filter_glob",
            )
        )

        global_matrix = axis_conversion(
            to_forward=self.axis_forward,
            to_up=self.axis_up,
        ).to_4x4() @ Matrix.Scale(self.global_scale, 4)
        keywords["global_matrix"] = global_matrix

        filepath = self.filepath
        export_json.save( self, context, **keywords )

        return {'FINISHED'}
#        return io_mesh_json.export_json.save( self, context, **self.properties )

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        sfile = context.space_data
        operator = sfile.active_operator

class JSON_PT_export_include(bpy.types.Panel):
    bl_space_type = 'FILE_BROWSER'
    bl_region_type = 'TOOL_PROPS'
    bl_label = "Include"
    bl_parent_id = "FILE_PT_operator"

    @classmethod
    def poll(cls, context):
        sfile = context.space_data
        operator = sfile.active_operator

        return operator.bl_idname == "EXPORT_MESH_OT_json"

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        sfile = context.space_data
        operator = sfile.active_operator

        layout.prop(operator, "use_selection")


class JSON_PT_export_transform(bpy.types.Panel):
    bl_space_type = 'FILE_BROWSER'
    bl_region_type = 'TOOL_PROPS'
    bl_label = "Transform"
    bl_parent_id = "FILE_PT_operator"

    @classmethod
    def poll(cls, context):
        sfile = context.space_data
        operator = sfile.active_operator

        return operator.bl_idname == "EXPORT_MESH_OT_json"

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        sfile = context.space_data
        operator = sfile.active_operator

        layout.prop(operator, "axis_forward")
        layout.prop(operator, "axis_up")
        layout.prop(operator, "global_scale")


class JSON_PT_export_geometry(bpy.types.Panel):
    bl_space_type = 'FILE_BROWSER'
    bl_region_type = 'TOOL_PROPS'
    bl_label = "Geometry"
    bl_parent_id = "FILE_PT_operator"

    @classmethod
    def poll(cls, context):
        sfile = context.space_data
        operator = sfile.active_operator

        return operator.bl_idname == "EXPORT_MESH_OT_json"

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        sfile = context.space_data
        operator = sfile.active_operator

        layout.prop(operator, "use_mesh_modifiers")

# ################################################################
# Common
# ################################################################

def menu_func_export(self, context):
    self.layout.operator(ExportJSON.bl_idname, text="Custom JSON-Exporter(.json)")

#    default_path = bpy.data.filepath.replace(".blend", ".json")
#    self.layout.operator( ExportJSON.bl_idname, text="JSON (.json)").filepath = default_path


classes = (
    ExportJSON,
    JSON_PT_export_include,
    JSON_PT_export_transform,
    JSON_PT_export_geometry,
)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.TOPBAR_MT_file_export.append(menu_func_export)

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)

if __name__ == "__main__":
    register()