package element

import (
	"golang.org/x/net/html/atom"
)

const (
	TextThemeSuccess = "success"
	TextThemeDanger  = "danger"
)

type (
	TextOption func(t *Text)

	Text struct {
		Tag     atom.Atom
		Content string
		Theme   string
		Color   string
		Style   Style
	}
)

func WithTextTheme(s string) TextOption {
	return func(t *Text) {
		t.Theme = s
	}
}

func WithTextStyle(ms map[string]string) TextOption {
	return func(t *Text) {
		t.Style = ms
	}
}

func WithTextColor(color string) TextOption {
	return func(t *Text) {
		t.Color = color
	}
}

func (text *Text) Html() string {
	return text.String()
}

func (text *Text) String() string {
	var className string
	if text.Style == nil {
		text.Style = make(map[string]string)
	}
	if text.Color != "" {
		text.Style["color"] = text.Color
	}
	if text.Tag == 0 {
		text.Tag = atom.Span
	}
	if text.Theme != "" {
		className = "text-" + text.Theme
	}
	return NewElement(
		text.Tag,
		text.Content,
		WithTagAttribute("class", className),
		WithTagAttribute("style", text.Style.String()),
	).String()
}

func NewText(s string, opts ...TextOption) *Text {
	txt := &Text{Content: s, Tag: atom.Span}
	for _, cb := range opts {
		cb(txt)
	}
	return txt
}