
import datetime

from PyQt5.QtWidgets import QDialog

from ..core.graph import Graph
from ..gen_resources.member_uic import Ui_DialogMember


class MemberDialog(QDialog, Ui_DialogMember):
    """
    classdocs
    """

    def __init__(self, account, community, person):
        """
        Constructor
        """
        super().__init__()
        self.setupUi(self)
        self.community = community
        self.account = account
        self.person = person
        self.label_uid.setText(person.uid)

        join_date = self.person.get_join_date(self.community)
        join_date = datetime.datetime.fromtimestamp(join_date).strftime("%d/%m/%Y %I:%M")
        if join_date is None:
            join_date = 'not a member'

        # calculate path to account member
        graph = Graph(self.community)
        path = None
        # if selected member is not the account member...
        if person.pubkey != self.account.pubkey:
            # add path from selected member to account member
            path = graph.get_shortest_path_between_members(person, self.account.get_person())

        text = """
            <table cellpadding="5">
            <tr><td align="right"><b>{:}</b></div></td><td>{:}</td></tr>
            <tr><td align="right"><b>{:}</b></div></td><td>{:}</td></tr>
            """.format(
                'Public key',
                self.person.pubkey,
                'Join date',
                join_date
            )

        if path:
            distance = len(path) - 1
            text += """<tr><td align="right"><b>{:}</b></div></td><td>{:}</td></tr>""".format('Distance', distance)
            if distance > 1:
                index = 0
                for node in path:
                    if index == 0:
                        text += """<tr><td align="right"><b>{:}</b></div></td><td>{:}</td></tr>""".format('Path', node['text'])
                    else:
                        text += """<tr><td align="right"><b>{:}</b></div></td><td>{:}</td></tr>""".format('', node['text'])
                    if index == distance and node['id'] != self.account.pubkey:
                        text += """<tr><td align="right"><b>{:}</b></div></td><td>{:}</td></tr>""".format('', self.account.name)
                    index += 1
        # close html text
        text += "</table>"

        # set text in label
        self.label_properties.setText(text)


@@ -134,16 +134,70 @@
 gMember%0A
+from ..tools.exceptions import MembershipNotFoundError
 %0A%0Aclass 
@@ -556,16 +556,33 @@
 n.uid)%0A%0A
+        try:%0A    
         
@@ -635,32 +635,189 @@
 munity)%0A        
+except MembershipNotFoundError:%0A            join_date = None%0A%0A        if join_date is None:%0A            join_date = 'not a member'%0A        else:%0A            
 join_date = date
@@ -885,77 +885,8 @@
 %25M%22)
-%0A        if join_date is None:%0A            join_date = 'not a member'
 %0A%0A  

fix bug popup infos member on a non member