package main

import (
	"image/color"
	"log"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	//"fyne.io/fyne/v2/layout"
)

var myApp fyne.App
var winMain fyne.Window
var botLogin string
var botPass string
var boxLeft *fyne.Container

func main() {
	myApp = app.New()
	winMain = myApp.NewWindow("[Бото-ферма WarTank]")
	winMain.Resize(fyne.NewSize(800, 600))
	green := color.NRGBA{R: 0, G: 180, B: 0, A: 255}

	text1 := canvas.NewText("Список ботов", green)
	text2 := canvas.NewText("There", green)
	text2.Move(fyne.NewPos(20, 20))
	//content := container.NewWithoutLayout(text1, text2)
	// content := container.New(layout.NewGridLayout(2), text1, text2)
	boxLeft = container.NewVBox(text1, text2)
	btnAddBot := widget.NewButton("Добавить бота", btnAddClick)
	btnExit := widget.NewButton("Выход", btnExitClick)
	boxDown := container.NewHBox(btnAddBot, layout.NewSpacer(), btnExit)
	boxBorder := container.NewBorder(nil, nil, boxLeft, nil, boxLeft)
	boxMain := container.NewBorder(nil, boxDown, nil, nil, boxBorder)

	winMain.SetContent(boxMain)
	winMain.Show()
	myApp.Run()
}

func btnAddClick() {
	log.Println("btnAddclick()")
	winAddBot := myApp.NewWindow("Новый бот")
	entLogin := widget.NewEntry()
	entPass := widget.NewEntry()
	form := &widget.Form{
		Items: []*widget.FormItem{ // we can specify items in the constructor
			{Text: "Логин", Widget: entLogin},
			{Text: "Пароль", Widget: entPass},
		},
		OnSubmit: func() { // optional, handle form submission
			botLogin = entLogin.Text
			log.Println("Form submitted:", entLogin.Text)
			botPass = entPass.Text
			log.Println("multiline:", entPass.Text)
			winAddBot.Close()
			btnBot := widget.NewButton(botLogin, btnBotClick(botLogin, botPass))
			boxLeft.Add(btnBot)
			// winMain.Canvas().Refresh()
		},
	}
	winAddBot.Resize(fyne.NewSize(640, 480))
	winAddBot.SetContent(form)
	winAddBot.Show()

}

func btnBotClick(botName, botPass string) func() {
	_botName := botName
	_botPass := botPass
	return func() {
		log.Printf("btnBotClick().fn(): botName=%q\tbotPass=%q\n", _botName, _botPass)
	}
}

func btnExitClick() {
	log.Println("btnExitClick()")
	myApp.Quit()
}
