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
use ratatui::widgets::ListState;

use crate::entities::topic_tag::Model as TopicTagModel;
use crate::{entities::question::Model as QuestionModel, errors::AppResult};
use std::collections::{HashMap, HashSet};

use super::{
    channel::{ChannelRequestSender, ChannelResponseReceiver, TaskResponse},
    list::StatefulList,
};

/// Application result type.

pub type SS = (TopicTagModel, Vec<QuestionModel>);

pub type TTReciever = crossbeam::channel::Receiver<SS>;
pub type TTSender = crossbeam::channel::Sender<SS>;

#[derive(Debug)]
pub enum Widget<'a> {
    QuestionList(&'a mut StatefulList<QuestionModel>),
    TopicTagList(&'a mut StatefulList<TopicTagModel>),
}

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

    pub widgets: &'a mut Vec<Widget<'a>>,

    pub questions_list: Option<HashMap<TopicTagModel, Vec<QuestionModel>>>,

    pub widget_switcher: i32,

    pub last_response: Option<TaskResponse>,

    pub show_popup: bool,

    pub task_request_sender: ChannelRequestSender,

    pub task_response_recv: ChannelResponseReceiver,
}

impl<'a> App<'a> {
    /// Constructs a new instance of [`App`].
    pub fn new(
        wid: &'a mut Vec<Widget<'a>>,
        task_request_sender: ChannelRequestSender,
        task_response_recv: ChannelResponseReceiver,
    ) -> AppResult<Self> {
        task_request_sender.send(super::channel::TaskRequest::GetAllQuestionsMap)?;
        task_request_sender.send(super::channel::TaskRequest::GetAllTopicTags)?;

        let mut app = Self {
            running: true,
            questions_list: None,
            widgets: wid,
            widget_switcher: 0,
            task_request_sender,
            task_response_recv,
            last_response: None,
            show_popup: false,
        };
        app.update_question_list();
        Ok(app)
    }

    pub fn next_widget(&mut self) {
        let a = self.widget_switcher + 1;
        let b = self.widgets.len() as i32;
        self.widget_switcher = ((a % b) + b) % b;
    }

    pub fn prev_widget(&mut self) {
        let a = self.widget_switcher - 1;
        let b = self.widgets.len() as i32;
        self.widget_switcher = ((a % b) + b) % b;
    }

    pub fn get_current_widget(&self) -> &Widget {
        &self.widgets[self.widget_switcher as usize]
    }

    pub fn update_question_in_popup(&self) -> AppResult<()> {
        if self.show_popup {
            if let Widget::QuestionList(s) = self.get_current_widget() {
                if let Some(selected_item) = s.get_selected_item() {
                    if let Some(slug) = &selected_item.title_slug {
                        self.task_request_sender.send(
                            super::channel::TaskRequest::QuestionDetail { slug: slug.clone() },
                        )?;
                    }
                }
            }
        }
        Ok(())
    }

    pub fn update_question_list(&mut self) {
        let mut tt_model: Option<TopicTagModel> = None;

        if let Widget::TopicTagList(ttl) = self.get_current_widget() {
            if let Some(selected) = ttl.get_selected_item() {
                tt_model = Some(selected.clone())
            }
        }

        for w in self.widgets.iter_mut() {
            if let Widget::QuestionList(ql) = w {
                if let Some(selected_tt_model) = &tt_model {
                    let mut items;
                    if let Some(tt_ql_map) = &mut self.questions_list {
                        if selected_tt_model.id.as_str() == "all" {
                            let set = tt_ql_map
                                .values()
                                .flat_map(|q| q.clone())
                                .collect::<HashSet<_>>();
                            items = set.into_iter().collect::<Vec<_>>();
                        } else {
                            items = tt_ql_map.get(selected_tt_model).unwrap().clone();
                        }
                        items.sort();
                        ql.items = items;
                        ql.state = ListState::default();
                    }
                }
            }
        }
    }

    // pub fn find_widget(&mut self, wid_type: Widget) -> &mut Widget {
    //     for wid in self.widgets {
    //         if wid == Widget::
    //     }

    // }

    pub fn toggle_popup(&mut self) {
        self.show_popup = !self.show_popup;
    }

    /// Handles the tick event of the terminal.
    pub fn tick(&mut self) {
        if let Ok(response) = self.task_response_recv.try_recv() {
            match response {
                TaskResponse::GetAllQuestionsMap(map) => {
                    if let Some(ql) = &mut self.questions_list {
                        ql.extend(map)
                    } else {
                        self.questions_list = Some(map);
                    }
                    self.update_question_list()
                }
                TaskResponse::AllTopicTags(tts) => {
                    for w in self.widgets.iter_mut() {
                        if let Widget::TopicTagList(tt_list) = w {
                            tt_list.items.extend(tts);
                            break;
                        }
                    }
                }
                response => self.last_response = Some(response),
            }
        }
    }

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