#  ***** 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 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/>
#  and write to the Free Software Foundation, Inc., 51 Franklin Street,
#  Fifth Floor, Boston, MA  02110-1301, USA..
#
#  The Original Code is Copyright (C) 2023 by Kurochkin Ilia  ###
#  All rights reserved.
#
#  Contact:      brothermechanic@yandex.com    ###
#  Information:  https://gitlab.com/robossembler   ###
#
#  The Original Code is: all of this file.
#
#  ***** END GPL LICENSE BLOCK *****
#
# -*- coding: utf-8 -*-

import os
import bpy

from bpy.types import (
    Panel,
    Operator,
    PropertyGroup)

from bpy.props import (
    StringProperty,
    EnumProperty,
    PointerProperty)

from .io_scene_json import export_json
from .io_anim_ros2bag import set_animation_data
from .io_entity_manager import switch_3d_entities

bl_info = {
    'name': 'Robossembler Tools',
    'author': 'brothermechanic@gmail.com',
    'version': (0, 2),
    'blender': (4, 2, 0),
    'location': '3D View > Toolbox',
    'description': 'Robossembler pipeline tools',
    'warning': '',
    'wiki_url': '',
    'tracker_url': 'https://gitlab.com/robossembler',
    'category': 'Robossembler',
}


class addon_Properties(PropertyGroup):

    engine: EnumProperty(
        name='Engine',
        description='Selest Target Engine',
        items=[('BULLET', 'Bullet', ''),
               ('ODE', 'O D E', ''),
               ('SIMBODY', 'Simbody', ''),
               ('OPENSIM', 'OpenSim', '')
               ]
    )

    entity: EnumProperty(
        name='Entity',
        description='Selest 3d Entity',
        items=[('hp', 'Highpoly', ''),
               ('mp', 'Modpoly', ''),
               ('lp', 'Lowpoly', '')
               ]
    )

    json_file_path: StringProperty(
        name='File Path',
        description='Input/output Json file path',
        default='',
        maxlen=1023,
        subtype='FILE_PATH'
    )

    ros2bag_file_path: StringProperty(
        name='File Path',
        description='Input Ros2Bag file path',
        default='',
        maxlen=1023,
        subtype='FILE_PATH'
    )

    refs_file_path: StringProperty(
        name='Dir Path',
        description='References library file path',
        default='',
        maxlen=1023,
        subtype='DIR_PATH'
    )


class RobossemblerPanel1(Panel):
    ''' Robossembler UI'''
    bl_idname = 'ROBOSSEMBLER_PT_EXPORT_JSON'
    bl_label = 'Export Scene as Json'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Robossembler'

    def draw(self, context):
        prop = context.scene.robossembler_properties

        layout = self.layout
        layout.prop(prop, 'json_file_path')
        layout.prop(prop, 'engine')

        col = layout.column()
        col.alert = True
        col.scale_y = 2.0
        col.operator('scene.export_json',
                     icon='WORLD_DATA',
                     text='Export Scene')


class RobossemblerPanel2(Panel):
    '''Doc'''
    bl_idname = 'ROBOSSEMBLER_PT_IMPORT_ANIMATION'
    bl_label = 'Import Ros2Bag Animation'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Robossembler'

    def draw(self, context):
        prop = context.scene.robossembler_properties

        layout = self.layout
        layout.prop(prop, 'ros2bag_file_path')

        col = layout.column()
        col.alert = True
        col.scale_y = 2.0
        col.operator('scene.import_ros2bag',
                     icon='ACTION',
                     text='Import Animation')


class RobossemblerPanel3(Panel):
    ''' Robossembler UI'''
    bl_idname = 'ROBOSSEMBLER_PT_ENTITY_MANAGER'
    bl_label = 'Switch 3d Entities'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Robossembler'

    def draw(self, context):
        prop = context.scene.robossembler_properties

        layout = self.layout
        layout.prop(prop, 'refs_file_path')
        layout.prop(prop, 'entity')

        col = layout.column()
        col.alert = True
        col.scale_y = 2.0
        col.operator('scene.manage_entities',
                     icon='ASSET_MANAGER',
                     text='Switch Entities')


class RobossemblerOperator1(Operator):
    '''Tooltip'''
    bl_idname = 'scene.export_json'
    bl_label = ''
    bl_description = 'Export scene liks to json config operator.'
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        prop = context.scene.robossembler_properties

        file_path = os.path.realpath(bpy.path.abspath((prop.json_file_path)))
        physics_engine = prop.engine
        export_json(context, file_path, physics_engine)

        return {'FINISHED'}


class RobossemblerOperator2(Operator):
    '''Tooltip'''
    bl_idname = 'scene.import_ros2bag'
    bl_label = ''
    bl_description = 'Import Ros2Bag animation to scene and apply it to liks.'
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        prop = context.scene.robossembler_properties

        file_path = os.path.realpath(bpy.path.abspath((prop.ros2bag_file_path)))
        set_animation_data(context, file_path)

        return {'FINISHED'}


class RobossemblerOperator3(Operator):
    '''Tooltip'''
    bl_idname = 'scene.manage_entities'
    bl_label = ''
    bl_description = 'Switch visual 3d entities.'
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        prop = context.scene.robossembler_properties

        file_path = os.path.realpath(bpy.path.abspath((prop.refs_file_path)))
        print('1'*10, file_path)
        entity = prop.entity
        switch_3d_entities(context, file_path)

        return {'FINISHED'}


classes = (
    RobossemblerPanel1,
    RobossemblerPanel2,
    RobossemblerPanel3,
    RobossemblerOperator1,
    RobossemblerOperator2,
    RobossemblerOperator3,
    addon_Properties)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.robossembler_properties = PointerProperty(type=addon_Properties)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.robossembler_properties


if __name__ == '__main__':
    register()
