package ln

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strings"

	"git.sp4ke.com/sp4ke/bit4sat/btc"
	"git.sp4ke.com/sp4ke/bit4sat/bus"
	"git.sp4ke.com/sp4ke/bit4sat/db"
	"git.sp4ke.com/sp4ke/bit4sat/lndrpc"
	"git.sp4ke.com/sp4ke/bit4sat/utils"
	"github.com/mediocregopher/radix/v3"
)

var (
	Client *http.Client
)

// Quick check if invoice was paid
func CheckInvoice(id string) (*Invoice, error) {

	lnInvoice, err := lndrpc.LookupInvoiceRhashStr(id)
	if err != nil {
		return nil, err
	}

	invoice := InvoiceFromLndIn(lnInvoice)

	return invoice, nil
}

// This will watch on the pubsub for paid invoices given an invoice id
func PollPaidInvoice(invoiceId string, invoicePaidChan chan<- *Invoice, errorChan chan<- error) {
	defer func() {
		log.Printf("quitting poller %s", invoiceId)
	}()

	watchPaidChannel := make(chan radix.PubSubMessage)

	busName := fmt.Sprintf("%s:%s", bus.InvoicePaidChannelPrefix, invoiceId)

	err := db.DB.RedisPubSub.Subscribe(watchPaidChannel, busName)
	if err != nil {
		log.Printf("error in poll invoice: %s", err)
		errorChan <- err
		return
	}

	log.Printf("poller: %s, listening to watchPaidChannel", invoiceId)

	for msg := range watchPaidChannel {
		log.Printf("poller received message on %s", msg.Channel)

		split := strings.Split(msg.Channel, ":")
		if len(split) != 2 {
			errorChan <- fmt.Errorf("error in pubsub channel parsing %s", msg.Channel)
		}
		targetInvoiceId := strings.Split(msg.Channel, ":")[1]
		log.Printf("target invoice is %s", targetInvoiceId)
		if targetInvoiceId == invoiceId {

			invoice := Invoice{}

			err := json.Unmarshal(msg.Message, &invoice)
			if err != nil {
				errorChan <- err
				return
			}

			invoicePaidChan <- &invoice
			close(invoicePaidChan)
			return

		}
	}
}

type InvoiceOpts struct {
	Amount float64
	Curr   Currency
	Memo   string
}

func NewInvoice(opts InvoiceOpts) (*Invoice, error) {
	var err error
	var satValue int64

	if opts.Curr == CurMSat {
		log.Println("cur is msat")
		// Convert to MSAT, meaning values under 1000 MSAT are ignored
		satValue = int64(opts.Amount / 1000)

		// Other supported currecies
	}

	if opts.Curr != CurSat {
		log.Println("cur is custom")

		// Get Sat satValue for this invoice
		msatVal, err := btc.FiatToMsat(CurrencyString[opts.Curr], opts.Amount)
		if err != nil {
			return nil, err
		}

		satValue = msatVal / 1000
	} else {
		satValue = int64(opts.Amount)
	}

	lndInvoice, err := lndrpc.AddInvoiceSat(opts.Memo, satValue)
	if err != nil {
		return nil, err
	}

	invoice := InvoiceFromLndIn(lndInvoice)
	invoice.QuotedCurrency = CurrencyString[opts.Curr]
	invoice.QuotedAmount = opts.Amount

	return invoice, nil
}

func init() {
	Client = utils.NewHttpClient()
}
