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

const (
	CHECKBOX_ON = iota
	CHECKBOX_OFF
	CHECKBOX_MAYBE
)

type Checkbox struct {
	id       string
	label    string
	style    tcell.Style
	active   bool
	visible  bool
	tabbable bool
	state    int
	x, y     int
	w, h     int

	stateRunes []rune
}

var _ Widget = (*Checkbox)(nil)

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

func (w *Checkbox) Init(id string, style tcell.Style) {
	w.id = id
	w.style = style
	w.visible = true
	w.stateRunes = []rune{'X', ' ', '-'}
	w.tabbable = true
}
func (w *Checkbox) Id() string { return w.id }
func (w *Checkbox) 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 *Checkbox) HandleKey(ev *tcell.EventKey) bool {
	if !w.active {
		return false
	}
	if ev.Key() == tcell.KeyEnter {
		if w.state == CHECKBOX_ON {
			w.state = CHECKBOX_OFF
		} else {
			w.state = CHECKBOX_ON
		}
		return true
	}
	return false
}
func (w *Checkbox) HandleTime(ev *tcell.EventTime) {}
func (w *Checkbox) Draw(screen tcell.Screen) {
	if !w.visible {
		return
	}
	dStyle := w.style
	if w.active {
		dStyle = w.style.Bold(true)
	}
	wh.DrawText(w.x, w.y, fmt.Sprintf("[%s] %s", string(w.state), w.label), dStyle, screen)
}

func (w *Checkbox) Active() bool      { return w.active }
func (w *Checkbox) SetActive(a bool)  { w.active = a }
func (w *Checkbox) Visible() bool     { return w.visible }
func (w *Checkbox) SetVisible(a bool) { w.visible = a }
func (w *Checkbox) SetX(x int)        { w.x = x }
func (w *Checkbox) SetY(y int)        { w.y = y }
func (w *Checkbox) GetX() int         { return w.x }
func (w *Checkbox) GetY() int         { return w.y }
func (w *Checkbox) GetPos() Coord     { return Coord{X: w.x, Y: w.y} }
func (w *Checkbox) SetPos(c Coord)    { w.x, w.y = c.X, c.Y }
func (w *Checkbox) SetW(x int)        { w.w = x }
func (w *Checkbox) SetH(y int)        { w.h = y }
func (w *Checkbox) GetW() int         { return w.w }
func (w *Checkbox) GetH() int         { return w.y }
func (w *Checkbox) WantW() int {
	return len(fmt.Sprintf("[%s] %s", string(w.state), w.label))
}
func (w *Checkbox) WantH() int         { return 1 }
func (w *Checkbox) SetSize(c Coord)    { w.w, w.h = c.X, c.Y }
func (w *Checkbox) Focusable() bool    { return true }
func (w *Checkbox) SetTabbable(b bool) { w.tabbable = b }
func (w *Checkbox) Tabbable() bool     { return w.tabbable }
func (w *Checkbox) MinW() int {
	return len(fmt.Sprintf("[%s] %s", string(w.state), w.label))
}
func (w *Checkbox) MinH() int { return 1 }

func (w *Checkbox) SetLabel(l string) { w.label = l }
func (w *Checkbox) SetChecked(v bool) {
	if v {
		w.state = CHECKBOX_ON
	} else {
		w.state = CHECKBOX_OFF
	}
}
func (w *Checkbox) IsChecked() bool { return w.state == CHECKBOX_ON }
func (w *Checkbox) SetToMaybe()     { w.state = CHECKBOX_MAYBE }
func (w *Checkbox) SetStateRunes(runes []rune) {
	for i := range runes {
		if i > 2 {
			return
		}
		w.stateRunes[i] = runes[i]
	}
}
