package main

import (
	"fmt"
	"os"

	"github.com/jorenchik/mdemory/src/compiler/api"
	"github.com/therecipe/qt/core"
	"github.com/therecipe/qt/widgets"
)

var workingPath string = "/home/jorenchik/Code/mdemory/memorybase"

func main() {
	// InitApp 
	app       := widgets.NewQApplication(len(os.Args), os.Args)
	window    := widgets.NewQMainWindow(nil, 0)
	hSplitter := widgets.NewQSplitter(nil)
	window.SetWindowTitle("MDemory")
	window.SetMinimumSize2(400, 300)

	leftWidget := widgets.NewQWidget(nil, 0)
	leftLayout := widgets.NewQVBoxLayout()
	mdemLabel  := widgets.NewQLabel2("Mdems", nil, 0)
	model      := widgets.NewQFileSystemModel(nil)
	mdemList   := widgets.NewQTreeView(nil)
	leftWidget.SetLayout(leftLayout)
	leftLayout.AddWidget(mdemLabel, 0, 0)
    model.SetRootPath(workingPath)
	mdemList.SetModel(model)

    rootIndex := model.Index2("/home/jorenchik/Code/mdemory/memorybase", 0)
    fmt.Printf("Root index: %s\n", model.FileName(rootIndex))
    mdemList.SetRootIndex(rootIndex)

    mdemList.HideColumn(1)
    mdemList.HideColumn(2)
    mdemList.HideColumn(3)
	mdemList.ConnectDoubleClicked(func(index *core.QModelIndex) {
        if model.IsDir(index) {
            return
        }
        filePath := model.FilePath(index)
        fileContents, err := os.ReadFile(filePath)
        if (err != nil) {
            widgets.QMessageBox_Critical(
                nil,
                "Compilation error",
                err.Error(),
                widgets.QMessageBox__Ok,
                widgets.QMessageBox__Ok,
            )
            return
        }
        _, err = api.Compile(string(fileContents))
        if (err != nil) {
            widgets.QMessageBox_Critical(
                nil,
                "Compilation error",
                err.Error(),
                widgets.QMessageBox__Ok,
                widgets.QMessageBox__Ok,
            )
        }

	})

	deckLabel := widgets.NewQLabel2("Decks", nil, 0)
	deckList  := widgets.NewQListView(nil)
	leftLayout.AddWidget(mdemList, 1, 0)
	leftLayout.AddWidget(deckLabel, 0, 0)
	leftLayout.AddWidget(deckList, 1, 0)

	rightWidget := widgets.NewQWidget(nil, 0)
	rightLayout := widgets.NewQVBoxLayout()
	rightWidget.SetLayout(rightLayout)

	top           := widgets.NewQWidget(nil, 0)
	hTop          := widgets.NewQHBoxLayout()
	deckListLabel := widgets.NewQLabel2("Mdem: file.mdem", nil, 0)
	top.SetLayout(hTop)
	rightLayout.AddWidget(top, 0, 0)

	hTop.AddWidget(deckListLabel, 0, 0)
	hTop.AddStretch(1)
	refresh  := widgets.NewQToolButton(nil)
	practice := widgets.NewQToolButton(nil)
    shuffle  := widgets.NewQToolButton(nil)
	hTop.AddWidget(refresh, 0, 0)
	hTop.AddWidget(shuffle, 0, 0)
	hTop.AddWidget(practice, 0, 0)
	refresh.SetText("Refresh")
	shuffle.SetText("Shuffle")
	practice.SetText("Practice")

	mdemScroll  := widgets.NewQScrollArea(nil)
	hMdemScroll := widgets.NewQVBoxLayout()
	mdemScroll.SetLayout(hMdemScroll)
    var mdem widgets.QWidget_ITF
	{ // CreateMdem

        // DefineMdem
        mdem := widgets.NewQWidget(nil, 0)
		mdem.SetMinimumHeight(60)
		hMdem := widgets.NewQHBoxLayout()
		hMdem.AddStretch(1)
		mdem.SetLayout(hMdem)

        // DefineId
		id := fmt.Sprintf("mdem_%d", 1)
		mdem.SetObjectName(id)
		mdem.SetStyleSheet(fmt.Sprintf(`
            QWidget#%s {
                border: 1px solid gray;
                background: white;
            }
        `, id))

        // AddContent
		mdemText := widgets.NewQLabel2(
			"What is the capital of Latvia?", nil, 0,
		)
		hMdem.AddWidget(mdemText, 0, 0)
		hideButton := widgets.NewQToolButton(nil)
		showAnswer := widgets.NewQToolButton(nil)
		hideButton.SetText("Hide")
		showAnswer.SetText("Show")
		hMdem.AddWidget(hideButton, 0, 0)
		hMdem.AddWidget(showAnswer, 0, 0)
	}
    hMdemScroll.AddWidget(mdem, 0, 0)
	hMdemScroll.AddStretch(1)
	rightLayout.AddWidget(mdemScroll, 1, 0)

    // Pagination 
	pagination  := widgets.NewQWidget(nil, 0)
	hPagination := widgets.NewQHBoxLayout()
	pagination.SetLayout(hPagination)

    // TEMP: mock pagination
    prevButton := widgets.NewQToolButton(nil)
    prevButton.SetText("<")
    hPagination.AddWidget(prevButton, 0, 0)
	for i := 0; i < 5; i++ {
		button := widgets.NewQToolButton(nil)
		button.SetText(fmt.Sprintf("%d", i+1))
		hPagination.AddWidget(button, 0, 0)
	}
    nextButton := widgets.NewQToolButton(nil)
    nextButton.SetText(">")
    hPagination.AddWidget(nextButton, 0, 0)

	hPagination.AddStretch(1)
	rightLayout.AddWidget(pagination, 0, 0)

	hSplitter.AddWidget(leftWidget)
	hSplitter.AddWidget(rightWidget)
    hSplitter.SetStretchFactor(0, 1)
    hSplitter.SetStretchFactor(1, 3)
	window.SetCentralWidget(hSplitter)

	window.Show()
	app.Exec()
}
