/*
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 (
	wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
	"github.com/gdamore/tcell"
)

type MenuItem struct {
	id       string
	label    string
	style    tcell.Style
	active   bool
	visible  bool
	tabbable bool
	x, y     int
	w, h     int

	menuType  MenuType
	cursor    int
	items     []*MenuItem
	onPressed func() bool

	manualExpand bool
	expanded     bool
	disabled     bool

	keyMap KeyMap
}

var _ Widget = (*MenuItem)(nil)

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

func (w *MenuItem) Init(id string, style tcell.Style) {
	w.id = id
	w.style = style
	w.visible = true
	w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
		tcell.KeyUp:   w.MoveUp,
		tcell.KeyDown: w.MoveDown,
		tcell.KeyEnter: func(ev *tcell.EventKey) bool {
			if w.onPressed != nil {
				return w.onPressed()
			} else if len(w.items) > w.cursor && w.items[w.cursor].HandleKey(ev) {
				return true
			}
			return false
		},
	})
	for i := range w.items {
		w.items[i].SetActive(i == w.cursor)
	}
	w.tabbable = true
}
func (w *MenuItem) Id() string { return w.id }
func (w *MenuItem) 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())
	// TODO: Trickle-down HandleResize
}

func (w *MenuItem) HandleKey(ev *tcell.EventKey) bool {
	if !w.active {
		return false
	}
	// Look for a sub-item that's selected
	return w.keyMap.Handle(ev)
}
func (w *MenuItem) HandleTime(ev *tcell.EventTime) {}
func (w *MenuItem) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	st := w.style
	if w.active {
		st = w.style.Reverse(true)
	}
	if w.disabled {
		st = st.Dim(true)
		st = st.Italic(true)
	}

	x, y := w.x, w.y
	wd := w.w
	wh.DrawText(x, y, wh.PadR(w.label, wd), st, screen)
	y += 1
	if w.expanded {
		if len(w.items) > 0 {
			wh.TitledBorderFilled(w.x-1, w.y, w.x+w.WantW(), w.y+w.WantH(), w.label, wh.BRD_CSIMPLE, w.style, screen)
		}
		x += 1
		for i := range w.items {
			// TODO: Use DrawOffset
			w.items[i].SetPos(Coord{X: x, Y: y})
			w.items[i].Draw(screen)
			y++
		}
	}
}

func (w *MenuItem) Active() bool { return w.active }
func (w *MenuItem) SetActive(a bool) {
	w.active = a
	if !w.manualExpand {
		w.expanded = a
	}
}
func (w *MenuItem) Visible() bool     { return w.visible }
func (w *MenuItem) SetVisible(a bool) { w.visible = a }
func (w *MenuItem) SetX(x int)        { w.x = x }
func (w *MenuItem) SetY(y int)        { w.y = y }
func (w *MenuItem) GetX() int         { return w.x }
func (w *MenuItem) GetY() int         { return w.y }
func (w *MenuItem) GetPos() Coord     { return Coord{X: w.x, Y: w.y} }
func (w *MenuItem) SetPos(c Coord)    { w.x, w.y = c.X, c.Y }
func (w *MenuItem) SetW(x int)        { w.w = x }
func (w *MenuItem) SetH(y int)        { w.h = y }
func (w *MenuItem) GetW() int         { return w.w }
func (w *MenuItem) GetH() int         { return w.y }
func (w *MenuItem) MinW() int         { return w.w }
func (w *MenuItem) MinH() int         { return w.y }
func (w *MenuItem) WantW() int {
	ret := len(w.label) + 2
	if len(w.items) > 0 {
		for i := range w.items {
			if w.items[i].WantW() > ret {
				ret = w.items[i].WantW() + 1
				// TODO: Figure offset of subitems
			}
		}
	}
	return ret
}

func (w *MenuItem) WantH() int {
	ret := 1
	if len(w.items) > 0 {
		for i := range len(w.items) {
			ret += w.items[i].WantH()
		}
	}
	return ret
}
func (w *MenuItem) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *MenuItem) Focusable() bool    { return !w.disabled }
func (w *MenuItem) SetTabbable(b bool) { w.tabbable = b }
func (w *MenuItem) Tabbable() bool     { return w.tabbable }

// How much width this item wants
func (w *MenuItem) Expand(e bool) {
	if w.manualExpand {
		w.expanded = e
	}
}

func (w *MenuItem) updateActive() {
	for i := range w.items {
		w.items[i].SetActive(i == w.cursor)
	}
}

func (w *MenuItem) MoveUp(ev *tcell.EventKey) bool {
	// Look for a previous enabled item
	st := w.cursor
	i := (w.cursor - 1 + len(w.items)) % len(w.items)
	for ; i != st; i = (i - 1 + len(w.items)) % len(w.items) {
		if !w.items[i].IsDisabled() {
			break
		}
	}
	w.cursor = i
	w.updateActive()
	return true
}

func (w *MenuItem) MoveDown(ev *tcell.EventKey) bool {
	// Look for next enabled item
	st := w.cursor
	i := (st + 1) % len(w.items)
	for ; i != st; i = (i + 1) % len(w.items) {
		if !w.items[i].IsDisabled() {
			break
		}
	}
	w.cursor = i
	w.updateActive()
	return true
}
func (w *MenuItem) SetLabel(lbl string) { w.label = lbl }
func (w *MenuItem) SetDisabled(d bool)  { w.disabled = d }
func (w *MenuItem) IsDisabled() bool    { return w.disabled }

func (w *MenuItem) AddItems(iL ...*MenuItem) {
	w.items = append(w.items, iL...)
	for i := range w.items {
		w.items[i].SetActive(i == w.cursor)
	}
}
func (w *MenuItem) SetOnPressed(p func() bool) { w.onPressed = p }
