'''
    Copyright (C) 2021 - 2025 Akaneyu

    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/>.
'''

bl_info = {
    "name": "Image Editor Plus",
    "author": "akaneyu",
    "version": (1, 11, 0),
    "blender": (4, 2, 0),
    "location": "Image",
    "warning": "",
    "description": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Paint"}

if "bpy" in locals():
    import importlib
    importlib.reload(app)
    importlib.reload(api)
    importlib.reload(operators)
    importlib.reload(ui)
    importlib.reload(ui_renderer)
    importlib.reload(utils)

import bpy
from . import app
from . import api
from . import operators
from . import ui

classes = [
    app.IMAGE_EDITOR_PLUS_WindowPropertyGroup,
    app.IMAGE_EDITOR_PLUS_LayerPropertyGroup,
    app.IMAGE_EDITOR_PLUS_ImagePropertyGroup,
    operators.IMAGE_EDITOR_PLUS_OT_make_selection,
    operators.IMAGE_EDITOR_PLUS_OT_cancel_selection,
    operators.IMAGE_EDITOR_PLUS_OT_swap_colors,
    operators.IMAGE_EDITOR_PLUS_OT_fill_with_fg_color,
    operators.IMAGE_EDITOR_PLUS_OT_fill_with_bg_color,
    operators.IMAGE_EDITOR_PLUS_OT_clear,
    operators.IMAGE_EDITOR_PLUS_OT_cut,
    operators.IMAGE_EDITOR_PLUS_OT_copy,
    operators.IMAGE_EDITOR_PLUS_OT_paste,
    operators.IMAGE_EDITOR_PLUS_OT_crop,
    operators.IMAGE_EDITOR_PLUS_OT_deselect_layer,
    operators.IMAGE_EDITOR_PLUS_OT_move_layer,
    operators.IMAGE_EDITOR_PLUS_OT_delete_layer,
    operators.IMAGE_EDITOR_PLUS_OT_change_image_layer_order,
    operators.IMAGE_EDITOR_PLUS_OT_merge_layers,
    operators.IMAGE_EDITOR_PLUS_OT_flip,
    operators.IMAGE_EDITOR_PLUS_OT_rotate,
    operators.IMAGE_EDITOR_PLUS_OT_scale,
    operators.IMAGE_EDITOR_PLUS_OT_change_canvas_size,
    operators.IMAGE_EDITOR_PLUS_OT_flip_layer,
    operators.IMAGE_EDITOR_PLUS_OT_rotate_layer,
    operators.IMAGE_EDITOR_PLUS_OT_rotate_layer_arbitrary,
    operators.IMAGE_EDITOR_PLUS_OT_scale_layer,
    operators.IMAGE_EDITOR_PLUS_OT_offset,
    operators.IMAGE_EDITOR_PLUS_OT_adjust_color,
    operators.IMAGE_EDITOR_PLUS_OT_adjust_brightness,
    operators.IMAGE_EDITOR_PLUS_OT_adjust_gamma,
    operators.IMAGE_EDITOR_PLUS_OT_adjust_color_curve,
    operators.IMAGE_EDITOR_PLUS_OT_replace_color,
    operators.IMAGE_EDITOR_PLUS_OT_blur,
    operators.IMAGE_EDITOR_PLUS_OT_sharpen,
    operators.IMAGE_EDITOR_PLUS_OT_add_noise,
    operators.IMAGE_EDITOR_PLUS_OT_pixelize,
    operators.IMAGE_EDITOR_PLUS_OT_make_seamless,
    operators.IMAGE_EDITOR_PLUS_OT_normal_map,
    ui.IMAGE_EDITOR_PLUS_OT_scale_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_change_canvas_size_dialog,
    ui.IMAGE_EDITOR_PLUS_OffsetPropertyGroup,
    ui.IMAGE_EDITOR_PLUS_OT_offset_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_adjust_color_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_adjust_brightness_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_adjust_gamma_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_adjust_color_curve_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_replace_color_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_blur_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_sharpen_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_add_noise_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_pixelize_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_make_seamless_dialog,
    ui.IMAGE_EDITOR_PLUS_OT_normal_map_dialog,
    ui.IMAGE_EDITOR_PLUS_UL_layer_list,
    ui.IMAGE_EDITOR_PLUS_MT_edit_menu,
    ui.IMAGE_EDITOR_PLUS_MT_layers_menu,
    ui.IMAGE_EDITOR_PLUS_MT_transform_menu,
    ui.IMAGE_EDITOR_PLUS_MT_transform_layer_menu,
    ui.IMAGE_EDITOR_PLUS_MT_offset_menu,
    ui.IMAGE_EDITOR_PLUS_MT_adjust_menu,
    ui.IMAGE_EDITOR_PLUS_MT_filter_menu,
    ui.IMAGE_EDITOR_PLUS_PT_select_panel,
    ui.IMAGE_EDITOR_PLUS_PT_edit_panel,
    ui.IMAGE_EDITOR_PLUS_PT_layers_panel,
    ui.IMAGE_EDITOR_PLUS_PT_transform_panel,
    ui.IMAGE_EDITOR_PLUS_PT_transform_layer_panel,
    ui.IMAGE_EDITOR_PLUS_PT_offset_panel,
    ui.IMAGE_EDITOR_PLUS_PT_adjust_panel,
    ui.IMAGE_EDITOR_PLUS_PT_filter_panel
]

def register_keymaps():
    session = app.get_session()

    keymaps = session.keymaps

    wm = bpy.context.window_manager

    km = wm.keyconfigs.addon.keymaps.new(name='Image Generic', space_type='IMAGE_EDITOR')
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_make_selection.bl_idname,
            'B', 'PRESS')
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_cancel_selection.bl_idname,
            'D', 'PRESS', ctrl=True)
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_cut.bl_idname,
            'X', 'PRESS', ctrl=True)
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_copy.bl_idname,
            'C', 'PRESS', ctrl=True)
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_paste.bl_idname,
            'V', 'PRESS', ctrl=True)
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_fill_with_fg_color.bl_idname,
            'DEL', 'PRESS', ctrl=True)
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_clear.bl_idname,
            'DEL', 'PRESS')
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_move_layer.bl_idname,
            'G', 'PRESS')
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_rotate_layer_arbitrary.bl_idname,
            'R', 'PRESS')
    kmi = km.keymap_items.new(operators.IMAGE_EDITOR_PLUS_OT_scale_layer.bl_idname,
            'C', 'PRESS')

    keymaps.append(km)

