import numpy as np


class DescriptionAccessor:
    """
    Accessor class as helper to access specific objects in a pb2 object.
    """

    def __init__(self, ens, init_time, valid_time):
        """
        Define new object consisting of access values for the pb objects.

        Parameters
        ----------
        ens
        init_time
        valid_time
        """
        self.ensemble_id = ens
        self.init_time_id = init_time
        self.valid_time_id = valid_time

    @staticmethod
    def from_pb2root_to_object_list(pb2_root, acc):
        """
        Get object list in pb2 description, given the accessors in acc.

        Parameters
        ----------
        pb2_root
            Root of pb2 object
        acc
            DescriptionAccessor

        Returns
            list of objects in the pb2 object
        -------

        """
        if type(acc) is np.ndarray:
            acc = acc.item(0)
            # acc =  acc[0]

        if acc is not None and acc.ensemble_id is not None:
            eset = pb2_root.sets[acc.ensemble_id]
        else:  # no ensemble dimension
            eset = pb2_root.sets[0]
            # TODO init_time
        if acc is not None and acc.init_time_id is not None:
            print("TODO implement support for init times.")
            exit()
        if acc is not None and acc.valid_time_id is not None:
            tl_i = eset.timeline[acc.valid_time_id]
        else:
            tl_i = eset.timeline[0]

        return tl_i.objects

    def __str__(self):
        return "Ensemble: " + str(self.ensemble_id) + ", init time: " + str(self.init_time_id) + ", valid time: " + str(
            self.valid_time_id)