package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"

	"MobileMkch/api"
	"MobileMkch/settings"
	"MobileMkch/ui"
)

func main() {
	a := app.NewWithID("com.mkch.mobile")
	a.SetIcon(theme.ComputerIcon())

	settings.SetApp(a)

	MobileOptimizations(a)

	w := a.NewWindow("MobileMkch")
	w.Resize(fyne.NewSize(400, 700))

	apiClient := api.NewClient()

	appSettings, err := settings.Load()
	if err == nil {
		if appSettings.Key != "" {
			if err := apiClient.Authenticate(appSettings.Key); err != nil {
				fmt.Printf("Ошибка аутентификации по ключу: %v\n", err)
			} else {
				fmt.Println("Аутентификация по ключу успешна")
			}
		}
		
		if appSettings.Passcode != "" {
			if err := apiClient.LoginWithPasscode(appSettings.Passcode); err != nil {
				fmt.Printf("Ошибка входа с passcode: %v\n", err)
			} else {
				fmt.Println("Вход с passcode успешен")
			}
		}
	}

	uiManager := ui.NewUIManager(a, w, apiClient)

	w.SetContent(uiManager.GetMainContent())

	w.ShowAndRun()
}

const Version = "1.0.0"

func showAbout() *widget.PopUp {
	content := container.NewVBox(
		widget.NewLabel("MobileMkch"),
		widget.NewLabel(fmt.Sprintf("Версия: %s", Version)),
		widget.NewLabel("Мобильный клиент для mkch.pooziqo.xyz"),
		widget.NewLabel(""),
		widget.NewLabel("Разработано с ❤️ на Go + Fyne"),
	)

	return widget.NewModalPopUp(content, nil)
}
