/*
Copyright © Brian Buller <brian@bullercodeworks.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package widgets

import (
	"fmt"
	"strings"

	wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
	"github.com/gdamore/tcell"
)

type Button struct {
	id    string
	label string
	style tcell.Style
	x, y  int
	w, h  int

	active   bool
	visible  bool
	tabbable bool

	onPressed func() bool
}

var _ Widget = (*Button)(nil)

func NewButton(id string, style tcell.Style) *Button {
	b := &Button{}
	b.Init(id, style)
	return b
}

func (w *Button) Init(id string, style tcell.Style) {
	w.id = id
	w.style = style
	w.visible = true
	w.onPressed = func() bool { return false }
	w.tabbable = true
}
func (w *Button) Id() string { return w.id }
func (w *Button) HandleResize(ev *tcell.EventResize) {
	w.w, w.h = ev.Size()
	// w.w = wh.Min(w.w, w.WantW())
	// w.h = wh.Min(w.h, w.WantH())
}

func (w *Button) HandleKey(ev *tcell.EventKey) bool {
	if !w.active {
		return false
	}
	if ev.Key() == tcell.KeyEnter {
		return w.onPressed()
	}
	return false
}
func (w *Button) HandleTime(ev *tcell.EventTime) {}
func (w *Button) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	dStyle := w.style
	if w.active {
		dStyle = w.style.Bold(true)
	}
	switch w.h {
	case 1:
		lbl := w.label
		if w.w < len(lbl) {
			lbl = lbl[:w.w]
		} else if w.w == len(w.label)+2 {
			lbl = fmt.Sprintf("[%s]", lbl)
		} else if w.w > len(w.label)+2 {
			lbl = fmt.Sprintf("[%s]", wh.Center(lbl, w.w-2))
		}
		wh.DrawText(w.x, w.y, lbl, dStyle, screen)
	case 2:
		lbl := w.label
		if w.w < len(lbl) {
			lbl = lbl[:w.w]
		} else if w.w == len(lbl)+2 {
			lbl = fmt.Sprintf("╭%s╮", lbl)
		}
		wh.DrawText(w.x, w.y, lbl, dStyle, screen)
		wh.DrawText(w.x, w.y+1, fmt.Sprintf("╰%s╯", strings.Repeat("─", len(lbl)-2)), dStyle, screen)
		return
	}
	if w.w < 2 {
		wh.DrawText(w.x, w.y, "╬", dStyle, screen)
		return
	} else if w.w == 2 {
		wh.DrawText(w.x, w.y, "[]", dStyle, screen)
		return
	}
	lbl := wh.Center(w.label, w.w-2)
	wh.DrawText(w.x, w.y, fmt.Sprintf("╭%s╮", strings.Repeat("─", w.w-2)), dStyle, screen)
	wh.DrawText(w.x, w.y+1, fmt.Sprintf("│%s│", wh.Center(lbl, w.w-2)), dStyle, screen)
	wh.DrawText(w.x, w.y+2, fmt.Sprintf("╰%s╯", strings.Repeat("─", w.w-2)), dStyle, screen)
}

func (w *Button) Active() bool       { return w.active }
func (w *Button) SetActive(a bool)   { w.active = a }
func (w *Button) Visible() bool      { return w.visible }
func (w *Button) SetVisible(a bool)  { w.visible = a }
func (w *Button) SetX(x int)         { w.x = x }
func (w *Button) SetY(y int)         { w.y = y }
func (w *Button) GetX() int          { return w.x }
func (w *Button) GetY() int          { return w.y }
func (w *Button) GetPos() Coord      { return Coord{X: w.x, Y: w.y} }
func (w *Button) SetPos(c Coord)     { w.x, w.y = c.X, c.Y }
func (w *Button) SetW(x int)         { w.w = x }
func (w *Button) SetH(y int)         { w.h = y }
func (w *Button) GetW() int          { return w.w }
func (w *Button) GetH() int          { return w.y }
func (w *Button) WantW() int         { return 4 + len(w.label) }
func (w *Button) WantH() int         { return 3 }
func (w *Button) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *Button) Focusable() bool    { return true }
func (w *Button) SetTabbable(b bool) { w.tabbable = b }
func (w *Button) Tabbable() bool     { return w.tabbable }
func (w *Button) MinW() int          { return len(w.label) + 2 }
func (w *Button) MinH() int          { return 1 }

func (w *Button) SetLabel(l string)          { w.label = l }
func (w *Button) SetOnPressed(p func() bool) { w.onPressed = p }
