import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls.Material 2.15

ColoredItemDelegate {
        id: partDel

        property int thisIndex: index
        property var thisData: widgetData[ "participants" ][partDel.thisIndex]

        width: control.width
        height: partDelCol.showSideBySide ? 40:70

        text: ""

        opacity: 0
        scale: 0.9

        onThisDataChanged: {
            fadeInPa.start()
        }

        onClicked: {
            app.openWidget({person:thisData["PerId"]})
        }

        highlighted: partDel.thisIndex % 2 == 0

        ParallelAnimation {
            id: fadeInPa
            NumberAnimation { target: partDel; property: "opacity"; from: 0; to: 1.0; duration: 400 }
            NumberAnimation { target: partDel; property: "scale"; from: 0.8; to: 1.0; duration: 400 }
        }

        GridLayout {
            id: partDelCol

            property bool showSideBySide: app.landscape() || app.largeScreen()

            anchors.fill: parent
            anchors.margins: 5

            columns: showSideBySide ? 2:1
            rows: showSideBySide ? 1:2

            columnSpacing: height * 0.2
            rowSpacing: 0

            Row {
                id: partDelFirstRow

                Layout.fillWidth: true
                Layout.fillHeight: true

                Label {
                    height: parent.height
                    width: text === "" ? parent.width * 0.08:parent.width * 0.1

                    fontSizeMode: Text.Fit
                    font.bold: true
                    font.pixelSize: Math.abs( partDelSecondRow.height > 0 ? height * 0.6:height * 0.4 )
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter

                    text: partDel.thisData["result_rank"] === undefined ? "":partDel.thisData["result_rank"]
                }

                Label {
                    height: parent.height
                    width: parent.width * 0.5

                    fontSizeMode: Text.Fit
                    font.bold: true
                    font.pixelSize: Math.abs( partDelSecondRow.height > 0 ? height * 0.6:height * 0.4 )
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft

                    text: partDel.thisData["firstname"] + " " + partDel.thisData["lastname"] + (partDel.thisData["start_number"] !== undefined ? (" (" + partDel.thisData["start_number"] + ")"):"")
                }

                Label {
                    height: parent.height
                    width: parent.width * 0.4

                    fontSizeMode: Text.Fit
                    font.bold: false
                    font.pixelSize: Math.abs( partDelSecondRow.height > 0 ? height * 0.4:height * 0.3 )
                    minimumPixelSize: height * 0.3 < 1 ? 1:height * 0.3

                    elide: "ElideRight"

                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter

                    text: "<html>(<a href=\"" + (partDel.thisData["fed_url"] === undefined ? "":partDel.thisData["fed_url"]).toString() + "\">" + (widgetData[ "display_athlete" ] === "nation" ? partDel.thisData["nation"] : partDel.thisData["federation"]) + "</a>)</html>"

                    onLinkActivated: {
                        Qt.openUrlExternally(link)
                    }
                }
            }

            Row {
                id: partDelSecondRow

                Layout.preferredWidth: partDelCol.showSideBySide ? parent.width * 0.5 : parent.width
                Layout.preferredHeight: partDelCol.showSideBySide ? parent.height : parent.height * 0.5

                visible: boulderResRow.active || generalResRow.active || speedQualificationResRow.active || resultLa.active

                BoulderResultRow {
                    id: boulderResRow
                }

                GeneralResultRow {
                    id: generalResRow
                }

                SpeedQualificationResultRow {
                    id: speedQualificationResRow
                }

                Label {
                    id: resultLa

                    property bool active: ( parseInt(widgetData[ "route_num_problems" ]) > 0 || widgetData["discipline"] !== "boulder" ) && parseInt(widgetData[ "route_order" ]) > -1 && !speedQualificationResRow.active

                    width: enabled ? parent.width * 0.25:0
                    height: enabled ? parent.height:0

                    enabled: active

                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter

                    fontSizeMode: Text.Fit
                    font.pixelSize: Math.abs( height * 0.6 )
                    minimumPixelSize: 1

                    text: widgetData[ "participants" ][partDel.thisIndex]["result"] === undefined ? "":widgetData[ "participants" ][partDel.thisIndex]["result"]
                }
            }
        }
    }
