/*
 * Copyright (C) 2020-2026 Savoir-faire Linux Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQml.Models
import net.jami.Adapters 1.1
import net.jami.Models 1.1
import net.jami.Constants 1.1
import net.jami.Enums 1.1
import "../../commoncomponents"

Item {
    id: root

    property int count: 0
    property bool inLine: CallParticipantsModel.conferenceLayout === CallParticipantsModel.ONE_WITH_SMALL
    property bool participantsSide
    property bool enableHideSpectators: CallParticipantsModel.count > 1 && CurrentCall.hideSpectators
    property string hoveredOverlayUri: ""
    property string hoveredOverlaySinkId: ""
    property bool hoveredOverVideoMuted: true
    property bool screenshotButtonHovered: false

    ListModel {
        id: emptyParticipantsModel
    }

    onVisibleChanged: {
        CurrentCall.hideSelf = UtilsAdapter.getAppValue(Settings.HideSelf);
        CurrentCall.hideSpectators = UtilsAdapter.getAppValue(Settings.HideSpectators);
        CurrentCall.flipSelf = UtilsAdapter.getAppValue(Settings.FlipSelf);
    }

    Component {
        id: callVideoMedia

        ParticipantOverlay {
            id: overlay

            anchors.fill: parent
            anchors.leftMargin: leftMargin_
            isScreenshotButtonHovered: screenshotButtonHovered && hoveredOverlaySinkId === sinkId_
            opacity: screenshotButtonHovered ? hoveredOverlaySinkId !== sinkId ? 0.1 : 1 : 1
            sinkId: sinkId_
            uri: uri_
            deviceId: deviceId_
            isMe: isLocal_
            participantIsModerator: isModerator_
            bestName: {
                if (bestName_ === uri_)
                    NameDirectory.lookupAddress(CurrentAccount.id, uri_);
                return bestName_;
            }
            videoMuted: videoMuted_
            participantIsActive: active_
            isLocalMuted: audioLocalMuted_
            voiceActive: voiceActive_
            isRecording: isRecording_
            participantIsModeratorMuted: audioModeratorMuted_
            participantHandIsRaised: isHandRaised_
            isSharing: isSharing_

            onParticipantHoveredChanged: {
                if (participantHovered) {
                    hoveredOverlayUri = overlay.uri;
                    hoveredOverlaySinkId = overlay.sinkId;
                    hoveredOverVideoMuted = videoMuted_;
                }
            }

            Connections {
                id: registeredNameFoundConnection

                target: NameDirectory
                enabled: bestName_ === uri_

                function onRegisteredNameFound(status, address, registeredName, requestedName) {
                    if (address === uri_ && status === NameDirectory.LookupStatus.SUCCESS) {
                        bestName_ = registeredName;
                    }
                }
            }
        }
    }

    SortFilterProxyModel {
        id: genericParticipantsModel
        model: root.visible ? CallParticipantsModel : emptyParticipantsModel
        filters: [
            ValueFilter {
                roleName: "Active"
                value: false
            },
            ValueFilter {
                enabled: CallParticipantsModel.count > 1 && CurrentCall.hideSelf
                roleName: "IsLocal"
                value: false
            },
            ValueFilter {
                enabled: root.enableHideSpectators
                roleName: "HideSpectators"
                value: false
            }
        ]
    }

    SortFilterProxyModel {
        id: activeParticipantsModel
        model: root.visible ? CallParticipantsModel : emptyParticipantsModel
        filters: ValueFilter {
            roleName: "Active"
            value: true
        }
    }

    ParticipantsLayoutVertical {
        anchors.fill: parent
        participantComponent: callVideoMedia
        visible: !participantsSide
        onLayoutCountChanged: root.count = layoutCount
    }

    ParticipantsLayoutHorizontal {
        anchors.fill: parent
        participantComponent: callVideoMedia
        visible: participantsSide
        onLayoutCountChanged: root.count = layoutCount
    }
}
