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

// TopMenuLayout is a greedy widget and will consume all of the space you
// give it
type TopMenuLayout struct {
	id    string
	style tcell.Style

	x, y   int
	w, h   int
	menu   *Menu
	widget Widget

	active  bool
	visible bool

	logger func(string, ...any)
}

var _ Widget = (*TopMenuLayout)(nil)

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

func (w *TopMenuLayout) Init(id string, s tcell.Style) {
	w.id = id
	w.style = s
	w.visible = true

	w.menu = NewMenu(fmt.Sprintf("%s.mainmenu", id), tcell.StyleDefault)
	w.menu.SetActive(false)
	w.menu.SetType(MenuTypeH)
	w.menu.SetTabbable(false)
}

func (w *TopMenuLayout) Id() string { return w.id }
func (w *TopMenuLayout) HandleResize(ev *tcell.EventResize) {
	w.w, w.h = ev.Size()
	if w.menu != nil {
		w.menu.SetPos(Coord{X: 0, Y: 0})
		w.menu.SetSize(Coord{X: w.w, Y: 1})
	}

	if w.widget != nil {
		w.widget.SetPos(Coord{X: 0, Y: 1})
		w.widget.HandleResize(tcell.NewEventResize(w.w, w.h-1))
	}
}

func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
	if !w.active {
		return false
	}
	if ev.Key() == tcell.KeyEscape && w.menu != nil {
		w.menu.SetActive(!w.menu.Active())
		return true
	}
	if w.menu.Active() {
		return w.menu.HandleKey(ev)
	}

	// Pass the key through to the main widget
	if w.widget != nil {
		return w.widget.HandleKey(ev)
	}
	return false
}

func (w *TopMenuLayout) HandleTime(ev *tcell.EventTime) {
	w.menu.HandleTime(ev)
	w.widget.HandleTime(ev)
}

func (w *TopMenuLayout) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	if w.menu != nil {
		w.GetPos().DrawOffset(w.menu, screen)
	}
	if w.widget != nil {
		w.GetPos().DrawOffset(w.widget, screen)
	}
}

func (w *TopMenuLayout) Active() bool { return w.active }
func (w *TopMenuLayout) SetActive(a bool) {
	w.active = a
	w.widget.SetActive(a)
}
func (w *TopMenuLayout) Visible() bool      { return w.visible }
func (w *TopMenuLayout) SetVisible(a bool)  { w.visible = a }
func (w *TopMenuLayout) SetX(x int)         { w.x = x }
func (w *TopMenuLayout) SetY(y int)         { w.y = y }
func (w *TopMenuLayout) GetX() int          { return w.x }
func (w *TopMenuLayout) GetY() int          { return w.y }
func (w *TopMenuLayout) GetPos() Coord      { return Coord{X: w.x, Y: w.y} }
func (w *TopMenuLayout) SetPos(c Coord)     { w.x, w.y = c.X, c.Y }
func (w *TopMenuLayout) SetW(x int)         { w.w = x }
func (w *TopMenuLayout) SetH(y int)         { w.h = y }
func (w *TopMenuLayout) GetW() int          { return w.w }
func (w *TopMenuLayout) GetH() int          { return w.y }
func (w *TopMenuLayout) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *TopMenuLayout) Focusable() bool    { return true }
func (w *TopMenuLayout) SetTabbable(b bool) {}
func (w *TopMenuLayout) Tabbable() bool     { return true }

func (w *TopMenuLayout) WantW() int { return wh.MaxInt }
func (w *TopMenuLayout) WantH() int { return wh.MaxInt }
func (w *TopMenuLayout) MinW() int  { return 0 }
func (w *TopMenuLayout) MinH() int  { return 0 }

func (w *TopMenuLayout) Menu() *Menu                     { return w.menu }
func (w *TopMenuLayout) AddMenuItems(iL ...*MenuItem)    { w.menu.AddItems(iL...) }
func (w *TopMenuLayout) RemoveMenuItems(iL ...*MenuItem) { w.menu.RemoveItems(iL...) }

func (w *TopMenuLayout) SetWidget(wd Widget) { w.widget = wd }

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