import pickle
import time
import conf


class Token:
    def __init__(self, token, expire, createdAt):
        self.token = token
        self.expire = expire
        self._createdAt = createdAt
    

class User:
    def __init__(self, guid=0, account='', password='', token='', expire=0, imei='', ip='', phone='', uri=''):
        self.guid = guid
        self.account = account
        self.password = password
        self.tk = Token(token, expire, int(time.time()))
        self.imei = imei
        self.ip = ip
        self.phone = phone
        self.uri = uri


def serialize(user):
    try:
        print('serialized to:', conf.ClientId)
        fh = open(str(conf.ClientId) + '.ui', 'wb')
        pickle.dump(user, fh, 0)
        fh.close()
        return True
    except Exception:
        return False
    
def load():
    try:
        fh = open(str(conf.ClientId) + '.ui', 'rb')
        obj = pickle.load(fh)
        fh.close()
        return obj
    except Exception:
        return None


if __name__ == '__main__':
    u = User(guid=1,account="ac1",password='2343556',token='234243',expire=234,imei='23445')
    serialize(u)
    u2 = load()
    print(u2.account)
