from typing import Any, Optional

from .report_value_formatters import ReportValueFormatters


class ReportValue(object):
    """A report value with an optional display formatter.

    Attributes:
        value_content: Resolved content — raw value when no formatter, or
            ``{"value": ..., "type": ...}`` when a formatter is set.
    """

    def __init__(self, value: Any, formatter: Optional[ReportValueFormatters] = None):
        """Creates a ReportValue with an optional display formatter.

        Args:
            value: The numeric or string value.
            formatter: Optional formatter that controls how the value is displayed.
        """
        self._value = value
        self._formatter = formatter

        if self._formatter is None:
            self.value_content = value
        else:
            self.value_content = {
                "value": value,
                "type": formatter.value,
            }

    def add_to_value(self, value: Any):
        """Adds a value to the current value in-place.

        Args:
            value: The amount to add. If a ``ReportValue``, its inner value is used.
        """
        value_to_add = value

        if type(value_to_add) is ReportValue:
            value_to_add = value_to_add._value

        if self._formatter is None:
            self.value_content += value_to_add
            self._value = self.value_content
        else:
            self.value_content["value"] += value_to_add
            self._value = self.value_content["value"]

    def clone(self) -> "ReportValue":
        """Returns a new ``ReportValue`` with the same value and formatter.

        Returns:
            A shallow copy of this instance.
        """
        return ReportValue(self._value, self._formatter)
