package main

import "log"
import "reflect"
import "os"

import "github.com/gookit/config"
import "github.com/andlabs/ui"
import "github.com/davecgh/go-spew/spew"

import "git.wit.com/jcarr/shell"

var mainwin *ui.Window
var mydrive string

func rsync(mybut *ui.Button) {
	log.Println("rsync() mybut =", reflect.ValueOf(mybut).Elem())

	if _, err := os.Stat("/mnt/sdcard/lost+found/"); !os.IsNotExist(err) {
		log.Println("run rsync here")
		script(`
			rsync -av --progress --inplace /home/pinebook/factory/FACTORY-IMAGER-ROOTFS/ /mnt/sdcard/
			umount /mnt/sdcard
		`)
		ui.MsgBox(mainwin, "The sdcard is finished", "")
	} else {
		log.Println("partition is not mounted")
		ui.MsgBoxError(mainwin, "The partition is not mounted at /mnt/sdcard", "")
	}
}

func dd(mybut *ui.Button) int {
	if _, err := os.Stat("/mnt/factory-image/lost+found/"); os.IsNotExist(err) {
		shell.Run("mkdir /mnt/factory-image/")
		shell.Run("mount /dev/sdb2 /mnt/factory-image/")
	}
	if _, err := os.Stat("/mnt/factory-image/lost+found/"); os.IsNotExist(err) {
		log.Println("partition is not mounted")
		ui.MsgBoxError(mainwin, "The partition is not mounted at /mnt/sdcard", "")
		return -1
	}


	script("dd if=/home/pinebook/factory/factory-pine14inch of=/dev/sdb status=progress bs=1M oflag=sync count=300");

		log.Println("run rsync here")
		script(`
			rsync -av --progress --inplace /home/pinebook/factory/FACTORY-IMAGER-ROOTFS/ /mnt/sdcard/
			umount /mnt/sdcard
		`)
		ui.MsgBox(mainwin, "The sdcard is finished", "")
	} else {
		log.Println("partition is not mounted")
		ui.MsgBoxError(mainwin, "The partition is not mounted at /mnt/sdcard", "")
	}
	return 0
}

// select the dev entry to partition and format
// for example: /dev/mtdblock0 or /dev/sdc
func selectDrive(mybox *ui.Combobox) {
	spew.Dump(mybox.Visible())
	spew.Dump(mybox.Selected())
	spew.Dump(mybox.ControlBase)
	// spew.Dump(box.ControlBase.Parent)
	spew.Dump(mybox)
	log.Println("selected = ", mybox.Selected())
	if (mybox.Selected() == 1) {
		mydrive = "/dev/sdb"
	}
}

func format(mybut *ui.Button) {
	log.Println("format() mybut =", reflect.ValueOf(mybut).Elem())
	log.Println("format and mount here")

	script(`
		parted -s /dev/sdb mklabel msdos
		parted -s /dev/sdb mkpart primary ext4 1MiB 8GB
		parted -s /dev/sdb mkpart primary ext4 8GB 32GB
		sleep 1
		mkfs.ext4 /dev/sdb1
		mkfs.ext4 /dev/sdb2
		e2label /dev/sdb1 root
		e2label /dev/sdb2 factory-image
		mount /dev/sdb1 /mnt/sdcard/
		pwd
		dd if=../u-boot/u-boot-sunxi-with-spl.bin of=/dev/sdb status=progress oflag=sync bs=1K seek=8
	`)
}

func addButton(vbox *ui.Box, label string, click func(*ui.Button)) {
	hbox := ui.NewHorizontalBox()
	hbox.SetPadded(true)

	// look up translation here for 'label'
	button := ui.NewButton(label)
	hbox.Append(button, false)
	button.OnClicked(click)

	vbox.Append(hbox, false)
}

func addGroupBox(hbox *ui.Box, name string) *ui.Box {
	groupDrive := ui.NewGroup(name)
	groupDrive.SetMargined(true)

	vbox := ui.NewVerticalBox()
	vbox.SetPadded(true)

	groupDrive.SetChild(vbox)
	hbox.Append(groupDrive, true)

	log.Println("groupDrive =", reflect.TypeOf(groupDrive))

	vbox.SetPadded(true)
	return vbox
}

func makeBurnSDcardPage() *ui.Box {
	hbox := ui.NewVerticalBox()
	hbox.SetPadded(true)

	vbox := addGroupBox(hbox, "Drive to Format")

	cbox := ui.NewCombobox()
	cbox.Append("/dev/sdb")
	cbox.Append("/dev/sr0")
	cbox.Append("Combobox Item 3")
	cbox.OnSelected(selectDrive)
	vbox.Append(cbox, false)

	vbox2 := addGroupBox(hbox, "Select the Image")

	addButton(vbox2, "Partition and Format", format)
	addButton(vbox2, "rsync filesystem", rsync)
	addButton(vbox2, "dd image", dd)

	return hbox
}

func setupUI() {
	mainwin = ui.NewWindow("Make a Pinebook SDCARD", config.Int("width"), config.Int("height"), false)
	mainwin.OnClosing(func(*ui.Window) bool {
		ui.Quit()
		return true
	})
	ui.OnShouldQuit(func() bool {
		mainwin.Destroy()
		return true
	})

	tab := ui.NewTab()
	mainwin.SetChild(tab)
	mainwin.SetMargined(true)

	tab.Append("Burn SDCARD", makeBurnSDcardPage())
	tab.SetMargined(0, true)

	mainwin.Show()
}

func main() {
	parseConfig()

	ui.Main(setupUI)
}
