1/*
2 * Copyright © 2015-2016 Antti Lamminsalo
3 *
4 * This file is part of Orion.
5 *
6 * Orion is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with Orion.  If not, see <http://www.gnu.org/licenses/>.
13 */
14
15import QtQuick.Controls 2.1
16import QtQuick 2.5
17import "components"
18import "util.js" as Util
19
20import app.orion 1.0
21
22Item{
23    id: vodsView
24
25    //anchors.fill: parent
26    property variant selectedChannel
27    property int itemCount: 0
28    property var channelVodPositions
29
30    function search(channel){
31
32        if (!channel || typeof channel == "undefined")
33            return
34
35        selectedChannel = {
36            "_id": channel._id,
37            "name": channel.name,
38            "game": channel.game,
39            "title": channel.title,
40            "online": channel.online,
41            "favourite": channel.favourite || ChannelManager.containsFavourite(channel._id),
42            "viewers": channel.viewers,
43            "logo": channel.logo,
44            "preview": channel.preview,
45        }
46
47        channelVodPositions = VodManager.getChannelVodsLastPlaybackPositions(channel.name);
48
49        VodManager.search(selectedChannel._id, 0, 35)
50
51        itemCount = 35
52
53        requestSelectionChange(3)
54    }
55
56    function getLastPlaybackPosition(channel, vod) {
57        console.log("getLastPlaybackPosition", channel.name, vod._id);
58        return VodManager.getVodLastPlaybackPosition(channel.name, vod._id);
59    }
60
61    property bool itemInView: isItemInView(this)
62    onItemInViewChanged: {
63        if (itemInView) {
64            vodgrid.positionViewAtBeginning()
65            vodgrid.checkScroll()
66        }
67    }
68
69    Connections {
70        target: VodManager
71        onVodLastPositionUpdated: {
72            //console.log("onVodLastPositionUpdated", channel, vod, position);
73            if (selectedChannel.name === channel) {
74                channelVodPositions[vod] = position;
75                // need binding to update
76                channelVodPositions = channelVodPositions;
77            }
78        }
79    }
80
81    CommonGrid {
82        id: vodgrid
83
84        anchors {
85            fill: parent
86        }
87
88        model: vodsModel
89
90        delegate: Video {
91            _id: model.id
92            title: model.title
93            views: model.views
94            preview: model.preview
95            logo: preview
96            duration: model.duration
97            position: channelVodPositions[model.id] || 0
98            game: model.game
99            createdAt: model.createdAt
100            seekPreviews: model.seekPreviews
101
102            width: vodgrid.cellWidth
103        }
104
105        function playItem(item) {
106            var lastPlaybackPosition = getLastPlaybackPosition(selectedChannel, item);
107            playerView.getStreams(selectedChannel, item, lastPlaybackPosition || 0);
108        }
109
110        onItemClicked: playItem(clickedItem)
111        onItemDoubleClicked: playItem(clickedItem)
112
113        onItemTooltipHover: {
114            if (g_tooltip)
115                g_tooltip.displayVod(item, getPosition)
116        }
117
118        onAtYEndChanged: checkScroll()
119
120        onUpdateTriggered: search(selectedChannel)
121
122        function checkScroll(){
123            if (atYEnd && model.count() === itemCount && itemCount > 0){
124                VodManager.search(selectedChannel._id, itemCount, 25)
125                itemCount += 25
126            }
127        }
128    }
129}
130