# -*- coding: utf-8 -*-
# gui/components/model_view/contour/model_view_contour_facade.py

from __future__ import annotations

from typing import TYPE_CHECKING

from error_logger import log_exception

if TYPE_CHECKING:
    from gui.components.model_view_widget import ModelViewWidget


class ModelViewContourFacade:
    """Контурный фасад для ModelViewWidget.

    Фасад вводится как стабильная точка входа для последующей миграции
    от mixin-реализаций к компонентам без массового переписывания
    вызывающего кода за один шаг.
    """

    def __init__(self, model_view: "ModelViewWidget") -> None:
        self._mv = model_view

    def start_contour_mode(self, *, overlay_enabled: bool) -> bool:
        mv = self._mv
        if not hasattr(mv, "start_contour_definition"):
            return False
        mv.start_contour_definition()
        if hasattr(mv, "set_contour_zone_overlay_enabled"):
            mv.set_contour_zone_overlay_enabled(bool(overlay_enabled))
        return True

    def stop_contour_mode(self) -> bool:
        mv = self._mv
        if not hasattr(mv, "stop_contour_definition"):
            return False
        mv.stop_contour_definition()
        return True

    def finalize_contour(self) -> bool:
        mv = self._mv
        fn = getattr(mv, "finalize_contour_selection", None)
        if not callable(fn):
            return False
        try:
            return bool(fn())
        except Exception as e:
            log_exception(__name__, "finalize_contour", e)
            return False

    def restore_contour_from_final(self) -> bool:
        mv = self._mv
        fn = getattr(mv, "restore_contour_definition_from_final", None)
        if not callable(fn):
            return False
        try:
            return bool(fn())
        except Exception as e:
            log_exception(__name__, "restore_contour_from_final", e)
            return False

    def load_contour_definition(self, points: list[tuple[float, float]]) -> bool:
        mv = self._mv
        fn = getattr(mv, "load_contour_definition", None)
        if not callable(fn):
            return False
        try:
            return bool(fn(points))
        except Exception as e:
            log_exception(__name__, "load_contour_definition", e)
            return False

    def set_aux_visibility(self, visible: bool) -> None:
        mv = self._mv
        fn = getattr(mv, "set_contour_auxiliary_visibility", None)
        if callable(fn):
            try:
                fn(bool(visible))
            except Exception as e:
                log_exception(__name__, "set_aux_visibility", e)
                return

    def is_contour_active(self) -> bool:
        mv = self._mv
        fn = getattr(mv, "is_scenario_active", None)
        if callable(fn):
            try:
                return bool(fn("contour_edit"))
            except Exception as e:
                log_exception(__name__, "is_contour_active", e)
                return False
        return False

    def set_overlay_enabled(self, enabled: bool) -> None:
        mv = self._mv
        fn = getattr(mv, "set_contour_zone_overlay_enabled", None)
        if callable(fn):
            try:
                fn(bool(enabled))
            except Exception as e:
                log_exception(__name__, "set_overlay_enabled", e)
                return
