#
# gPrime - A web-based genealogy program
#
# Copyright (C) 2000-2006  Donald N. Allingham
# Copyright (C) 2010       Michiel D. Nauta
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

"""
LdsOrdBase class for Gramps.
"""

#-------------------------------------------------------------------------
#
# Gprime modules
#
#-------------------------------------------------------------------------
from .ldsord import LdsOrd
from .const import IDENTICAL, EQUAL

#-------------------------------------------------------------------------
#
# LdsOrdBase classes
#
#-------------------------------------------------------------------------
class LdsOrdBase:
    """
    Base class for lds_ord-aware objects.
    """

    def __init__(self, source=None):
        """
        Initialize a LdsOrdBase.

        If the source is not None, then object is initialized from values of
        the source object.

        :param source: Object used to initialize the new object
        :type source: LdsOrdBase
        """

        if source:
            self.lds_ord_list = [LdsOrd(lds_ord)
                                 for lds_ord in source.lds_ord_list]
        else:
            self.lds_ord_list = []

    def to_struct(self):
        """
        Convert the data held in this object to a structure (eg,
        struct) that represents all the data elements.

        This method is used to recursively convert the object into a
        self-documenting form that can easily be used for various
        purposes, including diffs and queries.

        These structures may be primitive Python types (string,
        integer, boolean, etc.) or complex Python types (lists,
        tuples, or dicts). If the return type is a dict, then the keys
        of the dict match the fieldname of the object. If the return
        struct (or value of a dict key) is a list, then it is a list
        of structs. Otherwise, the struct is just the value of the
        attribute.

        :returns: Returns a struct containing the data of the object.
        :rtype: list
        """
        return [lds_ord.to_struct() for lds_ord in self.lds_ord_list]

    def set_from_struct(self, struct):
        """
        Convert a serialized tuple of data to an object
        """
        lds_ord_list = struct.get("lds_ord_list", [])
        self.lds_ord_list = [LdsOrd.from_struct(item) for item in lds_ord_list]

    def add_lds_ord(self, lds_ord):
        """
        Add the :class:`~.ldsord.LdsOrd` instance to the object's list of
        lds_ords.

        :param lds_ord: :class:`~.ldsord.LdsOrd` instance to add to the object's
                        lds_ord list
        :type lds_ord: list
        """
        self.lds_ord_list.append(lds_ord)

    def remove_lds_ord(self, lds_ord):
        """
        Remove the specified :class:`~.ldsord.LdsOrd` instance from the lds_ord
        list.

        If the instance does not exist in the list, the operation has no effect.

        :param lds_ord: :class:`~.ldsord.LdsOrd` instance to remove from the
                        list
        :type lds_ord: :class:`~.ldsord.LdsOrd`

        :returns: True if the lds_ord was removed, False if it was not in the
                  list.
        :rtype: bool
        """
        if lds_ord in self.lds_ord_list:
            self.lds_ord_list.remove(lds_ord)
            return True
        else:
            return False

    def get_lds_ord_list(self):
        """
        Return the list of :class:`~.ldsord.LdsOrd` instances associated with
        the object.

        :returns: Returns the list of :class:`~.ldsord.LdsOrd` instances
        :rtype: list
        """
        return self.lds_ord_list

    def set_lds_ord_list(self, lds_ord_list):
        """
        Assign the passed list to the object's list of :class:`~.ldsord.LdsOrd`
        instances.

        :param lds_ord_list: List of :class:`~.ldsord.LdsOrd` instances to be
                             associated with the object
        :type lds_ord_list: list
        """
        self.lds_ord_list = lds_ord_list

    def _merge_lds_ord_list(self, acquisition):
        """
        Merge the list of ldsord from acquisition with our own.

        :param acquisition: the ldsord list of this object will be merged with
                            the current ldsord list.
        :type acquisition: LdsOrdBase
        """
        ldsord_list = self.lds_ord_list[:]
        for addendum in acquisition.get_lds_ord_list():
            for ldsord in ldsord_list:
                equi = ldsord.is_equivalent(addendum)
                if equi == IDENTICAL:
                    break
                elif equi == EQUAL:
                    ldsord.merge(addendum)
                    break
            else:
                self.lds_ord_list.append(addendum)