def unregister_keymaps():
    session = app.get_session()

    wm = bpy.context.window_manager

    for km in session.keymaps:
        wm.keyconfigs.addon.keymaps.remove(km)

def register():
    global draw_handler

    api.API.VERSION = bl_info['version']

    app.load_icons()

    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.IMAGE_MT_image.append(ui.menu_func)

    bpy.app.handlers.save_pre.append(app.save_pre_handler)

    draw_handler = bpy.types.SpaceImageEditor.draw_handler_add(
            app.draw_handler, (), 'WINDOW', 'POST_PIXEL')

    wm = bpy.types.WindowManager

    wm.imageeditorplus_api = api.API
    wm.imageeditorplus_properties = \
        bpy.props.PointerProperty(type=app.IMAGE_EDITOR_PLUS_WindowPropertyGroup)
    bpy.types.Image.imageeditorplus_properties = \
        bpy.props.PointerProperty(type=app.IMAGE_EDITOR_PLUS_ImagePropertyGroup)

    register_keymaps()

def unregister():
    global draw_handler

    app.cleanup_scene()

    app.dispose_icons()

    for cls in classes:
        bpy.utils.unregister_class(cls)

    bpy.types.IMAGE_MT_image.remove(ui.menu_func)

    bpy.app.handlers.save_pre.remove(app.save_pre_handler)

    bpy.types.SpaceImageEditor.draw_handler_remove(draw_handler, 'WINDOW')

    wm = bpy.types.WindowManager

    del wm.imageeditorplus_properties
    del bpy.types.Image.imageeditorplus_properties

    unregister_keymaps()

if __name__ == "__main__":
    register()

    draw_handler = None
