package main

import "fmt"
import "os"
import "io/ioutil"
import "log"
import "time"

import "git.wit.org/wit/gui"
import "github.com/davecgh/go-spew/spew"
import "github.com/getlantern/systray"
// "github.com/skratchdot/open-golang/open"

func systrayMain() {
	onExit := func() {
		fmt.Println("Starting onExit")

		now := time.Now()
		ioutil.WriteFile(fmt.Sprintf(`/tmp/on_exit_%d.txt`, now.UnixNano()), []byte(now.String()), 0644)
		now = time.Now()
		ioutil.WriteFile(fmt.Sprintf(`/tmp/on_exit_%d.2.txt`, now.UnixNano()), []byte(now.String()), 0644)

		fmt.Println("Finished onExit")
	}
	// Should be called at the very beginning of main().
	systray.Run(onReady, onExit)
}

var mHide *systray.MenuItem
var mShow *systray.MenuItem
var mChecked *systray.MenuItem

func onReady() {
	systray.SetIcon(myIcon)
	systray.SetTitle("Awesome App")
	systray.SetTooltip("Lantern")
	mDump := systray.AddMenuItem("Debug Window", "Debug Window")
	/*
		mQuitOrig := systray.AddMenuItem("Quit", "Quit the whole app")
		go func() {
			<-mQuitOrig.ClickedCh
			fmt.Println("Requesting quit")
			systray.Quit()
			fmt.Println("Finished quitting")
		}()
	*/

	var c = 0
	// We can manipulate the systray in other goroutines
	go func() {
		systray.SetIcon(myIcon)
		systray.SetTitle("Awesome App")
		systray.SetTooltip("Pretty awesome棒棒嗒")

		mShow = systray.AddMenuItem("Show 640x480", "Show 480")
		mHide = systray.AddMenuItem("Hide Window", "Hide Window")

		mChange := systray.AddMenuItem("Change Me", "Change Me")
		mChecked = systray.AddMenuItem("Unchecked", "Check Me")
		mEnabled := systray.AddMenuItem("Enabled", "Enabled")
		mUrl := systray.AddMenuItem("Open Lantern.org", "my home")
		// mQuit := systray.AddMenuItem("退出", "Quit the whole app")
		mQuit := systray.AddMenuItem("Quit", "Quit the whole app")

		// Sets the icon of a menu item. Only available on Mac.
		mQuit.SetIcon(myIcon)

		systray.AddSeparator()
		mToggle := systray.AddMenuItem("Toggle", "Toggle the Quit button")
		shown := true
		// var blah struct{int}
		// blah := make(chan struct{})
		for {
			select {
			case <-mChange.ClickedCh:
				log.Println("START")
				log.Println("END")
				mChange.SetTitle("I've Changed")
			case <-mChecked.ClickedCh:
				log.Println("CHECKED START")
				if mChecked.Checked() {
					mChecked.Uncheck()
					mChecked.SetTitle("Unchecked")
					log.Println("UNCHECKED")
				} else {
					mChecked.Check()
					mChecked.SetTitle("Checked")
					log.Println("CHECKED")
				}
				spew.Dump(mChecked.ClickedCh)
				log.Println("CHECKED END")
			case <-mDump.ClickedCh:
				log.Println("START")
				gui.Config.Width = 800
				gui.Config.Height = 300
				gui.Queue(gui.DebugWindow)
				log.Println("END")
			case <-mShow.ClickedCh:
				log.Println("START Show()")
				gui.Config.Width = 640 + c*20
				c = c + 1
				gui.Config.Height = 480
				gui.Queue(gui.DebugWindow)
				log.Println("END")
			case <-mHide.ClickedCh:
				log.Println("START Hide()")
				/*
				window := gui.Data.WindowMap["splash"]
				if window != nil {
					box := window.BoxMap["splash"]
					box.Window.UiTab.Hide()
				}
				*/
				log.Println("END")
			case <-mEnabled.ClickedCh:
				log.Println("START")
				mEnabled.SetTitle("Disabled")
				mEnabled.Disable()
				log.Println("END")
			case <-mUrl.ClickedCh:
				log.Println("START")
				// open.Run("https://www.getlantern.org")
				log.Println("END")
			case <-mToggle.ClickedCh:
				log.Println("START")
				if shown {
					// mQuitOrig.Hide()
					mEnabled.Hide()
					shown = false
				} else {
					// mQuitOrig.Show()
					mEnabled.Show()
					shown = true
				}
				log.Println("END")
				// os.Exit(0)
			case <-mQuit.ClickedCh:
				log.Println("START")
				log.Println("Systray Quit now...")
				systray.Quit()
				log.Println("END")
				log.Println("os.Exit() now...")
				os.Exit(0)
				return
			}
		}
	}()
}
