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

type Field struct {
	id    string
	style tcell.Style

	label string
	value string

	cursor   int
	visible  bool
	active   bool
	tabbable bool
	x, y     int
	w, h     int

	filter   func(*tcell.EventKey) bool
	onChange func(prev, curr string)

	keyMap KeyMap
}

var _ Widget = (*Field)(nil)

func NewField(id string, style tcell.Style) *Field {
	f := &Field{}
	f.Init(id, style)
	return f
}

func (w *Field) Init(id string, style tcell.Style) {
	w.id = id
	w.style = style
	w.visible = true
	w.filter = func(ev *tcell.EventKey) bool {
		return wh.IsBS(*ev) ||
			wh.KeyIsDisplayable(*ev)
	}
	w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
		tcell.KeyLeft:  w.handleLeft,
		tcell.KeyRight: w.handleRight,
		tcell.KeyHome:  w.handleHome,
		tcell.KeyEnd:   w.handleEnd,
		tcell.KeyCtrlU: w.clearValueBeforeCursor,
	})
	w.tabbable = true
}

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

func (w *Field) HandleKey(ev *tcell.EventKey) bool {
	if !w.active {
		return false
	}
	if wh.IsBS(*ev) {
		return w.handleBackspace(ev)
	}
	if ok := w.keyMap.Handle(ev); ok {
		return true
	}
	if w.filter != nil && !w.filter(ev) {
		return false
	}
	if ev.Key() == tcell.KeyRune {
		w.SetValue(fmt.Sprintf("%s%s%s", w.value[:w.cursor], string(ev.Rune()), w.value[w.cursor:]))
		w.cursor++
		return true
	}
	return false
}
func (w *Field) HandleTime(ev *tcell.EventTime) {}
func (w *Field) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	useStyle := w.style
	if w.active {
		useStyle = w.style.Bold(true)
	}
	x := w.x
	labelW := len(w.label)
	if labelW > 0 {
		wh.DrawText(w.x, w.y, w.label+": ", useStyle, screen)
		x = x + labelW + 2
	}
	cursor := " "
	var pre, post string
	if len(w.value) > 0 {
		pre = w.value[:w.cursor]
		if w.cursor < len(w.value) {
			cursor = string(w.value[w.cursor])
			post = w.value[w.cursor+1:]
		}
	}

	wh.DrawText(x, w.y, pre, useStyle, screen)
	x += len(pre)
	if w.active {
		wh.DrawText(x, w.y, cursor, useStyle.Reverse(true).Blink(true), screen)
	} else {
		wh.DrawText(x, w.y, cursor, useStyle, screen)
	}
	x += 1
	wh.DrawText(x, w.y, post, useStyle, screen)
}

func (w *Field) Active() bool      { return w.active }
func (w *Field) SetActive(a bool)  { w.active = a }
func (w *Field) Visible() bool     { return w.visible }
func (w *Field) SetVisible(a bool) { w.visible = a }
func (w *Field) SetX(x int)        { w.x = x }
func (w *Field) SetY(y int)        { w.y = y }
func (w *Field) GetX() int         { return w.x }
func (w *Field) GetY() int         { return w.y }
func (w *Field) GetPos() Coord     { return Coord{X: w.x, Y: w.y} }
func (w *Field) SetPos(c Coord)    { w.x, w.y = c.X, c.Y }
func (w *Field) SetW(wd int)       { w.w = wd }
func (w *Field) SetH(h int)        { w.h = h }
func (w *Field) GetW() int         { return w.w }
func (w *Field) GetH() int         { return w.h }
func (w *Field) WantW() int {
	return len(w.label) + 2 + len(w.value) + 1
}

func (w *Field) WantH() int {
	return 1
}
func (w *Field) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *Field) Focusable() bool    { return true }
func (w *Field) SetTabbable(b bool) { w.tabbable = b }
func (w *Field) Tabbable() bool     { return w.tabbable }
func (w *Field) MinW() int          { return len(w.label) + 15 }
func (w *Field) MinH() int          { return 1 }

/* Non-Widget-Interface Functions */
func (w *Field) handleBackspace(_ *tcell.EventKey) bool {
	st := w.cursor
	if w.cursor > 0 {
		w.cursor--
	}
	if w.cursor < len(w.value) {
		w.SetValue(w.value[:w.cursor] + w.value[w.cursor+1:])
	}
	if st != w.cursor {
		return true
	}
	return false
}

func (w *Field) handleLeft(ev *tcell.EventKey) bool {
	if w.cursor > 0 {
		w.cursor--
		return true
	}
	return false
}

func (w *Field) handleRight(ev *tcell.EventKey) bool {
	if w.cursor < len(w.value) {
		w.cursor++
		return true
	}
	return false
}

func (w *Field) clearValueBeforeCursor(ev *tcell.EventKey) bool {
	w.SetValue(w.value[w.cursor:])
	w.cursor = 0
	return true
}

func (w *Field) handleHome(ev *tcell.EventKey) bool {
	w.cursor = 0
	return true
}

func (w *Field) handleEnd(ev *tcell.EventKey) bool {
	w.cursor = len(w.value)
	return true
}

func (w *Field) SetLabel(l string) { w.label = l }
func (w *Field) Label() string     { return w.label }
func (w *Field) SetValue(v string) {
	prev := w.value
	w.value = v
	w.doOnChange(prev, v)
}
func (w *Field) Value() string { return w.value }

func (w *Field) SetOnChange(v func(prev, curr string)) { w.onChange = v }
func (w *Field) doOnChange(prev, curr string) {
	if w.onChange != nil {
		w.onChange(prev, curr)
	}
}
