/*
    Speed Climbing Stopwatch - Simple Stopwatch for Climbers
    Copyright (C) 2018 - 2019  Itsblue Development - Dorian Zeder

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, version 3 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import QtQuick 2.9
import QtMultimedia 5.8
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
import QtGraphicalEffects 1.0

import de.itsblue.ScStw 2.0
import de.itsblue.ScStw.Styling 2.0
import de.itsblue.ScStw.Styling.Components 1.0
import de.itsblue.ScStwApp 2.0

import "../components"


RemoteDataListView {
    id: profileList

    property int currentAthlete: -1
    property string title: "profiles"
    property string secondButt: "add"

    signal opened()

    onOpened: {
        profileList.loadData()
    }

    //anchors.fill: parent
    //anchors.topMargin: topContainerItm.height * 0.1

    loadData: function () {
        status = 905
        //listData = {}
        var retData = speedBackend.getAthletes()

        if(!retData){
            status = 500
            return
        }

        listData = retData["allAthletes"]
        currentAthlete = retData["activeAthlete"]
        status = listData.lenght !== false ? 200:0
    }

    delegate: SwipeDelegate {
        id: swipeDelegate

        property int thisIndex: index

        text: profileList.listData[index]["fullName"]
        width: profileList.width - (swipeDelegate.x)
        height: profileList.height / 5

        font.pixelSize: profilesStack.text_pixelSize

        function remove() {
            removeAnim.start()
        }

        onClicked: {
            profilesStack.openResults(profileList.listData[index]["userName"])
        }

        contentItem: Text {
            visible: false
        }

        Text {

            anchors {
                verticalCenter: parent.verticalCenter
                left: parent.left
                leftMargin: swipeDelegate.width * 0.05
                right: athleteSelectBoxRow.left
                rightMargin: leftMargin
            }

            text: swipeDelegate.text
            color: appTheme.theme.colors.text

            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft

            fontSizeMode: Text.Fit

            font.pixelSize: swipeDelegate.height * 0.4

            minimumPixelSize: 1
        }

        background: Rectangle {
            color: pressed ? appTheme.theme.colors.delegatePressed : appTheme.theme.colors.delegateBackground

            Behavior on color {

                ColorAnimation {
                    duration: 200
                }
            }
        }

        Row {
            id: athleteSelectBoxRow
            anchors {
                verticalCenter: parent.verticalCenter
                right: parent.right
                rightMargin: 7
            }

            height: parent.height
            width: childrenRect.width

            Repeater {
                id: athleteSelectBoxRep

                model: scStwRemoteRace.timers.length

                delegate: CheckBox {
                    id: control

                    property bool active: scStwRemoteRace.timers[index]["id"] === profileList.listData[swipeDelegate.thisIndex]["active"]

                    anchors.verticalCenter: parent.verticalCenter

                    height: parent.height * 0.6

                    enabled: scStwRemoteRace.timers[index]["state"] !== ScStwTimer.DISABLED
                    checked: control.active

                    onCheckedChanged: {
                        if(checked && !control.active && speedBackend.selectAthlete(profileList.listData[swipeDelegate.thisIndex]["userName"], scStwRemoteRace.timers[index]["id"])){
                            profileList.loadData()
                        }
                    }

                    indicator: Rectangle {
                        implicitWidth: 26
                        implicitHeight: 26

                        height: parent.height
                        width: height

                        x: control.leftPadding
                        y: parent.height / 2 - height / 2

                        radius: width * 0.2
                        border.color: control.enabled ? control.down ? "#17a81a" : "#21be2b" : "grey"
                        color: control.down ? appTheme.theme.colors.delegatePressed : appTheme.theme.colors.delegateBackground

                        Rectangle {
                            width: parent.width * 0.65
                            height: width
                            anchors.centerIn: parent
                            radius: control.checked ? width * 0.2:0
                            color: control.enabled ? control.down ? "#17a81a" : "#21be2b" : "grey"
                            opacity: control.checked ? 1:0
                            scale: control.checked ? 0.9:0

                            Behavior on color {
                                ColorAnimation {
                                    duration: 200
                                }
                            }

                            Behavior on radius {
                                NumberAnimation {
                                    duration: 200
                                }
                            }

                            Behavior on opacity {
                                NumberAnimation {
                                    duration: 200
                                }
                            }

                            Behavior on scale {
                                NumberAnimation {
                                    duration: 200
                                }
                            }
                        }
                    }
                }

            }

        }

        Rectangle {
            color: "grey"
            height: 1
            width: parent.width * 0.9
            visible: index > 0
            anchors {
                horizontalCenter: parent.horizontalCenter
                top: parent.top
            }
        }

        NumberAnimation {
            id: removeAnim
            target: swipeDelegate
            property: "height"
            to: 0
            easing.type: Easing.InOutQuad
            onStopped: profileModel.model.remove(index)
        }

        swipe.transition: Transition {
            SmoothedAnimation { velocity: 3; easing.type: Easing.InOutCubic }
        }

        swipe.left: Row {
            anchors.left: parent.left
            height: parent.height

            Label {
                id: deleteLabel
                text: qsTr("Delete")
                color: appTheme.theme.colors.text
                verticalAlignment: Label.AlignVCenter
                padding: 12
                height: parent.height

                SwipeDelegate.onClicked: {
                    profileList.status = 905
                    if(speedBackend.deleteAthlete(profileList.listData[index]["userName"])){
                        profileList.loadData()
                        return
                    }
                    profileList.status = 200
                }

                background: Rectangle {
                    color: deleteLabel.SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato"
                }
            }
        }
    }
}
