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

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

	x, y       int
	w, h       int
	setW, setH int
	widget     Widget

	drawRulers bool
	active     bool
	visible    bool
	tabbable   bool
	mTL, mBR   Coord // Margins (Top-Right & Bottom Left)

	logger func(string, ...any)
}

var _ Widget = (*DebugWidget)(nil)

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

func (w *DebugWidget) Init(id string, s tcell.Style) {
	w.id = id
	w.style = s
	w.visible = true
	w.tabbable = true
	w.drawRulers = true
	w.setW, w.setH = -1, -1
}

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

func (w *DebugWidget) Id() string { return w.id }
func (w *DebugWidget) HandleResize(ev *tcell.EventResize) {
	// Trim space for border and pass the resize to the widget
	sw, sh := ev.Size()
	if w.setW > 0 {
		w.w = wh.Min(sw, w.WantW())
	} else {
		w.w = sw
	}
	if w.setH > 0 {
		w.h = wh.Min(sh, w.WantH())
	} else {
		w.h = sh
	}

	nX, nY := 1, 1
	if w.w > 9 {
		nX = 2
	}
	if w.h > 9 {
		nY = 2
	}
	w.mTL = Coord{X: nX, Y: nY}
	w.mBR = Coord{X: 1, Y: 1}

	w.widget.SetPos(w.mTL)
	w.widget.HandleResize(tcell.NewEventResize(w.w-w.mTL.X-w.mBR.X, w.h-w.mTL.Y-w.mBR.Y))
}

func (w *DebugWidget) HandleKey(ev *tcell.EventKey) bool { return w.widget.HandleKey(ev) }
func (w *DebugWidget) HandleTime(ev *tcell.EventTime)    { w.widget.HandleTime(ev) }

func (w *DebugWidget) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	st := w.style
	if !w.active {
		st = st.Dim(true)
	}

	wh.Border(w.x+w.mTL.X-1, w.y+w.mTL.Y-1, w.x+w.w+w.mBR.X+1, w.y+w.h+w.mBR.Y+1, wh.BRD_CSIMPLE, st, screen)

	if w.drawRulers {
		// X Ruler
		var lastX rune
		for i := 0; i < w.w; i++ {
			str := fmt.Sprintf("%d", i)
			var wrk rune
			if len(str) == 3 {
				wrk = rune(str[1])
			} else if len(str) == 2 {
				wrk = rune(str[0])
			}
			if wrk != lastX {
				lastX = wrk
				screen.SetContent(w.x+i+w.mTL.X, w.y, wrk, nil, st)
			}
			screen.SetContent(w.x+i+w.mTL.X, w.y+w.mTL.Y-1, rune(str[len(str)-1]), nil, st)
		}

		// Y Ruler
		var lastY rune
		for i := 0; i < w.h; i++ {
			str := fmt.Sprintf("%d", i)
			if len(str) > 1 {
				wrk := rune(str[0])
				if wrk != lastY {
					lastY = wrk
					screen.SetContent(w.x, w.y+i+w.mTL.Y, wrk, nil, st)
				}
			}
			screen.SetContent(w.x+w.mTL.X-1, w.y+i+w.mTL.Y, rune(str[len(str)-1]), nil, st)
		}
	}

	w.GetPos().DrawOffset(w.widget, screen)
}

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

func (w *DebugWidget) SetH(h int) {
	w.setH = h
	w.h = h
}

func (w *DebugWidget) SetSize(c Coord) {
	w.SetW(c.X)
	w.SetH(c.Y)
}

func (w *DebugWidget) WantW() int {
	if w.setW > 0 {
		return w.setW
	}
	return w.w
}

func (w *DebugWidget) WantH() int {
	if w.setH > 0 {
		return w.setH
	}
	return w.h
}
func (w *DebugWidget) MinW() int { return 2 + w.widget.MinW() }
func (w *DebugWidget) MinH() int { return 2 + w.widget.MinH() }

func (w *DebugWidget) SetWidget(wd Widget) { w.widget = wd }
func (w *DebugWidget) EnableRulers(b bool) { w.drawRulers = b }
