/*
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 (
	"github.com/gdamore/tcell"
)

type AbsoluteLayout struct {
	id    string
	style tcell.Style

	x, y         int
	w, h         int
	widgets      []Widget
	wCoords      map[Widget]Coord
	wAnchor      map[Widget]AbsoluteAnchor
	wManualSizes map[Widget]Coord

	defAnchor AbsoluteAnchor

	active   bool
	visible  bool
	tabbable bool

	cursor     int
	disableTab bool

	logger func(string)
}

type AbsoluteAnchor int

const (
	AnchorTL = AbsoluteAnchor(iota) // widget starts at <startX>, <startY>
	AnchorT                         // widget starts at <middleX>-<widget width/2>, <startY>
	AnchorTR                        // widget starts at <endX>, <startY>
	AnchorL                         // widget starts at <startX>, <middleY>
	AnchorC                         // widget starts at <middleX>-<widget width/2>, <middleY>-<widget height/2>
	AnchorR                         // widget starts at <endX>-<widget width>, <middleY>-<widget height/2>
	AnchorBL                        // widget starts at <startX>, <endY>-<widget height>
	AnchorB                         // widget starts at <middleX>-<widget width/2>, <endY>-<widget height>
	AnchorBR                        // widget starts at <endX>-<widget width>, <endY>-<widget height>
	AnchorErr
)

var _ Widget = (*AbsoluteLayout)(nil)

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

func (w *AbsoluteLayout) Init(id string, s tcell.Style) {
	w.id = id
	w.style = s
	w.visible = true
	w.defAnchor = AnchorTL
	w.wCoords = make(map[Widget]Coord)
	w.wAnchor = make(map[Widget]AbsoluteAnchor)
	w.wManualSizes = make(map[Widget]Coord)
	w.tabbable = true
}

func (w *AbsoluteLayout) Id() string { return w.id }
func (w *AbsoluteLayout) 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())
	w.updateWidgetLayouts()
}

func (w *AbsoluteLayout) HandleKey(ev *tcell.EventKey) bool {
	if !w.disableTab && ev.Key() == tcell.KeyTab {
		fndP := -1
		for i := w.cursor; i < len(w.widgets); i++ {
			if fndP == -1 {
				if w.widgets[i].Active() {
					fndP = i
					w.widgets[i].SetActive(false)
					continue
				}
			} else {
				if w.widgets[i].Focusable() && w.widgets[i].Tabbable() {
					w.widgets[i].SetActive(true)
					return true
				}
			}
		}
		// If we're here, we hit the end.
		if fndP == -1 { // But didn't even find the start
			return false
		}
		for i := 0; i < fndP; i++ {
			if w.widgets[i].Focusable() && w.widgets[i].Tabbable() {
				w.widgets[i].SetActive(true)
				return true
			}
		}
		return false
	}
	for _, wi := range w.widgets {
		if wi.HandleKey(ev) {
			return true
		}
	}
	return false
}

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

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

func (w *AbsoluteLayout) Active() bool       { return w.active }
func (w *AbsoluteLayout) SetActive(a bool)   { w.active = a }
func (w *AbsoluteLayout) Visible() bool      { return w.visible }
func (w *AbsoluteLayout) SetVisible(a bool)  { w.visible = a }
func (w *AbsoluteLayout) Focusable() bool    { return true }
func (w *AbsoluteLayout) SetTabbable(b bool) { w.tabbable = b }
func (w *AbsoluteLayout) Tabbable() bool     { return w.tabbable }
func (w *AbsoluteLayout) SetX(x int)         { w.x = x }
func (w *AbsoluteLayout) SetY(y int)         { w.y = y }
func (w *AbsoluteLayout) GetX() int          { return w.x }
func (w *AbsoluteLayout) GetY() int          { return w.y }
func (w *AbsoluteLayout) GetPos() Coord      { return Coord{X: w.x, Y: w.y} }
func (w *AbsoluteLayout) SetPos(c Coord)     { w.x, w.y = c.X, c.Y }
func (w *AbsoluteLayout) GetW() int          { return w.w }
func (w *AbsoluteLayout) GetH() int          { return w.h }
func (w *AbsoluteLayout) SetW(wd int)        { w.w = wd }
func (w *AbsoluteLayout) SetH(h int)         { w.h = h }
func (w *AbsoluteLayout) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *AbsoluteLayout) WantW() int         { return w.w }
func (w *AbsoluteLayout) WantH() int         { return w.h }
func (w *AbsoluteLayout) MinW() int {
	// Find the highest value for x in all widgets GetX() + MinW()
	var minW int
	for _, wd := range w.widgets {
		wrk := wd.GetX() + wd.MinW()
		if wrk > minW {
			minW = wrk
		}
	}
	return minW
}

func (w *AbsoluteLayout) MinH() int {
	// Find the highest value for y in all widgets GetY() + MinH()
	var minH int
	for _, wd := range w.widgets {
		wrk := wd.GetY() + wd.MinH()
		if wrk > minH {
			minH = wrk
		}
	}
	return minH
}

// Add a widget at x/y
func (w *AbsoluteLayout) Add(n Widget, pos Coord) { w.AddAnchored(n, pos, w.defAnchor) }

func (w *AbsoluteLayout) AddAnchored(n Widget, pos Coord, anchor AbsoluteAnchor) {
	w.widgets = append(w.widgets, n)
	w.wCoords[n] = pos
	w.wAnchor[n] = anchor

	w.updateWidgetLayouts()
}

func (w *AbsoluteLayout) Clear() {
	w.widgets = []Widget{}
	w.wCoords = make(map[Widget]Coord)
}

func (w *AbsoluteLayout) SetDefaultAnchor(d AbsoluteAnchor) {
	if d < 0 || d > AnchorErr {
		d = AnchorTL
	}
	w.defAnchor = d
}

func (w *AbsoluteLayout) SetLogger(l func(string)) { w.logger = l }
func (w *AbsoluteLayout) Log(txt string) {
	if w.logger != nil {
		w.logger(txt)
	}
}

func (w *AbsoluteLayout) updateWidgetLayouts() {
	// In an Absolute Layout, widgets are given a definite position and anchor.
	// The anchor is a side of the layout (see AbsoluteAnchor type)
	for _, wd := range w.widgets {
		w.updateWidgetPos(wd)
		w.updateWidgetSize(wd)
	}
}

func (w *AbsoluteLayout) updateWidgetSize(wd Widget) {
	if sz, ok := w.wManualSizes[wd]; ok {
		wd.SetW(sz.X)
		wd.SetH(sz.Y)
		return
	}
	// The available space is:
	// X: Layout Width - Widget X
	// Y: Layout Height - Widget Y
	available := Coord{X: w.GetW() - wd.GetX(), Y: w.GetH() - wd.GetY()}
	ww := wd.WantW()
	if ww < available.X {
		wd.SetW(ww)
	} else if wd.MinW() < available.X {
		wd.SetW(available.X)
	} else {
		wd.SetW(wd.MinW())
	}

	wh := wd.WantH()
	if wh < available.Y {
		wd.SetH(wh)
	} else if wd.MinH() < available.Y {
		wd.SetH(available.Y)
	} else {
		wd.SetH(wd.MinH())
	}
}

// Set a widgets position relative to the layout
func (w *AbsoluteLayout) updateWidgetPos(wd Widget) { wd.SetPos(w.getRelPos(wd)) }

// Manually set the size of a widget, the Layout won't override it
func (w *AbsoluteLayout) SetWidgetSize(wd Widget, sz Coord) { w.wManualSizes[wd] = sz }

func (w *AbsoluteLayout) ShrinkWrap(wd Widget) {
	w.SetWidgetSize(wd, Coord{X: wd.MinW(), Y: wd.MinH()})
}

func (w *AbsoluteLayout) getRelPos(wd Widget) Coord {
	var p Coord
	var a AbsoluteAnchor
	var ok bool
	if p, ok = w.wCoords[wd]; !ok {
		// Default to top-left corner
		p = Coord{X: 0, Y: 0}
	}
	if a, ok = w.wAnchor[wd]; !ok {
		a = w.defAnchor
	}
	midX, midY := (w.w / 2), (w.h / 2)
	switch a {
	case AnchorTL:
		return p

	case AnchorT:
		return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: 0})

	case AnchorTR:
		return p.Add(Coord{X: w.w - wd.GetW(), Y: 0})

	case AnchorL:
		return p.Add(Coord{X: 0, Y: midY - (wd.GetH() / 2)})

	case AnchorC:
		return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: midY - (wd.GetH() / 2)})

	case AnchorR:
		return p.Add(Coord{X: w.w - wd.GetW(), Y: midY - (wd.GetH() / 2)})

	case AnchorBR:
		return p.Add(Coord{X: w.w - wd.GetW(), Y: w.h - wd.GetH()})

	case AnchorB:
		return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: w.h - wd.GetH()})

	case AnchorBL:
		return p.Add(Coord{X: 0, Y: w.h - wd.GetH()})

	}
	return p
}

func (w *AbsoluteLayout) getAbsPos(wd Widget) Coord {
	rel := w.getRelPos(wd)
	return rel.Add(Coord{X: w.x, Y: w.y})
}
