package main

import (
	"fmt"
	"log"
	"net/url"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/cmd/fyne_settings/settings"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

var Version string
var UseWake bool                     // WOL
var TvStatus string = "Disconnected" // Need refresh
var TvIp string
var TvMac string // Used for WOL

func setup_prefs(pref fyne.Preferences) {
	Version = pref.StringWithFallback("version", "")
	if Version == "" {
		pref.SetString("version", "1.0-dev")
		Version = "1.0-dev"
	}
	UseWake = pref.BoolWithFallback("wake_on_lan", false)
}

// Depreciated as mobile users will use the settings button rather than the menu
/*func make_mainMnu(a fyne.App, w fyne.Window) *fyne.MainMenu {
	var settingsItem *fyne.MenuItem = fyne.NewMenuItem("Settings", func() {
		var w2 fyne.Window = a.NewWindow("myRoku Settings")
		w2.SetContent(settings.NewSettings().LoadAppearanceScreen(w2))
		w2.CenterOnScreen()
		w2.Resize(fyne.NewSize(480, 480))
		w2.Show()
	})
	var mnu *fyne.MainMenu = fyne.NewMainMenu(fyne.NewMenu("File", settingsItem, fyne.NewMenuItemSeparator()))
	return mnu
}*/

func main() {
	var a fyne.App = app.NewWithID("my-roku")
	var pref fyne.Preferences = a.Preferences()
	setup_prefs(pref)
	log.Printf("myRoku v%s wol=%t", Version, UseWake)
	var home fyne.Window = a.NewWindow("myRoku")
	/*if a.Driver().Device().HasKeyboard() { // Desktop only
		home.SetMainMenu(make_mainMnu(a, home))
	} */
	home.CenterOnScreen()
	home.Resize(fyne.Size{Width: 400, Height: 600})
	home.SetContent(container.NewVBox(
		container.NewHBox(
			widget.NewLabel(fmt.Sprintf("myRoku - v%s", Version)),
			widget.NewButtonWithIcon("", theme.SettingsIcon(), func() {
				var w2 fyne.Window = a.NewWindow("myRoku Settings")
				w2.SetContent(container.NewVBox(
					settings.NewSettings().LoadAppearanceScreen(w2),
					widget.NewButton("Quit", func() {
						w2.Close()
					}),
				))
				w2.CenterOnScreen()
				w2.Resize(fyne.NewSize(480, 480))
				w2.Show()
			}),
		),
		container.NewHBox(
			widget.NewLabel(TvStatus),
			widget.NewButtonWithIcon("", theme.MediaReplayIcon(), func() {
				if UseWake {
					log.Println("Requested refresh of status")
				} else {
					log.Println("WOL disabled")
				}
			}),
			widget.NewButtonWithIcon("Connections", theme.ComputerIcon(), func() {
				var w2 fyne.Window = a.NewWindow("myRoku Connections")
				var ip_entry *widget.Entry = widget.NewEntry()
				ip_entry.SetText(TvIp)
				ip_entry.OnSubmitted = func(s string) {
					if TvIp != s { // It's the same thing!?!
						log.Printf("TvIp='%s' Now '%s'", TvIp, s)
						TvIp = s
						ip_entry.Refresh()
					}
					ip_entry.FocusLost() // Lose focus so the user can enter the next field
				}
				var mac_entry *widget.Entry = widget.NewEntry()
				mac_entry.SetText(TvMac)
				mac_entry.OnSubmitted = func(s string) {
					if TvMac != s { // It's the same thing!?!
						log.Printf("TvMac='%s' Now '%s'", TvMac, s)
						TvMac = s
						mac_entry.Refresh()
					}
					mac_entry.FocusLost() // Lose focus so the user can enter the next field
				}
				w2.SetContent(container.NewVBox(
					container.NewHBox(
						widget.NewLabel("Tv Ip Address"),
						widget.NewButtonWithIcon("", theme.HelpIcon(), func() {
							var u *url.URL
							var err error
							u, err = url.Parse("https://en.wikipedia.org/wiki/IP_address")
							if err != nil {
								log.Println("Failed sending the user to Wiki=IP_address", err)
								return
							}
							a.OpenURL(u)
						}),
					),
					ip_entry,
					container.NewHBox(
						widget.NewLabel("Tv Mac Address"),
						widget.NewButtonWithIcon("", theme.HelpIcon(), func() {
							var u *url.URL
							var err error
							u, err = url.Parse("https://en.wikipedia.org/wiki/MAC_address")
							if err != nil {
								log.Println("Failed sending the user to Wiki=MAC_address", err)
								return
							}
							a.OpenURL(u)
						}),
					),
					mac_entry,
					widget.NewButtonWithIcon("Back", theme.NavigateBackIcon(), func() {
						w2.Close()
					}),
				))
				w2.CenterOnScreen()
				w2.Resize(fyne.NewSize(480, 480))
				w2.Show()
			}),
		),
		widget.NewButton("Debug", func() {
			log.Printf("myRoku v%s wol=%t", Version, UseWake)
			log.Printf("TvStatus='%s' TvIp='%s' TvMac='%s'", TvStatus, TvIp, TvMac)
			log.Printf("driver=(mobile:%t, desktop:%t, browser:%t)", a.Driver().Device().IsMobile(), a.Driver().Device().HasKeyboard(), a.Driver().Device().IsBrowser())
		}),
	))
	home.Show()
	a.Run()
}
