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

type List struct {
	id        string
	title     string
	style     tcell.Style
	active    bool
	visible   bool
	focusable bool
	tabbable  bool

	x, y int
	w, h int

	border     []rune
	cursor     int
	cursorWrap bool
	list       []string
	itemsStyle map[int]tcell.Style

	onSelect func(int, string) bool
	keyMap   KeyMap
	vimMode  bool

	logger func(string, ...any)
}

var _ Widget = (*List)(nil)

func NewList(id string, style tcell.Style) *List {
	ret := &List{style: style}
	ret.Init(id, style)
	return ret
}

func (w *List) Init(id string, style tcell.Style) {
	w.id = id
	w.style = style
	w.focusable = true
	w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
		tcell.KeyUp:   w.MoveUp,
		tcell.KeyDown: w.MoveDown,
		tcell.KeyEnter: func(ev *tcell.EventKey) bool {
			if w.onSelect != nil && w.cursor < len(w.list) {
				return w.onSelect(w.cursor, w.list[w.cursor])
			}
			return false
		},
	})
	w.keyMap.AddRune('j', func(ev *tcell.EventKey) bool {
		if w.vimMode {
			return w.MoveDown(ev)
		}
		return false
	})
	w.keyMap.AddRune('k', func(ev *tcell.EventKey) bool {
		if w.vimMode {
			return w.MoveUp(ev)
		}
		return false
	})
	w.itemsStyle = make(map[int]tcell.Style)
	w.tabbable = true
}
func (w *List) Id() string { return w.id }
func (w *List) HandleResize(ev *tcell.EventResize) {
	w.w, w.h = ev.Size()
	// Fill as much space as we're given
	// w.w = wh.Min(w.w, w.WantW())
	// w.h = wh.Min(w.h, w.WantH())
}

func (w *List) HandleKey(ev *tcell.EventKey) bool {
	if !w.active || !w.focusable {
		return false
	}
	return w.keyMap.Handle(ev)
}
func (w *List) HandleTime(ev *tcell.EventTime) {}
func (w *List) Draw(screen tcell.Screen) {
	dS := w.style
	if !w.active {
		dS = dS.Dim(true)
	}
	x, y := w.x, w.y
	brdSz := 0
	if len(w.border) > 0 {
		brdSz = 2
		if len(w.title) > 0 {
			wh.TitledBorderFilled(x, y, x+w.w, y+w.h, w.title, w.border, dS, screen)
		} else {
			wh.BorderFilled(x, y, x+w.w, y+w.h, w.border, dS, screen)
		}
	}
	x, y = x+1, y+1
	for i := range w.list {
		rev := false
		if i == w.cursor {
			rev = true
		}
		txt := w.list[i]
		if len(txt) > w.w-brdSz {
			txt = txt[:(w.w - brdSz)]
		}
		var ok bool
		var s tcell.Style
		if s, ok = w.itemsStyle[i]; !ok {
			s = dS
		}
		wh.DrawText(x, y, txt, s.Reverse(rev), screen)
		y += 1
	}
}

func (w *List) Active() bool       { return w.active }
func (w *List) SetActive(a bool)   { w.active = a }
func (w *List) Visible() bool      { return w.visible }
func (w *List) SetVisible(a bool)  { w.visible = a }
func (w *List) SetX(x int)         { w.x = x }
func (w *List) SetY(y int)         { w.y = y }
func (w *List) GetX() int          { return w.x }
func (w *List) GetY() int          { return w.y }
func (w *List) GetPos() Coord      { return Coord{X: w.x, Y: w.y} }
func (w *List) SetPos(c Coord)     { w.x, w.y = c.X, c.Y }
func (w *List) SetW(x int)         { w.w = x }
func (w *List) SetH(y int)         { w.h = y }
func (w *List) GetW() int          { return w.w }
func (w *List) GetH() int          { return w.y }
func (w *List) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *List) Focusable() bool    { return w.focusable }
func (w *List) SetTabbable(b bool) { w.tabbable = b }
func (w *List) Tabbable() bool     { return w.tabbable }
func (w *List) WantW() int {
	lng := wh.Longest(w.list)
	if len(w.border) > 0 {
		return lng + 2
	}
	return lng
}

func (w *List) WantH() int {
	lng := len(w.list)
	if len(w.border) > 0 {
		return lng + 2
	}
	return lng
}

func (w *List) MinW() int {
	lng := wh.Longest(w.list)
	if lng > 80 {
		lng = 80
	}
	return 2 + lng
}

func (w *List) MinH() int { return 4 }

func (w *List) SetFocusable(f bool) { w.focusable = f }

func (w *List) SetCursorWrap(b bool) { w.cursorWrap = b }
func (w *List) MoveUp(ev *tcell.EventKey) bool {
	if w.cursor > 0 {
		w.cursor--
		return true
	} else if w.cursorWrap {
		w.cursor = len(w.list) - 1
		return true
	}
	return false
}

func (w *List) MoveDown(ev *tcell.EventKey) bool {
	if w.cursor < len(w.list)-1 {
		w.cursor++
		return true
	} else if w.cursorWrap {
		w.cursor = 0
		return true
	}
	return false
}
func (w *List) SetTitle(ttl string) { w.title = ttl }
func (w *List) SetList(l []string)  { w.list = l }
func (w *List) Add(l string)        { w.list = append(w.list, l) }
func (w *List) Remove(l string) {
	var idx int
	var found bool
	for idx = range w.list {
		if w.list[idx] == l {
			found = true
			break
		}
	}
	if found {
		w.list = append(w.list[:idx], w.list[idx+1:]...)
	}
}

func (w *List) SetBorder(brd []rune) {
	if len(brd) == 0 {
		w.border = wh.BRD_SIMPLE
	} else {
		w.border = wh.ValidateBorder(brd)
	}
}

func (w *List) SetItemStyle(idx int, s tcell.Style) { w.itemsStyle[idx] = s }
func (w *List) SetItem(idx int, txt string) {
	if len(w.list) < idx {
		w.list[idx] = txt
	}
}
func (w *List) GetIdx() int                          { return w.cursor }
func (w *List) ClearBorder()                         { w.border = []rune{} }
func (w *List) SetOnSelect(s func(int, string) bool) { w.onSelect = s }
func (w *List) SetVimMode(b bool)                    { w.vimMode = b }

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