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

// LinearLayout lays out all widgets added one after the other
// It will fill as much space as you give it
type LinearLayout struct {
	id    string
	style tcell.Style

	orientation LinearLayoutOrient

	x, y          int
	w, h          int
	widgets       []Widget
	layoutFlags   map[Widget]LayoutFlag
	layoutWeights map[Widget]int
	totalWeight   int

	active     bool
	visible    bool
	tabbable   bool
	disableTab bool

	cursor int
	logger func(string, ...any)
}

type LinearLayoutOrient int

const (
	LinLayV = LinearLayoutOrient(iota)
	LinLayH
)

var _ Widget = (*LinearLayout)(nil)

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

func (w *LinearLayout) Init(id string, s tcell.Style) {
	w.id = id
	w.style = s
	w.visible = true
	w.tabbable = true
	w.layoutFlags = make(map[Widget]LayoutFlag)
	w.layoutWeights = make(map[Widget]int)
}

func (w *LinearLayout) Id() string { return w.id }
func (w *LinearLayout) HandleResize(ev *tcell.EventResize) {
	w.w, w.h = ev.Size()
	// w.w = wh.Max(w.w, w.WantW())
	// w.h = wh.Max(w.h, w.WantH())
	w.updateWidgetLayouts()
}

func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
	// First, see if the active widget handles the key
	w.Log("LL: Handling Event: %s", ev.Name())
	if len(w.widgets) > w.cursor {
		w.Log("LL: Pushing to Widget: %s", w.widgets[w.cursor].Id())
		if w.widgets[w.cursor].HandleKey(ev) {
			return true
		}
	}

	if ev.Key() == tcell.KeyTab && !w.disableTab {
		return w.handleTab()
	}

	return false
}

func (w *LinearLayout) HandleTime(ev *tcell.EventTime) {
	for _, wi := range w.widgets {
		wi.HandleTime(ev)
	}
}

func (w *LinearLayout) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	pos := w.GetPos()
	for _, wd := range w.widgets {
		pos.DrawOffset(wd, screen)
	}
}

func (w *LinearLayout) Active() bool { return w.active }
func (w *LinearLayout) SetActive(a bool) {
	w.active = a
	w.updateActive()
}
func (w *LinearLayout) Visible() bool      { return w.visible }
func (w *LinearLayout) SetVisible(a bool)  { w.visible = a }
func (w *LinearLayout) Focusable() bool    { return true }
func (w *LinearLayout) SetTabbable(b bool) { w.tabbable = b }
func (w *LinearLayout) Tabbable() bool     { return w.tabbable }
func (w *LinearLayout) SetX(x int)         { w.x = x }
func (w *LinearLayout) SetY(y int)         { w.y = y }
func (w *LinearLayout) GetX() int          { return w.x }
func (w *LinearLayout) GetY() int          { return w.y }
func (w *LinearLayout) GetPos() Coord      { return Coord{X: w.x, Y: w.y} }
func (w *LinearLayout) SetPos(c Coord)     { w.x, w.y = c.X, c.Y }
func (w *LinearLayout) GetW() int          { return w.w }
func (w *LinearLayout) GetH() int          { return w.h }
func (w *LinearLayout) SetW(wd int)        { w.w = wd }
func (w *LinearLayout) SetH(h int)         { w.h = h }
func (w *LinearLayout) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *LinearLayout) WantW() int {
	var wantW int
	for _, wd := range w.widgets {
		switch w.orientation {
		case LinLayV:
			// Find the highest want of all widgets
			wantW = wh.Max(wd.WantW(), wantW)
		case LinLayH:
			// Find the sum of all widget widgets wants
			wantW = wantW + wd.WantW()
		}
	}
	return wantW
}

func (w *LinearLayout) WantH() int {
	var wantH int
	for _, wd := range w.widgets {
		switch w.orientation {
		case LinLayV:
			// Find the sum of all widget widgets wants
			wantH = wantH + wd.WantH()
		case LinLayH:
			// Find the highest want of all widgets
			wantH = wh.Max(wd.WantH(), wantH)
		}
	}
	return wantH
}

func (w *LinearLayout) MinW() int {
	var minW int
	for _, wd := range w.widgets {
		switch w.orientation {
		case LinLayV:
			// Find the highest minimum width of all widgets
			minW = wh.Max(wd.MinW(), minW)
		case LinLayH:
			// Find the sum of all widget minimum widgets
			minW = minW + wd.MinW()
		}
	}
	return minW
}

