/*
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 main

import (
	"fmt"

	w "git.bullercodeworks.com/brian/tcell-widgets"
	"github.com/gdamore/tcell"
)

type UiScreen struct {
	ui   *Ui
	w, h int

	widget w.Widget
	log    *w.Cli

	widgets []w.Widget
	cursor  int
}

func (s *UiScreen) Init(ui *Ui) {
	// Set up our log viewer
	s.ui = ui
	s.log = w.NewCli("log", ui.style)

	ll := w.NewLinearLayout("test", ui.style)
	ll.SetTabbable(true)
	ll.SetLogger(s.log.Log)

	testList := w.NewList("test-list", ui.style)
	for i := 0; i < 10; i++ {
		testList.Add(fmt.Sprintf("Item %d", i))
	}
	testList.SetLogger(s.log.Log)
	ll.Add(testList)

	btnL := w.NewLinearLayout("btn-ll", ui.style)
	btnL.SetTabbable(true)
	btnL.SetLogger(s.log.Log)
	btnL.SetOrientation(w.LinLayH)

	btnCancel := w.NewButton("btn-cancel", ui.style)
	btnCancel.SetLabel("Cancel")
	btnL.Add(btnCancel)
	btnL.AddFlag(btnCancel, w.LFAlignVBottom)

	btnOk := w.NewButton("btn-ok", ui.style)
	btnOk.SetLabel("Ok")
	btnL.Add(btnOk)
	btnL.AddFlag(btnOk, w.LFAlignVBottom)
	ll.Add(btnL)
	ll.AddFlag(btnL, w.LFAlignVBottom)

	ml := w.NewTopMenuLayout("menu", ui.style)
	m := ml.Menu()
	m.SetLabel("Widget Example")
	ml.AddMenuItems(
		m.CreateMenuItem("File", nil,
			m.CreateMenuItem("Exit", func() bool {
				s.ui.stop()
				return true
			}),
		),
	)
	ml.SetLogger(s.log.Log)
	ml.SetWidget(ll)
	ml.SetActive(true)
	s.widget = ml
	/*
		dw := w.NewDebugWidget("debug", ui.style)
		dw.SetWidget(ll)
		dw.SetSize(w.Coord{X: 20, Y: 30})
		dw.SetActive(true)
		s.widget = dw
	*/
	ll.Add(s.log)

	// s.widgets = append(s.widgets, ml)
	// s.widgets = append(s.widgets, s.log)
}

func (s *UiScreen) HandleResize(ev *tcell.EventResize) {
	s.w, s.h = ev.Size()
	s.widget.HandleResize(ev)
	lgH := 20
	s.log.SetPos(w.Coord{X: 0, Y: s.h - lgH - 1})
	s.log.HandleResize(w.Coord{X: s.w, Y: lgH}.ResizeEvent())
}

func (s *UiScreen) HandleKey(ev *tcell.EventKey) bool {
	if ev.Key() == tcell.KeyCtrlJ {
		// Ctrl+J is the keypad 'Enter'
		ev = tcell.NewEventKey(tcell.KeyEnter, 0, 0)
	}
	return s.widget.HandleKey(ev)
}
func (s *UiScreen) HandleTime(ev *tcell.EventTime) {}
func (s *UiScreen) Draw() {
	s.widget.Draw(s.ui.tScreen)
	// s.log.Draw(s.ui.tScreen)
}
func (s *UiScreen) Log(txt string, args ...any) {}

func (s *UiScreen) Exit() {}
