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

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

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

    function start() {
        kanaVoicingAnswer.focus = true
        roundNum = 0
        randomKnownKana()
    }

    KanaData {
        id: kanaData
    }

    property var currentKana: []

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

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

    function endRounds() {
        kanaVoicingAnswer.focus = false
        jLearnerScope.focus = true
        console.log("Switching back")
        parent.switchResult(correct/maxRoundNum, "KanaVoicing", pastCorrectness)
        pastKana = []
        pastCorrectness = []
        correct = 0
        kanaVoicingAnswer.placeholderText = "Type Voicing..."
        kanaVoicingAnswerBackground.color = kanaVoicingAnswer.activeFocus ? Colors.tertiary_fg : Colors.primary_fg
    }

    function randomKana() {
        if (roundNum >= maxRoundNum) {
            endRounds()
        }
        var list = kanaData.kana
        currentKana = list[Math.floor(Math.random() * list.length)]
        if (knownList.length > maxRoundNum) {
            while (pastKana.includes(currentKana)) {
                currentKana = list[Math.floor(Math.random() * list.length)]
            }
        }
        pastKana.push(currentKana)
        console.log("Round Num: " + roundNum + " - Choosing Kana: " + currentKana.hira)
        roundNum = roundNum + 1
    }

    function randomKnownKana() {
        if (roundNum >= maxRoundNum) {
            endRounds()
        }
        var knownList = kanaData.kana.filter(k => k.known)
        currentKana = knownList[Math.floor(Math.random() * knownList.length)]
        if (knownList.length > maxRoundNum) {
            while (pastKana.includes(currentKana)) {
                currentKana = knownList[Math.floor(Math.random() * knownList.length)]
            }
        }
        pastKana.push(currentKana)
        console.log("Round Num: " + roundNum + " - Choosing Kana: " + currentKana.hira)
        roundNum = roundNum + 1
    }

    Column {
        anchors.fill: parent
        spacing: 10

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

            Text {
                id: kanaVoicingText
                text: "Im Spiel - Runde: " + kanaVoicing.roundNum
                anchors.centerIn: parent
                font.pixelSize: parent.height * 0.6
                color: Colors.primary
            }
        }

        Rectangle {
            id: kanaVoicingKanaRectangle
            color: Colors.primaryContainer
            height: (parent.height - kanaVoicingTextRectangale.height)*0.75
            width: height
            anchors.horizontalCenter: parent.horizontalCenter
                    
            radius: 10
            border.width: 2
            border.color: Colors.primaryContainer_fg

            Text {
                id: kanaVoicingKana
                text: typeof kanaVoicing.currentKana.hira === "undefined" ? "" : kanaVoicing.currentKana.hira
                anchors.centerIn: parent
                font.pixelSize: Math.min(parent.width / text.length * 1.2, parent.height * 0.8)
                color: Colors.primary
            }
        }

        TextField {
            id: kanaVoicingAnswer

            placeholderText: "Type Voicing..."

            anchors.horizontalCenter: parent.horizontalCenter

            width: parent.width/2
            height: (parent.height - kanaVoicingTextRectangale.height)*0.25 - parent.spacing*2

            font.pixelSize: height * 0.8

            color: Colors.secondary_fg
            placeholderTextColor: Colors.secondary

            background: Rectangle {
                id: kanaVoicingAnswerBackground
                radius: 10
                color: kanaVoicingAnswer.activeFocus ? Colors.secondaryContainer : Colors.secondaryContainer_fg
                border.color: kanaVoicingAnswer.activeFocus ? Colors.secondaryContainer_fg : Colors.secondaryContainer
                border.width: 1
            }

            Keys.onReleased: function(event) {
                if (event.key === Qt.Key_Escape) {
                    console.log("Escape pressed inside TextField")
                    console.log("Pressed Escape, closing JLearner")
                    jLearnerLoader.active = false
                }
            }

            onAccepted: function(event) {
                console.log("Enter pressed inside TextField")
                if (kanaVoicing.currentKana.voice.includes(kanaVoicingAnswer.text.toLowerCase())){
                    console.log("Correct")
                    kanaVoicingAnswer.text = ""
                    kanaVoicingAnswer.placeholderText = "[" + kanaVoicing.currentKana.voice + "]"
                    kanaVoicingAnswerBackground.color = "green"
                    kanaVoicing.correct = kanaVoicing.correct + 1
                    pastCorrectness.push({
                        kana: kanaVoicing.currentKana.hira,
                        correctness: true,
                        answer: kanaVoicingAnswer.text.toLowerCase(),
                        correct: kanaVoicing.currentKana.voice.join("|")
                    })
                } else {
                    console.log("Wrong: [" + currentKana.voice + "]")
                    kanaVoicingAnswer.text = ""
                    kanaVoicingAnswerBackground.color = "red"
                    kanaVoicingAnswer.placeholderText = "[" + kanaVoicing.currentKana.voice + "]"
                    pastCorrectness.push({
                        kana: kanaVoicing.currentKana.hira,
                        correctness: false,
                        answer: kanaVoicingAnswer.text.toLowerCase(),
                        correct: kanaVoicing.currentKana.voice.join("|")
                    })
                }
                randomKnownKana()
            }
        }
    }
}