import contact
from messages import *


class Friend(contact.Contact):
    """
    Friend in list of friends. Can be hidden, properties 'has unread messages' and 'has alias' added
    """

    def __init__(self, *args):
        """
        :param number: number of friend.
        """
        super(Friend, self).__init__(*args)
        self._receipts = 0

    def __del__(self):
        super().__del__()

    # -----------------------------------------------------------------------------------------------------------------
    # History support
    # -----------------------------------------------------------------------------------------------------------------

    def get_receipts(self):
        return self._receipts

    receipts = property(get_receipts)  # read receipts

    def inc_receipts(self):
        self._receipts += 1

    def dec_receipt(self):
        if self._receipts:
            self._receipts -= 1
            self.mark_as_sent()

    # -----------------------------------------------------------------------------------------------------------------
    # File transfers support
    # -----------------------------------------------------------------------------------------------------------------

    def update_transfer_data(self, file_number, status, inline=None):
        """
        Update status of active transfer and load inline if needed
        """
        try:
            tr = list(filter(lambda x: x.get_type() == MESSAGE_TYPE['FILE_TRANSFER'] and x.is_active(file_number),
                        self._corr))[0]
            tr.set_status(status)
            i = self._corr.index(tr)
            if inline:  # inline was loaded
                self._corr.insert(i, inline)
            return i - len(self._corr)
        except:
            pass

    def get_unsent_files(self):
        messages = filter(lambda x: type(x) is UnsentFile, self._corr)
        return messages

    def clear_unsent_files(self):
        self._corr = list(filter(lambda x: type(x) is not UnsentFile, self._corr))

    def delete_one_unsent_file(self, time):
        self._corr = list(filter(lambda x: not (type(x) is UnsentFile and x.get_data()[2] == time), self._corr))
