1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::app_ui::widgets::CommonStateManager;
use std::collections::VecDeque;
use std::rc::Rc;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

use super::async_task_channel::{ChannelRequestSender, TaskResponse};
use super::event::VimPingSender;
use super::widgets::help_bar::HelpBar;
use super::widgets::notification::{Notification, WidgetName, WidgetVariant};
use super::widgets::popup::Popup;
use super::widgets::question_list::QuestionListWidget;
use super::widgets::stats::Stats;
use super::widgets::topic_list::TopicTagListWidget;
use super::widgets::Widget;
use crate::config::Config;
use crate::errors::AppResult;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use indexmap::IndexMap;

/// Application.
#[derive(Debug)]
pub struct App {
    /// Is the application running?
    pub running: bool,

    pub(crate) widget_map: indexmap::IndexMap<WidgetName, WidgetVariant>,

    selected_wid_idx: i32,

    pub task_request_sender: ChannelRequestSender,

    pub pending_notifications: VecDeque<Option<Notification>>,

    pub(crate) popup_stack: Vec<Popup>,

    pub vim_tx: VimPingSender,

    pub vim_running: Arc<AtomicBool>,

    pub config: Rc<Config>,
}

impl App {
    /// Constructs a new instance of [`App`].
    pub fn new(
        task_request_sender: ChannelRequestSender,
        vim_tx: VimPingSender,
        vim_running: Arc<AtomicBool>,
        config: Rc<Config>,
    ) -> AppResult<Self> {
        let w0 = WidgetVariant::TopicList(TopicTagListWidget::new(
            WidgetName::TopicList,
            task_request_sender.clone(),
        ));
        let w1 = WidgetVariant::QuestionList(QuestionListWidget::new(
            WidgetName::QuestionList,
            task_request_sender.clone(),
            vim_tx.clone(),
            vim_running.clone(),
            config.clone(),
        ));

        let w2 = WidgetVariant::Stats(Stats::new(WidgetName::Stats, task_request_sender.clone()));

        let w3 = WidgetVariant::HelpLine(HelpBar::new(
            WidgetName::HelpLine,
            task_request_sender.clone(),
        ));

        let order = [
            (WidgetName::TopicList, w0),
            (WidgetName::QuestionList, w1),
            (WidgetName::Stats, w2),
            (WidgetName::HelpLine, w3),
        ];

        let mut app = Self {
            config,
            running: true,
            widget_map: IndexMap::from(order),
            selected_wid_idx: 0,
            task_request_sender,
            pending_notifications: vec![].into(),
            popup_stack: vec![],
            vim_running,
            vim_tx,
        };
        app.setup()?;
        Ok(app)
    }

    pub fn total_widgets_count(&self) -> usize {
        self.widget_map.len()
    }

    pub fn navigate(&mut self, val: i32) -> AppResult<Option<Notification>> {
        if self.get_current_popup().is_some() {
            return Ok(None);
        }
        self.get_current_widget_mut().set_inactive();
        let a = self.selected_wid_idx + val;
        let b = self.total_widgets_count() as i32;
        self.selected_wid_idx = ((a % b) + b) % b;
        let maybe_notif = self.get_current_widget_mut().set_active()?;
        self.push_notif(maybe_notif);
        if !self.get_current_widget().is_navigable() {
            self.navigate(val)?;
        }
        Ok(None)
    }

    pub fn next_widget(&mut self) -> AppResult<Option<Notification>> {
        self.navigate(1)
    }

    pub fn prev_widget(&mut self) -> AppResult<Option<Notification>> {
        self.navigate(-1)
    }

    pub(crate) fn get_current_widget(&self) -> &WidgetVariant {
        let (_, v) = self
            .widget_map
            .get_index(self.selected_wid_idx as usize)
            .unwrap();
        v
    }

