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

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

type SimpleListWithHelp struct {
	id      string
	style   tcell.Style
	x, y    int
	w, h    int
	visible bool

	list *t.SimpleList
	help *t.Text
}

func NewSimpleListWithHelp(id string, s tcell.Style) *SimpleListWithHelp {
	ret := &SimpleListWithHelp{}
	ret.Init(id, s)
	return ret
}

func (w *SimpleListWithHelp) Init(id string, s tcell.Style) {
	w.id = id
	w.style = s
	w.visible = true
	w.list = t.NewSimpleList(fmt.Sprintf("%s.list", id), s)
	w.help = t.NewText(fmt.Sprintf("%s.help", id), s)
}

func (w *SimpleListWithHelp) Id() string { return w.id }
func (w *SimpleListWithHelp) HandleResize(ev *tcell.EventResize) {
	w.w, w.h = ev.Size()
	hh := 0
	if w.list.Active() {
		hh = w.help.WantH()
	}
	w.help.HandleResize((t.Coord{X: w.w, Y: hh}).ResizeEvent())
	w.help.SetPos(t.Coord{X: 1, Y: w.h - hh})
	w.list.HandleResize((t.Coord{X: w.w, Y: w.h - hh}).ResizeEvent())
	w.list.SetPos(t.Coord{X: 0, Y: 0})
}

func (w *SimpleListWithHelp) GetKeyMap() *t.KeyMap   { return w.list.GetKeyMap() }
func (w *SimpleListWithHelp) SetKeyMap(km *t.KeyMap) { w.list.SetKeyMap(km) }

func (w *SimpleListWithHelp) HandleKey(ev *tcell.EventKey) bool { return w.list.HandleKey(ev) }

func (w *SimpleListWithHelp) HandleTime(ev *tcell.EventTime) {
	w.list.HandleTime(ev)
	w.help.HandleTime(ev)
}

func (w *SimpleListWithHelp) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	w.GetPos().DrawOffset(w.list, screen)
	w.GetPos().DrawOffset(w.help, screen)
}

func (w *SimpleListWithHelp) Active() bool { return w.list.Active() }
func (w *SimpleListWithHelp) SetActive(a bool) {
	w.list.SetActive(a)
	w.help.SetVisible(a)
}
func (w *SimpleListWithHelp) Visible() bool       { return w.visible }
func (w *SimpleListWithHelp) SetVisible(a bool)   { w.visible = a }
func (w *SimpleListWithHelp) Focusable() bool     { return w.list.Focusable() }
func (w *SimpleListWithHelp) SetFocusable(b bool) { w.list.SetFocusable(b) }
func (w *SimpleListWithHelp) SetX(x int)          { w.x = x }
func (w *SimpleListWithHelp) SetY(y int)          { w.y = y }
func (w *SimpleListWithHelp) GetX() int           { return w.x }
func (w *SimpleListWithHelp) GetY() int           { return w.y }
func (w *SimpleListWithHelp) GetPos() t.Coord     { return t.Coord{X: w.x, Y: w.y} }
func (w *SimpleListWithHelp) SetPos(c t.Coord)    { w.x, w.y = c.X, c.Y }
func (w *SimpleListWithHelp) GetW() int           { return w.w }
func (w *SimpleListWithHelp) GetH() int           { return w.h }
func (w *SimpleListWithHelp) SetW(wd int)         { w.w = wd }
func (w *SimpleListWithHelp) SetH(h int)          { w.h = h }
func (w *SimpleListWithHelp) SetSize(c t.Coord)   { w.w, w.h = c.X, c.Y }
func (w *SimpleListWithHelp) WantW() int          { return w.list.WantW() + w.help.WantW() }
func (w *SimpleListWithHelp) WantH() int          { return h.Max(w.list.WantH(), w.help.WantH()) }
func (w *SimpleListWithHelp) MinW() int           { return h.Max(w.list.MinW(), w.help.MinW()) }
func (w *SimpleListWithHelp) MinH() int           { return w.list.MinH() + w.help.MinH() }

// Help Text Widget
func (w *SimpleListWithHelp) SetHelp(txt string) { w.help.SetText(txt) }
func (w *SimpleListWithHelp) GetHelp() string    { return w.help.GetText() }

// SimpleList Widget
func (w *SimpleListWithHelp) SetCursorWrap(b bool)                        { w.list.SetCursorWrap(b) }
func (w *SimpleListWithHelp) MoveUp() bool                                { return w.list.MoveUp() }
func (w *SimpleListWithHelp) MoveDown() bool                              { return w.list.MoveDown() }
func (w *SimpleListWithHelp) SetTitle(ttl string)                         { w.list.SetTitle(ttl) }
func (w *SimpleListWithHelp) SetList(l []string)                          { w.list.SetList(l) }
func (w *SimpleListWithHelp) Clear()                                      { w.list.Clear() }
func (w *SimpleListWithHelp) Add(l string)                                { w.list.Add(l) }
func (w *SimpleListWithHelp) Remove(l string)                             { w.list.Remove(l) }
func (w *SimpleListWithHelp) SetBorder(brd []rune)                        { w.list.SetBorder(brd) }
func (w *SimpleListWithHelp) SetItemStyle(idx int, s tcell.Style)         { w.list.SetItemStyle(idx, s) }
func (w *SimpleListWithHelp) SetItem(idx int, txt string)                 { w.list.SetItem(idx, txt) }
func (w *SimpleListWithHelp) SetSelectedIndex(i int)                      { w.list.SetSelectedIndex(i) }
func (w *SimpleListWithHelp) SelectedIndex() int                          { return w.list.SelectedIndex() }
func (w *SimpleListWithHelp) ClearBorder()                                { w.list.ClearBorder() }
func (w *SimpleListWithHelp) SetOnSelect(s func(int, string) bool)        { w.list.SetOnSelect(s) }
func (w *SimpleListWithHelp) SetVimMode(b bool)                           { w.list.SetVimMode(b) }
func (w *SimpleListWithHelp) SetLogger(l func(string, ...any))            { w.list.SetLogger(l) }
func (w *SimpleListWithHelp) Log(txt string, args ...any)                 { w.list.Log(txt, args...) }
func (w *SimpleListWithHelp) SetOnChange(c func(idx int, nm string) bool) { w.list.SetOnChange(c) }
func (w *SimpleListWithHelp) GetSelectedItem() string                     { return w.list.GetSelectedItem() }
func (w *SimpleListWithHelp) GetAllItems() []string                       { return w.list.GetAllItems() }
func (w *SimpleListWithHelp) GetAllItemStyles() map[int]tcell.Style       { return w.list.GetAllItemStyles() }
