import QtQuick 1.1
import com.nokia.meego 1.0
import "IMQuery.js" as JsIMQuery


Page {
    id: mainPage
    tools: commonTools
    Rectangle {
        id: rectMain
        color: "#0080e6"
        gradient: Gradient {
            GradientStop {
                position: 0.00;
                color: "#048ef7";
            }
            GradientStop {
                position: 0.26;
                color: "#dbdbdb";
            }
            GradientStop {
                position: 0.62;
                color: "#ffffff";
            }
            GradientStop {
                position: 1.00;
                color: "#c0c0c0";
            }
        }
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        width: mainPage.width
        height: mainPage.height

        SipAttributes {
            id:customSipAttributes
            actionKeyEnabled: true
            actionKeyHighlighted: true
            actionKeyLabel: qsTr("Submit")
        }

        Row {
            id: rowQuery
            anchors.horizontalCenter: parent.horizontalCenter

            Image {
                id: appLogo
                width: 64; height: 64
                source: "qrc:/appLogoWithoutBG"
                MouseArea {
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    anchors.top: parent.top
                    onClicked: Qt.openUrlExternally("http://dadablog.net/HarmattanApps")
                }
            }

            Label {
                id: lblInstruction
                width: 220
                y: 15
                text: qsTr("Specify a Chinese character: ")
                font.pointSize: 20
            }

            TextField {
                id: txtCharacter
                width: 90
                opacity: 0.7
                maximumLength: 1                
                platformSipAttributes: customSipAttributes
                Keys.onReturnPressed: {
                    txtQueryResults.queryCharacter(txtCharacter.text)
                    txtCharacter.text = ""
                    txtCharacter.closeSoftwareInputPanel()
                }

                platformStyle: TextFieldStyle { paddingRight: clearButton.width }
                Image {
                    id: clearButton
                    anchors.right: parent.right
                    anchors.verticalCenter: parent.verticalCenter
                    source: "image://theme/icon-m-input-clear"
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            inputContext.reset();
                            txtCharacter.text = "";
                        }

                    }

                }
            }

            Button{
                id: btnQuery
                width: 100
                text: qsTr("Submit")
                opacity: 0.5
                onClicked: submitQuery()

                function submitQuery() {
                    txtQueryResults.queryCharacter(txtCharacter.text)
                    txtCharacter.text = ""
                }
            }
        }
        Flickable {
            id: flick
            anchors.left: parent.left
            anchors.leftMargin: 0
            anchors.top: rowQuery.bottom
            contentHeight: rectMain.height * 2.7

            width: rectMain.width            
            height: rectMain.height
            clip: true

            flickableDirection: Flickable.VerticalFlick
            boundsBehavior: Flickable.DragOverBounds

            function ensureVisible(r)
            {
                if (contentY >= r.y)
                    contentY = r.y;
                else if (contentY+height <= r.y+r.height)
                    contentY = r.y+r.height-height;
            }

            QueryDialog {
                id: nonChineseErrorDialog
                titleText: qsTr("Error")
                acceptButtonText: qsTr("OK")
                message: qsTr("Please specify a Chinese character.")
            }

            TextEdit {
                id: txtQueryResults
                text: ""
                textFormat: TextEdit.RichText
                font.pointSize: 18
                width:  rectMain.width
                height: rectMain.height - rowQuery.height
                readOnly: true
                focus: true
                wrapMode: "WrapAtWordBoundaryOrAnywhere"
                onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)                


                function queryCharacter( character ) {

                    if (character != "")
                    {
                        var db = openDatabaseSync("IM","1.0","IMQuery",7235584);
                        var r = "<font size=\"+5\" color=\"#000080\">" + character + "</font> "

                        // Do nothing if character is not Chinese character.
                        if (character.charCodeAt(0)>=31 && character.charCodeAt(0)<=127) {
                            text = ""
                            nonChineseErrorDialog.open()
                        } else {
                            var bRowsReturned

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for Radicals.
                                            var rs = tx.executeSql('SELECT * FROM Radical WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr(" belongs to the ") + rs.rows.item(i).Radical + qsTr(" radical (") + rs.rows.item(i).RemainingStrokes + qsTr(" strokes)") + "<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            if (bRowsReturned==true) {
                                r += "<hr />"
                                bRowsReturned = false
                            }

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for Stroke Order strokes.
                                            var rs = tx.executeSql('SELECT * FROM StrokeOrder WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Total strokes: ") + (rs.rows.item(i).Strokes.length).toString() + "<br />"
                                                    r += qsTr("Stroke 5: ") + JsIMQuery.convertToStrokes(rs.rows.item(i).Strokes.substr(0,5)) + " (" + rs.rows.item(i).Strokes.substr(0,5) + ")<br />"
                                                    r += qsTr("Stroke orders: ") + JsIMQuery.convertToStrokes(rs.rows.item(i).Strokes) + " (" + rs.rows.item(i).Strokes + ")<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            if (bRowsReturned==true) {
                                r += "<hr />"
                                bRowsReturned = false
                            }

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for PinYin inputs.
                                            var rs = tx.executeSql('SELECT * FROM PinYin WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Pin Yin: ") + rs.rows.item(i).Keys + "<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for ZhuYin inputs.
                                            var rs = tx.executeSql('SELECT * FROM ZhuYin WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Zhu Yin: ") + JsIMQuery.convertToZhuYin(rs.rows.item(i).Keys) + " (" + rs.rows.item(i).Keys.toUpperCase() + ")<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            if (bRowsReturned==true) {
                                r += "<hr />"
                                bRowsReturned = false
                            }

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for ChangJie3 & Quick3 radicals.
                                            var rs = tx.executeSql('SELECT * FROM CangJie3 WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("CangJie 3rd Edition: ") + JsIMQuery.convertToCangJie(rs.rows.item(i).Keys) + " (" + rs.rows.item(i).Keys.toUpperCase() + ")<br />"
                                                    r += qsTr("Quick CangJie 3rd Edition: ") + JsIMQuery.convertToCangJie( rs.rows.item(i).Keys.substr(0,1) + rs.rows.item(i).Keys.substr( rs.rows.item(i).Keys.length - 1,1) ) + " (" + rs.rows.item(i).Keys.substr(0,1).toUpperCase() + rs.rows.item(i).Keys.substr( rs.rows.item(i).Keys.length - 1,1).toUpperCase() + ")<br />"
                                                    bRowsReturned = true
                                                }
                                            }

                                            text = r
                                        }
                                        )

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for ChangJie5 & Quick5 radicals.
                                            var rs = tx.executeSql('SELECT * FROM CangJie5 WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("CangJie 5th Edition: ") + JsIMQuery.convertToCangJie(rs.rows.item(i).Keys) + " (" + rs.rows.item(i).Keys.toUpperCase() + ")<br />"
                                                    r += qsTr("Quick CangJie 5th Edition: ") + JsIMQuery.convertToCangJie( rs.rows.item(i).Keys.substr(0,1) + rs.rows.item(i).Keys.substr( rs.rows.item(i).Keys.length - 1,1) ) + " (" + rs.rows.item(i).Keys.substr(0,1).toUpperCase() + rs.rows.item(i).Keys.substr( rs.rows.item(i).Keys.length - 1,1).toUpperCase() + ")<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            if (bRowsReturned==true) {
                                r += "<hr />"
                                bRowsReturned = false
                            }

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for Jyut Ping inputs.
                                            var rs = tx.executeSql('SELECT * FROM JyutPing WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Jyut Ping: ") + rs.rows.item(i).Keys + "<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )
                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for Cantonese inputs.
                                            var rs = tx.executeSql('SELECT * FROM Canton WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Cantonese: ") + rs.rows.item(i).Keys + "<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            if (bRowsReturned==true) {
                                r += "<hr />"
                                bRowsReturned = false
                            }

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for DaiYi 3rd Edition radicals.
                                            var rs = tx.executeSql('SELECT * FROM DaYi3 WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Da Yi 3rd Edition: ") + JsIMQuery.convertToDaiYi3(rs.rows.item(i).Keys) + " (" + rs.rows.item(i).Keys.toUpperCase() + ")<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for WuBi radicals.
                                            var rs = tx.executeSql('SELECT * FROM WuBi WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Wu Bi: ") + JsIMQuery.convertToWuBi(rs.rows.item(i).Keys) + " (" + rs.rows.item(i).Keys.toUpperCase() + ")<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )

                            if (bRowsReturned==true) {
                                r += "<hr />"
                                bRowsReturned = false
                            }

                            db.readTransaction(
                                        function(tx) {
                                            // Look up character for Array30 inputs.
                                            var rs = tx.executeSql('SELECT * FROM Array30 WHERE Character=\'' + character + '\'');

                                            if (rs.rows.length!=0) {
                                                for(var i = 0; i < rs.rows.length; i++) {
                                                    r += qsTr("Array 30 keys: ") + JsIMQuery.convertToArray30(rs.rows.item(i).Keys) + " (" + rs.rows.item(i).Keys + ")<br />"
                                                    bRowsReturned = true
                                                }
                                            }
                                            text = r
                                        }
                                        )


                            r += "\n\n"
                            text = r

                        }

                    }
                }
            }
        }
    }
}
