"""Class implementation for ellipse height interface.
"""

from typing import Dict

from apysc import Int
from apysc.type.revert_interface import RevertInterface
from apysc.type.variable_name_interface import VariableNameInterface


class EllipseHeightInterface(VariableNameInterface, RevertInterface):

    _ellipse_height: Int

    def _initialize_ellipse_height_if_not_initialized(self) -> None:
        """
        Initialize _ellipse_height attribute if it is not initialized yet.
        """
        if hasattr(self, '_ellipse_height'):
            return
        self._ellipse_height = Int(0)

    @property
    def ellipse_height(self) -> Int:
        """
        Get ellipse height value.

        Returns
        -------
        ellipse_height : Int
            Ellipse height value.
        """
        from apysc.type import value_util
        self._initialize_ellipse_height_if_not_initialized()
        return value_util.get_copy(value=self._ellipse_height)

    @ellipse_height.setter
    def ellipse_height(self, value: Int) -> None:
        """
        Update ellipse height value.

        Parameters
        ----------
        value : int or Int
            Ellipse height value.
        """
        from apysc.validation import number_validation
        number_validation.validate_integer(integer=value)
        if isinstance(value, int):
            value = Int(value)
        self._ellipse_height = value
        self._append_ellipse_height_update_expression()

    def _append_ellipse_height_update_expression(self) -> None:
        """
        Append ellipse height updating expression.
        """
        from apysc.expression import expression_file_util
        from apysc.type import value_util
        self._initialize_ellipse_height_if_not_initialized()
        if hasattr(self, '_ellipse_width'):
            width_value_str: str = value_util.get_value_str_for_expression(
                value=getattr(self, '_ellipse_width'))
        else:
            width_value_str = value_util.get_value_str_for_expression(
                value=0)
        height_value_str: str = value_util.get_value_str_for_expression(
            value=self._ellipse_height)
        expression: str = (
            f'{self.variable_name}.radius({width_value_str}, '
            f'{height_value_str});'
        )
        expression_file_util.append_js_expression(expression=expression)

    _ellipse_height_snapshots: Dict[str, int]

    def _make_snapshot(self, snapshot_name: str) -> None:
        """
        Make value's snapshot.

        Parameters
        ----------
        snapshot_name : str
            Target snapshot name.
        """
        if not hasattr(self, '_ellipse_height_snapshots'):
            self._ellipse_height_snapshots = {}
        if self._snapshot_exists(snapshot_name=snapshot_name):
            return
        self._initialize_ellipse_height_if_not_initialized()
        self._ellipse_height_snapshots[snapshot_name] = int(
            self._ellipse_height._value)

    def _revert(self, snapshot_name: str) -> None:
        """
        Revert value if snapshot exists.

        Parameters
        ----------
        snapshot_name : str
            Target snapshot name.
        """
        if not self._snapshot_exists(snapshot_name=snapshot_name):
            return
        self._ellipse_height._value = self._ellipse_height_snapshots[
            snapshot_name]