    pub(crate) fn get_current_widget_mut(&mut self) -> &mut WidgetVariant {
        let (_, v) = self
            .widget_map
            .get_index_mut(self.selected_wid_idx as usize)
            .unwrap();
        v
    }

    pub(crate) fn get_current_popup(&self) -> Option<&Popup> {
        self.popup_stack.last()
    }

    pub(crate) fn get_current_popup_mut(&mut self) -> Option<&mut Popup> {
        self.popup_stack.last_mut()
    }

    pub fn setup(&mut self) -> AppResult<()> {
        let maybe_notif = self.get_current_widget_mut().set_active()?;
        self.push_notif(maybe_notif);
        for (_, widget) in self.widget_map.iter_mut() {
            widget.setup()?;
        }
        Ok(())
    }

    pub fn push_notif(&mut self, value: Option<Notification>) {
        self.pending_notifications.push_back(value)
    }

    /// Handles the tick event of the terminal.
    pub fn tick(&mut self, task: Option<TaskResponse>) -> AppResult<()> {
        if let Some(task) = task {
            self.process_task(task)?;
        }

        if let Some(popup) = self.get_current_popup_mut() {
            if !popup.is_active() {
                self.popup_stack.pop();
                let maybe_notif;
                if let Some(popup) = self.get_current_popup_mut() {
                    maybe_notif = popup.set_active()?;
                } else {
                    maybe_notif = self.get_current_widget_mut().set_active()?;
                }
                self.push_notif(maybe_notif);
            };
        }

        for wid in self.widget_map.values_mut() {
            while let Some(notif) = wid.get_notification_queue().pop_front() {
                self.pending_notifications.push_back(Some(notif));
            }
        }
        self.process_pending_notification()?;
        Ok(())
    }

    pub fn process_task(&mut self, task: TaskResponse) -> AppResult<()> {
        self.widget_map
            .get_mut(&task.get_widget_name())
            .unwrap()
            .process_task_response(task)?;
        Ok(())
    }

    pub fn process_pending_notification(&mut self) -> AppResult<()> {
        while let Some(elem) = self.pending_notifications.pop_front() {
            if let Some(notif) = elem {
                let wid_name = notif.get_wid_name();
                if let WidgetName::Popup = wid_name {
                    let mut popup_instance =
                        Popup::new(wid_name.clone(), self.task_request_sender.clone());
                    let maybe_notif = popup_instance.process_notification(notif)?;
                    self.push_notif(popup_instance.set_active()?);
                    self.pending_notifications.push_back(maybe_notif);
                    self.popup_stack.push(popup_instance);
                } else {
                    let widget_var = self.widget_map.get_mut(wid_name).unwrap();
                    let more_notif = widget_var.process_notification(notif)?;
                    self.pending_notifications.push_back(more_notif);
                }
            }
        }
        Ok(())
    }

    /// Set running to false to quit the application.
    pub fn quit(&mut self) {
        self.running = false;
    }

    pub fn handle_key_events(&mut self, key_event: KeyEvent) -> AppResult<()> {
        let mut p_notif = None;

        // if ui has active popups then send only events registered with popup
        if let Some(popup) = self.get_current_popup_mut() {
            p_notif = popup.handler(key_event)?;
        } else if self.get_current_widget().parent_can_handle_events() {
            match key_event.code {
                KeyCode::Left => p_notif = self.next_widget()?,
                KeyCode::Right => p_notif = self.prev_widget()?,
                KeyCode::Char('q') | KeyCode::Char('Q') => {
                    self.running = false;
                }
                KeyCode::Char('c') | KeyCode::Char('C') => {
                    if key_event.modifiers == KeyModifiers::CONTROL {
                        self.running = false;
                    }
                }
                _ => {
                    p_notif = self.get_current_widget_mut().handler(key_event)?;
                }
            }
        } else {
            p_notif = self.get_current_widget_mut().handler(key_event)?;
        }

        self.pending_notifications.push_back(p_notif);
        self.tick(None)?;

        Ok(())
    }
}