package main

import (
	"fmt"

	"code.rocketnine.space/tslocum/cview"
	"git.saintnet.tech/tomecraft/tome_lib"
	"github.com/gdamore/tcell/v2"
)

type UIContainer struct {
	State     *tome_lib.GameView
	Updated   chan bool
	Output    chan string
	Input     chan string
	GetUpdate chan bool
}

func main() {
	app := cview.NewApplication()
	app.EnableMouse(true)
	container := &UIContainer{
		State:     nil,
		Updated:   make(chan bool),
		Output:    make(chan string),
		Input:     make(chan string),
		GetUpdate: make(chan bool),
	}

	grid := cview.NewGrid()
	grid.SetRows(3, 0, 0, 3, 3)
	grid.SetColumns(30, 0, 30)
	grid.SetBorders(true)

	board := cview.NewTable()
	board.SetBorders(true)
	hand := cview.NewTable()
	hand.SetBorders(true)
	playerside := cview.NewTextView()
	playerside.SetText("Game has not started")
	playerside.SetTextAlign(cview.AlignCenter)
	commandinput := cview.NewInputField()
	commandoutput := cview.NewTextView()
	commandinput.SetDoneFunc(func(key tcell.Key) {
		container.Input <- commandinput.GetText()
		commandinput.SetText("")
	})
	commandoutput.SetChangedFunc(func() {
		app.Draw()
	})
	playerside.SetChangedFunc(func() {
		app.Draw()
	})

	go func() {
		for {
			select {
			case <-container.Updated:
				playerside.Clear()
				var pv string
				if container.State != nil && container.State.Player != nil {
					ct := "Noone"
					if container.State.CurrentTurn == tome_lib.SentinalID {
						ct = "Sentinal"
					} else if container.State.CurrentTurn == tome_lib.ScourgeID {
						ct = "Scourge"
					}
					pv = fmt.Sprintf("Your Life: %v\nEnemy Life: %v\nEnemy Hand size: %v\nEnemy Deck Size: %v\n\nCT:%v CD: %v, HD %v, Status: %v\n", container.State.Player.Life, container.State.EnemyLife, container.State.EnemyHandSize, container.State.EnemyDeckSize, ct, container.State.CanDraw, container.State.HasDrawn, container.State.Status)
				} else {
					pv = fmt.Sprintf("%v", "Awaiting player info")
				}
				fmt.Fprintf(playerside, "%v", pv)

				for c := 0; c < 4; c++ {
					color := tcell.ColorWhite.TrueColor()
					be := "_"
					if container.State != nil && container.State.Board != nil {
						be = container.State.Board.Sentinal[c].String()
					}
					cell := cview.NewTableCell(be)
					cell.SetTextColor(color)
					cell.SetAlign(cview.AlignCenter)
					board.SetCell(0, c, cell)
				}
				for c := 0; c < 4; c++ {
					color := tcell.ColorWhite.TrueColor()
					be := "_"
					if container.State != nil && container.State.Board != nil {
						be = container.State.Board.Scourge[c].String()
					}
					cell := cview.NewTableCell(be)
					cell.SetTextColor(color)
					cell.SetAlign(cview.AlignCenter)
					board.SetCell(1, c, cell)
				}
				cols := 0
				if container.State != nil && container.State.Player != nil && container.State.Player.Hand != nil {
					cols = len(container.State.Player.Hand)
				}
				hand.Clear()
				for c := 0; c < cols; c++ {
					color := tcell.ColorWhite.TrueColor()
					ce := "_"
					if container.State != nil && container.State.Player != nil && container.State.Player.Hand != nil {
						ce = container.State.Player.Hand[c].String()
					}
					cell := cview.NewTableCell(ce)
					cell.SetTextColor(color)
					cell.SetAlign(cview.AlignCenter)
					hand.SetCell(0, c, cell)
				}
				app.Draw()
			case o := <-container.Output:
				commandoutput.Clear()
				fmt.Fprintf(commandoutput, "Result: %v", o)
			}
		}
	}()

	grid.AddItem(playerside, 1, 0, 2, 1, 0, 100, false)
	grid.AddItem(board, 1, 1, 1, 1, 0, 100, false)
	grid.AddItem(hand, 2, 1, 1, 1, 0, 100, false)
	grid.AddItem(commandinput, 3, 0, 1, 3, 0, 0, true)
	grid.AddItem(commandoutput, 4, 0, 1, 3, 0, 0, false)
	container.Updated <- true
	go backend(container)
	app.SetRoot(grid, true)
	if err := app.Run(); err != nil {
		panic(err)
	}
}
