1/*
2 * SPDX-FileCopyrightText: 2020 Dimitris Kardarakos <dimkard@posteo.net>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7import QtQuick 2.7
8import QtQuick.Controls 2.0 as Controls2
9import QtQuick.Layouts 1.3
10import org.kde.kirigami 2.12 as Kirigami
11import org.kde.calindori 0.1 as Calindori
12
13ListView {
14    id: root
15
16    property int fstDayOfWeek: _appLocale.firstDayOfWeek
17    property date startDate
18    property date selectedWeekDate: firstDateOfWeek(startDate)
19    property date selectedDate: startDate
20    property var cal
21    property bool wideScreen
22
23    signal nextWeek
24    signal previousWeek
25    signal goCurrentWeek
26    signal addEvent
27    signal addTodo
28
29    /**
30    * @brief Get the date of the first day of a week, given a date in the week
31    *
32    */
33
34    function firstDateOfWeek(inputDate)
35    {
36        var t = inputDate;
37        t.setDate(inputDate.getDate() - inputDate.getDay() + (inputDate.getDay() >= fstDayOfWeek ? fstDayOfWeek : fstDayOfWeek-7));
38        t.setHours(inputDate.getHours())
39        t.setMinutes(0);
40        t.setSeconds(0);
41
42        return t;
43    }
44
45    /**
46    * @brief Remove the editor page from the stack. If an incidence page exists in the page stack, remove it as well
47    *
48    */
49    function removeEditorPage(editor)
50    {
51        var incidencePageExists = pageStack.items[pageStack.depth-2] && pageStack.items[pageStack.depth - 2].hasOwnProperty("isIncidencePage");
52        pageStack.pop(eventEditor);
53        if(incidencePageExists)
54        {
55            pageStack.pop(incidencePage);
56        }
57    }
58
59    function moveDate(startDt, offset)
60    {
61        var movedDt = startDt;
62        movedDt.setDate(startDt.getDate() + offset);
63
64        return movedDt;
65    }
66
67    onNextWeek: {
68        selectedWeekDate = moveDate(selectedWeekDate, 7);
69        selectedDate = selectedWeekDate;
70        currentIndex = 0;
71    }
72
73    onPreviousWeek: {
74        selectedWeekDate = moveDate(selectedWeekDate, -7);
75        selectedDate = selectedWeekDate;
76        currentIndex = 0;
77    }
78
79    onGoCurrentWeek: {
80        selectedWeekDate = firstDateOfWeek(startDate);
81        selectedDate = startDate;
82        currentIndex = selectedDate.getDay() >= fstDayOfWeek ? selectedDate.getDay() - fstDayOfWeek : 7 - (selectedDate.getDay() +  fstDayOfWeek)
83    }
84
85    onCurrentIndexChanged: {
86        if (pageStack.depth > 1) {
87            pageStack.pop(null);
88        }
89    }
90
91    model: 7
92    currentIndex: selectedDate.getDay() >= fstDayOfWeek ? selectedDate.getDay() - fstDayOfWeek : 7 - (selectedDate.getDay() +  fstDayOfWeek)
93
94    delegate: Kirigami.SwipeListItem {
95        id: dayListItem
96
97        property var weekDay: model.index
98        property var itemDate: {
99                    var dt = root.selectedWeekDate;
100                    dt.setDate(dt.getDate() + index);
101                    dt.setHours(dt.getHours() + 1);
102                    dt.setMinutes(0);
103                    dt.setSeconds(0);
104                    dt.setMilliseconds(0);
105
106                    return dt;
107        }
108
109        alwaysVisibleActions: false
110
111        RowLayout {
112            spacing: Kirigami.Units.largeSpacing * 2
113
114            ColumnLayout  {
115                Layout.minimumWidth: Kirigami.Units.gridUnit * 2
116
117                Controls2.Label {
118                    text: _appLocale.dayName(model.index + fstDayOfWeek, Locale.ShortFormat)
119                    Layout.alignment: Qt.AlignHCenter
120                }
121
122                Controls2.Label {
123                    font: Kirigami.Theme.smallFont
124                    text: itemDate.toLocaleDateString(_appLocale, "d MMM")
125                    Layout.alignment: Qt.AlignHCenter
126                }
127            }
128
129            ColumnLayout {
130
131                Repeater {
132                    model: Calindori.IncidenceModel {
133                        appLocale: _appLocale
134                        calendar: root.cal
135                        filterDt: moveDate(root.selectedWeekDate, dayListItem.weekDay)
136                        filterMode: 4
137                    }
138
139                    IncidenceItemDelegate {
140                        itemBackgroundColor: model.type === 0 ? Kirigami.Theme.backgroundColor : Qt.darker(Kirigami.Theme.backgroundColor, 1.1)
141                        label: model.summary
142                        subtitle: (model.type == 0 ? model.displayStartEndTime : (model.displayDueTime || model.displayStartTime))
143                        Layout.fillWidth: true
144
145                        onClicked: {
146                            if(pageStack.lastItem && pageStack.lastItem.hasOwnProperty("isIncidencePage")) {
147                                pageStack.pop(incidencePage);
148                            }
149
150                            pageStack.push(incidencePage, { incidence: model })
151                        }
152                    }
153                }
154            }
155        }
156
157        actions: [
158            Kirigami.Action {
159                iconName: "resource-calendar-insert"
160                text: i18n("New event")
161
162                onTriggered: pageStack.push(eventEditor, { startDt: itemDate })
163            },
164
165            Kirigami.Action {
166                iconName: "task-new"
167                text: i18n("New task")
168
169                onTriggered: pageStack.push(todoEditor, { startDt: itemDate })}
170        ]
171
172        onClicked: { root.selectedDate = moveDate(root.selectedWeekDate, model.index) }
173
174    }
175
176    Component {
177        id: incidencePage
178
179        IncidencePage {
180            calendar: root.cal
181        }
182    }
183
184    Component {
185        id: eventEditor
186
187        EventEditorPage {
188            calendar: root.cal
189
190            onEditcompleted: removeEditorPage(eventEditor)
191        }
192    }
193
194    Component {
195        id: todoEditor
196
197        TodoEditorPage {
198            calendar: root.cal
199
200            onEditcompleted: removeEditorPage(todoEditor)
201        }
202    }
203}
204