func (w *LinearLayout) MinH() int {
	var minH int
	for _, wd := range w.widgets {
		switch w.orientation {
		case LinLayV:
			minH = minH + wd.MinH()
		case LinLayH:
			minH = wh.Max(wd.MinH(), minH)
		}
	}
	return minH
}

// Find the widget at 'cursor' and set it active.
// All others to inactive
// If this layout is not active, everything is inactive
func (w *LinearLayout) updateActive() {
	if !w.active {
		w.Log("LL: Not Active")
	}
	for i, wd := range w.widgets {
		if i == w.cursor {
			w.Log("LL: Setting Active: %s", w.widgets[i].Id())
		}
		wd.SetActive(i == w.cursor && w.active)
	}
}

func (w *LinearLayout) SetOrientation(o LinearLayoutOrient) { w.orientation = o }
func (w *LinearLayout) IndexOf(n Widget) int {
	for i := range w.widgets {
		if w.widgets[i] == n {
			return i
		}
	}
	return -1
}

func (w *LinearLayout) Contains(n Widget) bool {
	return w.IndexOf(n) >= 0
}

func (w *LinearLayout) Add(n Widget) {
	if w.Contains(n) {
		// If the widget is already in the layout, move it to the end
		pFlags, pWeight := w.layoutFlags[n], w.layoutWeights[n]
		w.Delete(n)
		w.layoutFlags[n], w.layoutWeights[n] = pFlags, pWeight
	}
	w.widgets = append(w.widgets, n)
	// If we don't already have a weight set, set it to 1
	if _, ok := w.layoutWeights[n]; !ok {
		w.layoutWeights[n] = 1
	}
	w.updateTotalWeight()
}

func (w *LinearLayout) Insert(n Widget, idx int) {
	if idx >= len(w.widgets) {
		w.Add(n)
		return
	}
	if pos := w.IndexOf(n); pos >= 0 {
		if pos < idx {
			idx--
		}
		// Preserve the flags & weight
		pFlags, pWeight := w.layoutFlags[n], w.layoutWeights[n]
		w.Delete(n)
		w.layoutFlags[n], w.layoutWeights[n] = pFlags, pWeight
	}
	w.widgets = append(w.widgets[:idx], append([]Widget{n}, w.widgets[idx:]...)...)
	// If we don't already have a weight set, set it to 1
	if _, ok := w.layoutWeights[n]; !ok {
		w.layoutWeights[n] = 1
	}
	w.updateTotalWeight()
}

func (w *LinearLayout) Delete(n Widget) {
	for i := 0; i < len(w.widgets); i++ {
		if w.widgets[i] == n {
			w.DeleteIndex(i)
			return
		}
	}
}

func (w *LinearLayout) DeleteIndex(idx int) {
	if idx < len(w.widgets) {
		p := w.widgets[idx]
		w.widgets = append(w.widgets[:idx], w.widgets[idx+1:]...)
		delete(w.layoutFlags, p)
		delete(w.layoutWeights, p)
		w.updateTotalWeight()
	}
}

func (w *LinearLayout) AddFlag(wd Widget, f LayoutFlag) {
	if f.IsAlignH() {
		w.layoutFlags[wd].ClearAlignH()
	} else if f.IsAlignV() {
		w.layoutFlags[wd].ClearAlignV()
	}
	w.layoutFlags[wd].Add(f)
}

func (w *LinearLayout) RemoveFlag(wd Widget, f LayoutFlag) {
	// Removing an alignment flag centers that direction
	if f.IsAlignH() {
		w.layoutFlags[wd].ClearAlignH()
	} else if f.IsAlignV() {
		w.layoutFlags[wd].ClearAlignV()
	}
}

func (w *LinearLayout) SetWeight(wd Widget, wt int) {
	if !w.Contains(wd) {
		return
	}
	w.layoutWeights[wd] = wt
	w.updateTotalWeight()
}

func (w *LinearLayout) updateTotalWeight() {
	w.totalWeight = 0
	for _, v := range w.layoutWeights {
		w.totalWeight += v
	}
}

func (w *LinearLayout) updateWidgetLayouts() {
	switch w.orientation {
	case LinLayV:
		for _, wd := range w.widgets {
			w.updateLLVWidgetSize(wd)
			w.updateLLVWidgetPos(wd)
		}
	case LinLayH:
		for _, wd := range w.widgets {
			w.updateLLHWidgetSize(wd)
			w.updateLLHWidgetPos(wd)
		}
	}
}

