from dataclasses import field
from typing import Any, List, Optional

from ome_types.dataclasses import EMPTY, ome_dataclass

from .annotation_ref import AnnotationRef
from .text_annotation import TextAnnotation


@ome_dataclass
class XMLAnnotation(TextAnnotation):
    """An general xml annotation.

    The contents of this is not processed as OME XML but should still be well-
    formed XML.

    Parameters
    ----------
    value : Any
    annotation_ref : AnnotationRef, optional
    annotator : ExperimenterID, optional
        The Annotator is the person who attached this annotation. e.g. If
        UserA annotates something with TagB, owned by UserB, UserA is still
        the Annotator.
    description : str, optional
        A description for the annotation.
    id : AnnotationID
    namespace : str, optional
        We recommend the inclusion of a namespace for annotations you define.
        If it is absent then we assume the annotation is to use our (OME's)
        default interpretation for this type.
    """

    value: Any = EMPTY  # type: ignore
    annotation_ref: List[AnnotationRef] = field(default_factory=list)
    description: Optional[str] = None

    # NOTE: pickling this object requires xmlschema>=1.4.1

    def _to_dict(self):
        from xml.etree import ElementTree

        d = self.__dict__.copy()
        d["value"] = ElementTree.tostring(d.pop("value")).strip()
        return d

    def __eq__(self, o: "XMLAnnotation") -> bool:
        return self._to_dict() == o._to_dict()
