package lndrpc

import (
	"log"

	"golang.org/x/net/context"

	lnrpc "github.com/lightningnetwork/lnd/lnrpc"
)

func GetInfo() error {
	log.Println("get info")
	ctxb := context.Background()
	client, cleanUp := GetClient()
	defer cleanUp()

	req := &lnrpc.GetInfoRequest{}
	resp, err := client.GetInfo(ctxb, req)
	if err != nil {
		return err
	}

	log.Println(resp)

	return nil
}

func lookupInvoiceRhash(rhash []byte) (*lnrpc.Invoice, error) {
	ctxb := context.Background()
	client, cleanUp := GetClient()
	defer cleanUp()

	req := &lnrpc.PaymentHash{
		RHash: rhash,
	}

	// Get back the invoice
	createdInvoice, err := client.LookupInvoice(ctxb, req)
	if err != nil {
		return nil, err
	}

	return createdInvoice, nil
}

func LookupInvoiceRhashStr(rhash string) (*lnrpc.Invoice, error) {

	ctxb := context.Background()
	client, cleanUp := GetClient()
	defer cleanUp()

	req := &lnrpc.PaymentHash{
		RHashStr: rhash,
	}

	// Get back the invoice
	createdInvoice, err := client.LookupInvoice(ctxb, req)
	if err != nil {
		return nil, err
	}

	return createdInvoice, nil

}

func AddInvoiceSat(desc string, satVal int64) (*lnrpc.Invoice, error) {
	ctxb := context.Background()
	client, cleanUp := GetClient()
	defer cleanUp()

	invoice := &lnrpc.Invoice{
		Memo: desc,

		// Value in satoshis
		Value: satVal,
	}

	resp, err := client.AddInvoice(ctxb, invoice)
	if err != nil {
		return nil, err
	}

	return lookupInvoiceRhash(resp.RHash)
}
