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

// TODO: Make sure this works right... I don't think it does.
type BorderedWidget struct {
	id    string
	style tcell.Style

	x, y   int
	w, h   int
	widget Widget
	border []rune

	title    string
	active   bool
	visible  bool
	tabbable bool

	logger func(string)
}

var _ Widget = (*BorderedWidget)(nil)

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

func NewBorderedWidget(id string, wd Widget, s tcell.Style) *BorderedWidget {
	ret := &BorderedWidget{
		widget: wd,
	}
	ret.Init(id, s)
	return ret
}

func (w *BorderedWidget) Init(id string, s tcell.Style) {
	w.id = id
	w.style = s
	w.visible = true
	w.border = wh.BRD_CSIMPLE
	w.tabbable = true
}

func (w *BorderedWidget) Id() string { return w.id }
func (w *BorderedWidget) HandleResize(ev *tcell.EventResize) {
	// Trim space for border and pass the resize to the widget
	w.w, w.h = ev.Size()
	// w.w = wh.Min(w.w, w.WantW())
	// w.h = wh.Min(w.h, w.WantH())

	w.widget.HandleResize(tcell.NewEventResize(w.w-2, w.h-2))
}

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

func (w *BorderedWidget) HandleTime(ev *tcell.EventTime) {
	w.HandleTime(ev)
}

func (w *BorderedWidget) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	if len(w.title) > 0 {
		wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.title, w.border, w.style, screen)
	} else {
		wh.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.border, w.style, screen)
	}
	w.widget.SetPos(Coord{X: w.x + 1, Y: w.y + 1})
	w.GetPos().DrawOffset(w, screen)
}

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

func (w *BorderedWidget) SetBorder(r []rune)  { w.border = r }
func (w *BorderedWidget) SetTitle(ttl string) { w.title = ttl }
