package ledger

import "fmt"

type Currency string

const (
	USD = Currency("$")
)

type Delta struct {
	Date        string
	OtherDates  []string
	Name        string
	Value       float64
	Currency    Currency
	Description string
	Transaction string
	Payee       bool

	isSet    bool
	fileName string
	lineNo   int
	with     []Delta
}

func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool, fileName string, lineNo int, otherDates []string) Delta {
	return Delta{
		Date:        d,
		OtherDates:  otherDates,
		Name:        name,
		Value:       v,
		Currency:    Currency(c),
		Description: desc,
		Transaction: transaction,
		Payee:       payee,

		isSet:    isSet,
		fileName: fileName,
		lineNo:   lineNo,
	}
}

func (delta Delta) ID() string {
	return fmt.Sprintf("%.2f", delta.Value)
}

func (delta Delta) withWith(other Delta) Delta {
	other.with = nil
	delta.with = append(delta.with, other)
	return delta
}

func (delta Delta) equivalent(other Delta) bool {
	delta.fileName = ""
	delta.lineNo = 0
	delta.with = nil
	other.fileName = ""
	other.lineNo = 0
	other.with = nil
	return fmt.Sprintf("%+v", delta) == fmt.Sprintf("%+v", other)
}

func (delta Delta) Debug() string {
	return fmt.Sprintf("{@%s %s(payee=%v):\"%s\" %s%.2f %s @%s#%d}", delta.Date, delta.Name, delta.Payee, delta.Description, func() string {
		if !delta.isSet {
			return ""
		}
		return "= "
	}(), delta.Value, delta.Currency, delta.fileName, delta.lineNo)
}
