package main

import (
	"log"
	"os"
	"time"

	"lovelyui/apps"
)

// browserShots: the browser app inside the real shell with the real
// keyboard app; the fake engine draws the pages.
func browserShots(out, appDir, bin string) {
	os.Setenv("LOVELY_WEBENGINE", "fake")
	r := newRig(appDir, bin)
	defer r.close()
	var app apps.App
	for _, a := range apps.Load([]string{appDir}) {
		if a.AppID() == "browser" {
			app = a
		}
	}
	if app.Exec == nil {
		log.Fatal("no browser.desktop in " + appDir)
	}
	r.on(func() { r.sh.Launch(app) })
	if !r.appInFront(1) {
		log.Fatal("the browser did not come up")
	}
	time.Sleep(1500 * time.Millisecond)
	r.save(out, "live-browser-01-page")
	// the address bar: the address page with the keyboard
	r.disp.Tap(300, 1440-96-40)
	r.wait("the keyboard", func() bool { return r.sh.KeyboardHeight() > 0 })
	r.save(out, "live-browser-02-address")
	r.on(r.sh.Back) // hides the keyboard
	r.wait("the keyboard to go", func() bool { return r.sh.KeyboardHeight() == 0 })
	r.on(r.sh.Back) // leaves the address page
	r.disp.Quiet(300*time.Millisecond, 3*time.Second)
	// a text field of the page: the engine asks for the keyboard
	r.disp.Tap(360, 50+230)
	r.wait("the keyboard for the page's field", func() bool { return r.sh.KeyboardHeight() > 0 })
	r.save(out, "live-browser-03-page-field")
	r.on(r.sh.Back)
	r.wait("the keyboard to go", func() bool { return r.sh.KeyboardHeight() == 0 })
	// the menu
	r.disp.Tap(700, 1440-96-40)
	r.disp.Quiet(300*time.Millisecond, 3*time.Second)
	r.save(out, "live-browser-04-menu")
	// home: the app is paused; back to it
	r.on(r.sh.Home)
	r.disp.Quiet(300*time.Millisecond, 3*time.Second)
	r.on(func() { r.sh.Launch(app) })
	r.disp.Quiet(300*time.Millisecond, 3*time.Second)
	r.save(out, "live-browser-05-resumed")
	processes()
}
