/*
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"

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

type Alert struct {
	id    string
	style tcell.Style

	x, y     int
	w, h     int
	active   bool
	visible  bool
	tabbable bool

	layout           *LinearLayout
	title            string
	message          *Text
	btnOk, btnCancel *Button

	keyMap KeyMap
	logger func(string, ...any)
}

var _ Widget = (*Alert)(nil)

func NewAlert(id string, style tcell.Style) *Alert {
	ret := &Alert{}
	ret.Init(id, style)
	return ret
}

func (w *Alert) Init(id string, style tcell.Style) {
	w.id = id
	w.style = style

	w.layout = NewLinearLayout(fmt.Sprintf("%s-layout", id), tcell.StyleDefault)

	w.message = NewText(fmt.Sprintf("%s-text", id), style)
	w.layout.Add(w.message)

	btnLayout := NewLinearLayout("alertbtn-layout", tcell.StyleDefault)
	btnLayout.SetOrientation(LinLayH)
	w.layout.Add(btnLayout)

	w.btnCancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
	w.btnCancel.SetLabel("Cancel")
	btnLayout.Add(w.btnCancel)

	w.btnOk = NewButton(fmt.Sprintf("%s-select", id), style)
	w.btnOk.SetLabel("Ok")
	w.btnOk.SetActive(true)
	btnLayout.Add(w.btnOk)

	w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
		tcell.KeyTab:   w.SelectNext,
		tcell.KeyRight: w.SelectNext,
		tcell.KeyDown:  w.SelectNext,
		tcell.KeyLeft:  w.SelectNext,
		tcell.KeyUp:    w.SelectNext,
		tcell.KeyEnter: w.Do,
	})
	w.tabbable = true
}
func (w *Alert) Id() string { return w.id }
func (w *Alert) 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())
	// Trim space for the borders and pass on the size to the layout
	w.layout.HandleResize(tcell.NewEventResize(w.w-2, w.h-2))
}

func (w *Alert) HandleKey(ev *tcell.EventKey) bool {
	if !w.active {
		return false
	}
	return w.keyMap.Handle(ev)
}
func (w *Alert) HandleTime(ev *tcell.EventTime) {}
func (w *Alert) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	dS := w.style
	if !w.active {
		dS = dS.Dim(true)
	}

	wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.title, wh.BRD_SIMPLE, w.style, screen)
	w.GetPos().DrawOffset(w, screen)
}

func (w *Alert) Active() bool       { return w.active }
func (w *Alert) SetActive(a bool)   { w.active = a }
func (w *Alert) Visible() bool      { return w.visible }
func (w *Alert) SetVisible(a bool)  { w.visible = a }
func (w *Alert) SetX(x int)         { w.x = x }
func (w *Alert) SetY(y int)         { w.y = y }
func (w *Alert) GetX() int          { return w.x }
func (w *Alert) GetY() int          { return w.y }
func (w *Alert) GetPos() Coord      { return Coord{X: w.x, Y: w.y} }
func (w *Alert) SetPos(c Coord)     { w.x, w.y = c.X, c.Y }
func (w *Alert) SetW(x int)         { w.w = x }
func (w *Alert) SetH(y int)         { w.h = y }
func (w *Alert) GetW() int          { return w.w }
func (w *Alert) GetH() int          { return w.y }
func (w *Alert) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *Alert) Focusable() bool    { return true }
func (w *Alert) SetTabbable(b bool) { w.tabbable = b }
func (w *Alert) Tabbable() bool     { return w.tabbable }
func (w *Alert) WantW() int {
	return 4 + wh.Max(w.message.WantW(), (w.btnOk.WantW()+w.btnCancel.WantW()))
}

func (w *Alert) WantH() int {
	return 4 + w.btnOk.WantH() + w.message.WantH()
}

// Borders + Buttons
func (w *Alert) MinW() int {
	return 2 + wh.Max(w.message.MinW(), (w.btnOk.MinW()+w.btnCancel.MinW()))
}

// Borders + Buttons + 2 lines for message
func (w *Alert) MinH() int {
	return 2 + w.message.MinH() + w.btnOk.MinH()
}

func (w *Alert) SetTitle(ttl string)   { w.title = ttl }
func (w *Alert) SetMessage(msg string) { w.message.SetText(msg) }

func (w *Alert) SetOkPressed(b func() bool)     { w.btnOk.SetOnPressed(b) }
func (w *Alert) SetCancelPressed(b func() bool) { w.btnCancel.SetOnPressed(b) }
func (w *Alert) SelectNext(ev *tcell.EventKey) bool {
	if w.btnOk.Active() {
		w.btnOk.SetActive(false)
		w.btnCancel.SetActive(true)
	} else {
		w.btnOk.SetActive(true)
		w.btnCancel.SetActive(false)
	}
	return true
}

func (w *Alert) Do(ev *tcell.EventKey) bool {
	if w.btnOk.Active() {
		return w.btnOk.HandleKey(ev)
	} else if w.btnCancel.Active() {
		return w.btnCancel.HandleKey(ev)
	}
	return false
}

func (w *Alert) SetLogger(l func(string, ...any)) {
	w.logger = l
	w.layout.SetLogger(l)
}

func (w *Alert) Log(txt string, args ...any) {
	if w.logger != nil {
		w.logger(txt, args...)
	}
}
