import QtQuick
import QtQuick.Controls
import "../../../colors"

Item { // Kana_Try
    id: voicingKana
    width: parent.width
    height: parent.height
    anchors.margins: 15
    visible: false

    // Component.onCompleted: {
    //     randomKnownKana()
    // }

    function start() {
        roundNum = 0
        new_round()
    }

    KanaData {
        id: kanaData
    }

    property int roundNum: 0
    property int correct: 0
    property int maxRoundNum: 10

    property var pastKana: []
    property var pastCorrectness: []

    property var kanaArray: createResults(6)
    property var targetKana: kanaArray[Math.floor(Math.random() * kanaArray.length)]

    function createResults(n) {
        var arr = []
        for (var i = 0; i < n; i++) {
            arr.push({ hira: "あ", voice: ["a"], known: true })
        }
        return arr
    }

    function new_round() {
        roundNum = roundNum + 1
        kanaArray = gemerateKanaArray()
        targetKana = kanaArray[Math.floor(Math.random() * kanaArray.length)]
        console.log("Round Num: " + roundNum + " - Choosing Kana: " + targetKana.hira)
        pastKana.push(targetKana)
    }

    function gemerateKanaArray() {
        var arr = []
        for (var i = 0; i < 6; i++) {
            arr.push(randomKnownKana(arr))
        }
        return arr
    }

    function randomKana(arr) {
        if (roundNum > maxRoundNum) {
            endRounds()
        }
        var list = kanaData.kana
        var newKana = list[Math.floor(Math.random() * list.length)]
        
        var arrExists = Array.isArray(arr)

        if (knownList.length > maxRoundNum + 6) {
            if (arrExists) {
                while (pastKana.includes(newKana) || arr.includes(newKana)) {
                    newKana = list[Math.floor(Math.random() * list.length)]
                }
            } else{
                while (pastKana.includes(newKana)){
                    newKana = list[Math.floor(Math.random() * list.length)]
                }
            }
        } else {
            if (arrExists) {
                while (arr.includes(newKana)) {
                    newKana = list[Math.floor(Math.random() * list.length)]
                }
            }
        }
        kanaArray.push(newKana)
        return newKana
    }

    function randomKnownKana(arr) {
        if (roundNum > maxRoundNum) {
            endRounds()
        }
        var knownList = kanaData.kana.filter(k => k.known)
        var newKana = knownList[Math.floor(Math.random() * knownList.length)]
        
        var arrExists = Array.isArray(arr)

        if (knownList.length > maxRoundNum + 6) {
            if (arrExists) {
                while (pastKana.includes(newKana) || arr.includes(newKana)) {
                    newKana = knownList[Math.floor(Math.random() * knownList.length)]
                }
            } else{
                while (pastKana.includes(newKana)){
                    newKana = knownList[Math.floor(Math.random() * knownList.length)]
                }
            }
        } else {
            if (arrExists) {
                while (arr.includes(newKana)) {
                    newKana = knownList[Math.floor(Math.random() * knownList.length)]
                }
            }
        }
        return newKana
    }

    function endRounds() {
        console.log("Switching back")
        roundNum = 0
        parent.switchResult(correct/maxRoundNum, "VoicingKana", pastCorrectness)
    }

    Column {
        anchors.fill: parent
        spacing: 10

        Rectangle {
            id: voicingKanaTextRectangale
            color: Colors.primaryContainer
            width: mainText.width + 20
            height: parent.height * 0.1
            anchors.horizontalCenter: parent.horizontalCenter
                    
            radius: 10
            border.width: 2
            border.color: Colors.primaryContainer_fg

            Text {
                id: mainText
                text: "Runde: " + roundNum + " - [" + targetKana.voice.join("|") + "]:"
                anchors.centerIn: parent
                font.pixelSize: parent.height * 0.6
                color: Colors.primary
            }
        }

        Rectangle{
            width: parent.width
            height: parent.height - voicingKanaTextRectangale.height - voicingKanaResultRectangale.height - parent.spacing * 2
            color: "transparent"
            Grid {
                id: grid
                anchors.fill: parent
                columns: 3
                spacing: 10

                property int rows: 2

                // calculate cell width and height based on parent size, spacing, and number of columns/rows
                property real cellWidth: (width - (columns - 1) * spacing) / columns
                property real cellHeight: (height - (rows - 1) * spacing) / rows

                Repeater {
                    model: kanaArray
                    MouseArea {
                        hoverEnabled: true
                        cursorShape: Qt.PointingHandCursor
                        width:  grid.cellWidth
                        height: grid.cellHeight

                        onClicked: {
                            console.log("Choose a Kana: " + modelData.hira)
                            if (modelData.hira === targetKana.hira) {
                                console.log("Correct")
                                correct = correct + 1
                                voicingKanaResultRectangale.color = "green"
                            } else {
                                console.log("Wrong: [" + targetKana.hira + "]")
                                voicingKanaResultRectangale.color = "red"
                            }
                            pastCorrectness.push({
                                kana: targetKana.hira,
                                correctness: modelData.hira === targetKana.hira,
                                answer: modelData.hira,
                                correct: targetKana.voice.join("|")
                            })
                            voicingKanaResult.text = "Letzte Runde: " + targetKana.hira + ": " + "[" + targetKana.voice.join("|") + "]"
                            new_round()
                        }

                        Rectangle {
                            width: parent.width
                            height: parent.height
                            color: parent.containsMouse ? Colors.secondaryContainer_fg : Colors.secondaryContainer
                            
                            radius: 10
                            border.width: 2
                            border.color: parent.containsMouse ? Colors.secondaryContainer : Colors.secondaryContainer_fg

                            Text{
                                text: modelData.hira
                                anchors.centerIn: parent
                                anchors.verticalCenterOffset: -parent.height * 0.1
                                font.pixelSize: parent.height * 0.8
                        color: parent.parent.containsMouse ? Colors.secondary_fg : Colors.secondary
                            }
                        }
                    }
                }
            }
        }

        Rectangle {
            id: voicingKanaResultRectangale
            color: Colors.primaryContainer
            width: voicingKanaResult.width + 20
            height: parent.height * 0.1
            anchors.horizontalCenter: parent.horizontalCenter
                    
            radius: 10
            border.width: 2
            border.color: Colors.primaryContainer_fg

            Text {
                id: voicingKanaResult
                text: "Wähle das richtige Zeichen"
                anchors.centerIn: parent
                font.pixelSize: parent.height * 0.6
                color: Colors.primary
            }
        }
    }
}