# ##### BEGIN GPL LICENSE BLOCK #####
# KeenTools for blender is a blender addon for using KeenTools in Blender.
# Copyright (C) 2019-2023  KeenTools

# 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 <https://www.gnu.org/licenses/>.
# ##### END GPL LICENSE BLOCK #####

from typing import Any

from bpy.props import IntProperty, StringProperty, FloatProperty

from ..utils.kt_logging import KTLogger
from ..addon_config import gt_settings, get_addon_preferences
from ..geotracker_config import GTConfig
from ..tracker.pinmode import PinMode
from .ui_strings import buttons
from ..preferences.hotkeys import (geotracker_keymaps_register,
                                   all_keymaps_unregister)


_log = KTLogger(__name__)


class GT_OT_PinMode(PinMode):
    bl_idname = GTConfig.gt_pinmode_idname
    bl_label = buttons[bl_idname].label
    bl_description = buttons[bl_idname].description
    bl_options = {'REGISTER', 'INTERNAL'}

    geotracker_num: IntProperty(default=-1)
    pinmode_id: StringProperty(default='')

    camera_clip_start: FloatProperty(default=0.1)
    camera_clip_end: FloatProperty(default=1000.0)

    movepin_operator_idname: str = GTConfig.gt_movepin_idname

    bus_id: IntProperty(default=-1)

    @classmethod
    def get_settings(cls) -> Any:
        return gt_settings()

    def register_hotkeys(self) -> None:
        prefs = get_addon_preferences()
        if prefs.use_hotkeys:
            _log.yellow(f'{self.__class__.__name__} register_hotkeys')
            geotracker_keymaps_register()
        else:
            _log.red(f'{self.__class__.__name__} register_hotkeys disabled')

    def unregister_hotkeys(self) -> None:
        _log.yellow(f'{self.__class__.__name__} unregister_hotkeys')
        all_keymaps_unregister()
