import grpc
import codecs
from . import lnrpc_pb2 as ln
from .lnrpc_pb2_grpc import LightningStub
import logging


class Lnd:
    def __init__(self, connstr, cert, macaroon):
        macaroon_bytes = macaroon
        macaroon = codecs.encode(macaroon_bytes, 'hex')

        def metadata_callback(context, callback):
            callback([('macaroon', macaroon)], None)

        cert_creds = grpc.ssl_channel_credentials(cert)
        auth_creds = grpc.metadata_call_credentials(metadata_callback)
        combined_creds = grpc.composite_channel_credentials(cert_creds, auth_creds)
        channel = grpc.secure_channel(connstr, combined_creds)
        self.client = LightningStub(channel)

    @property
    def status(self):
        try:
            info = self.client.GetInfo(ln.GetInfoRequest())
            if info.synced_to_chain:
                return "OK"
            return "Syncing"
        except:
            logging.exception('Lnd failed to get status')
            return "Error"