// The Layout should have a static Size set at this point that we can use
// For now we're centering all views in the Layout (on both axes)
//
// We need to determine the allowed size of this widget so we can determine
// it's position
func (w *LinearLayout) updateLLVWidgetSize(wd Widget) {
	wd.HandleResize((&Coord{X: w.w, Y: w.getWeightedH(wd)}).ResizeEvent())
}

func (w *LinearLayout) updateLLHWidgetSize(wd Widget) {
	wd.HandleResize((&Coord{X: w.getWeightedW(wd), Y: w.h}).ResizeEvent())
}

// The Layout should have a static Size set at this point that we can use
// For now we're centering all views in the Layout (on both axes)
// TODO: Use LayoutFlags to determine alignment in each 'cell'
//
// The position and size of each widget before this should be correct
// This widget should also know its size by now. We just need to
// position it relative to the layout.
func (w *LinearLayout) updateLLVWidgetPos(wd Widget) {
	c := Coord{}
	for i := range w.widgets {
		if w.widgets[i] == wd {
			break
		}
		c.Y += w.getWeightedH(w.widgets[i])
	}

	// Do we have a layout flag for this widget?
	var ok bool
	var flgs LayoutFlag
	if flgs, ok = w.layoutFlags[wd]; !ok {
		flgs = LayoutFlag(LFAlignHCenter | LFAlignVCenter)
	}

	// c.Y is the top of this 'cell'
	if wd.GetH() < w.getWeightedH(wd) {
		// But we've got some extra space
		switch flgs.AlignV() {
		case LFAlignVBottom:
			c.Y += w.getWeightedH(wd) - wd.GetH()
		case LFAlignVCenter:
			c.Y += (w.getWeightedH(wd) / 2) - (wd.GetH() / 2)
		}
	}
	c.X = int((float64(w.w) / 2) - (float64(wd.GetW()) / 2))
	wd.SetPos(c)
}

func (w *LinearLayout) updateLLHWidgetPos(wd Widget) {
	c := Coord{}
	for i := range w.widgets {
		if w.widgets[i] == wd {
			break
		}
		c.X += w.getWeightedW(w.widgets[i])
	}
	// Do we have a layout flag for this widget?
	var ok bool
	var flgs LayoutFlag
	if flgs, ok = w.layoutFlags[wd]; !ok {
		flgs = LayoutFlag(LFAlignHCenter | LFAlignVCenter)
	}

	// c.X is the left-most of this 'cell'
	if wd.GetW() < w.getWeightedW(wd) {
		// But we've got some extra space.
		switch flgs.AlignH() {
		case LFAlignHRight:
			c.X += w.getWeightedW(wd) - wd.GetW()
		case LFAlignHCenter:
			c.X += (w.getWeightedW(wd) / 2) - (wd.GetW() / 2)
		}
	}
	c.Y = int((float64(w.h) / 2) - (float64(wd.GetH()) / 2))
	wd.SetPos(c)
}

func (w *LinearLayout) updateWidgetPos(wd Widget) {
	prevP, prevS := 0, 0
	for _, wrk := range w.widgets {
		c := Coord{}
		switch w.orientation {
		case LinLayV:

			c.X, c.Y = 0, prevP+prevS
			prevP, prevS = c.Y, wrk.GetH()
		case LinLayH:
			c.X, c.Y = prevP+prevS, 0
			prevP, prevS = c.X, wrk.GetW()
		}
		wd.SetPos(c)
	}
}

func (w *LinearLayout) getWeightedH(wd Widget) int {
	if !w.Contains(wd) {
		return 0
	}
	wght := w.layoutWeights[wd]
	if wght == 0 {
		wght = 1
	}
	ret := int(float64(w.h) * (float64(wght) / float64(w.totalWeight)))
	return ret
}

func (w *LinearLayout) getWeightedW(wd Widget) int {
	if !w.Contains(wd) {
		return 0
	}
	wght := w.layoutWeights[wd]
	if wght == 0 {
		wght = 1
	}
	return int(float64(w.w) * (float64(wght) / float64(w.totalWeight)))
}

func (w *LinearLayout) handleTab() bool {
	beg := w.cursor
	// Find the next tabbable widget
	w.cursor = (w.cursor + 1) % len(w.widgets)
	for !w.widgets[w.cursor].Tabbable() && beg != w.cursor {
		w.cursor = (w.cursor + 1) % len(w.widgets)
	}
	w.updateActive()
	return w.cursor > 0 && w.cursor != beg
}

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