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
252
253
254
255
256
use super::header::SelectedMenuItem;
use color_eyre::eyre::Result;
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Style, Stylize},
    text::{Line, Span},
    widgets::{Block, Borders, Cell, Padding, Row, Table},
    Frame,
};
use tokio::sync::mpsc::UnboundedSender;

use super::Component;
use crate::{
    action::Action,
    components::header::Header,
    mode::{InputMode, Scene},
    style::{EUCALYPTUS, GHOST_WHITE, VERY_LIGHT_AZURE, VIVID_SKY_BLUE},
    widgets::hyperlink::Hyperlink,
};
use ansi_to_tui::IntoText;

#[derive(Clone)]
pub struct Help {
    pub active: bool,
    pub action_tx: Option<UnboundedSender<Action>>,
}

impl Help {
    pub async fn new() -> Result<Self> {
        Ok(Self {
            active: false,
            action_tx: None,
        })
    }
}

impl Component for Help {
    fn init(&mut self, _area: Rect) -> Result<()> {
        Ok(())
    }

    fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
        if !self.active {
            return Ok(());
        }
        // We define a layout, top and down box.
        let layout = Layout::default()
            .direction(Direction::Vertical)
            .constraints(vec![
                Constraint::Length(1),
                Constraint::Min(7),
                Constraint::Max(9),
            ])
            .split(area);

        // ==== Header =====
        let header = Header::new();
        f.render_stateful_widget(header, layout[0], &mut SelectedMenuItem::Help);

        // ---- Get Help & Support ----
        // Links

        let quickstart_guide_link = Hyperlink::new(
            "docs.autonomi.com/getstarted",
            "https://docs.autonomi.com/getstarted",
        );
        let beta_rewards_link = Hyperlink::new("autonomi.com/beta", "https://autonomi.com/beta");
        let get_direct_support_link =
            Hyperlink::new("autonomi.com/support", "https://autonomi.com/support");
        let download_latest_link =
            Hyperlink::new("autonomi.com/downloads", "https://autonomi.com/downloads");

        // Content
        let rows_help_and_support = vec![
            Row::new(vec![
                Cell::from(Line::from(vec![Span::styled(
                    "See the quick start guides:",
                    Style::default().fg(GHOST_WHITE),
                )])),
                Cell::from(Line::from(vec![Span::styled(
                    "To join the Beta Rewards Program:",
                    Style::default().fg(GHOST_WHITE),
                )])),
            ]),
            Row::new(vec![
                Cell::from(
                    quickstart_guide_link
                        .to_string()
                        .into_text()
                        .unwrap()
                        .clone(),
                ),
                Cell::from(beta_rewards_link.to_string().into_text().unwrap().clone()),
            ]),
            Row::new(vec![
                // Empty row for padding
                Cell::from(Span::raw(" ")),
                Cell::from(Span::raw(" ")),
            ]),
            Row::new(vec![
                Cell::from(Line::from(vec![Span::styled(
                    "Get Direct Support:",
                    Style::default().fg(GHOST_WHITE),
                )])),
                Cell::from(Line::from(vec![Span::styled(
                    "Download the latest launchpad:",
                    Style::default().fg(GHOST_WHITE),
                )])),
            ]),
            Row::new(vec![
                Cell::from(
                    get_direct_support_link
                        .to_string()
                        .into_text()
                        .unwrap()
                        .clone(),
                ),
                Cell::from(
                    download_latest_link
                        .to_string()
                        .into_text()
                        .unwrap()
                        .clone(),
                ),
            ]),
        ];

        let table_help_and_support = Table::new(
            rows_help_and_support,
            vec![Constraint::Percentage(50), Constraint::Percentage(50)],
        )
        .block(
            Block::new()
                .borders(Borders::ALL)
                .padding(Padding::uniform(1))
                .title(" Get Help & Support ")
                .title_style(Style::default().bold()),
        );

        f.render_widget(table_help_and_support, layout[1]);

        // ---- Keyboard shortcuts ----
        let rows_keyboard_shortcuts = vec![
            Row::new(vec![
                Cell::from(Line::from(vec![
                    Span::styled("[S] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Status", Style::default().fg(VIVID_SKY_BLUE)),
                ])),
                Cell::from(Line::from(vec![
                    Span::styled("[Ctrl+G] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Manage Nodes", Style::default().fg(EUCALYPTUS)),
                ])),
                Cell::from(Line::from(vec![
                    Span::styled("[Ctrl+D] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled(
                        "Change Storage Drive",
                        Style::default().fg(VERY_LIGHT_AZURE),
                    ),
                ])),
            ]),
            Row::new(vec![
                // Empty row for padding
                Cell::from(Span::raw(" ")),
                Cell::from(Span::raw(" ")),
                Cell::from(Span::raw(" ")),
            ]),
            Row::new(vec![
                Cell::from(Line::from(vec![
                    Span::styled("[O] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Options", Style::default().fg(VIVID_SKY_BLUE)),
                ])),
                Cell::from(Line::from(vec![
                    Span::styled("[Ctrl+S] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Start Nodes", Style::default().fg(EUCALYPTUS)),
                ])),
                Cell::from(Line::from(vec![
                    Span::styled("[Ctrl+B] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled(
                        "Edit Discord Username",
                        Style::default().fg(VERY_LIGHT_AZURE),
                    ),
                ])),
            ]),
            Row::new(vec![
                // Empty row for padding
                Cell::from(Span::raw(" ")),
                Cell::from(Span::raw(" ")),
                Cell::from(Span::raw(" ")),
            ]),
            Row::new(vec![
                Cell::from(Line::from(vec![
                    Span::styled("[H] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Help", Style::default().fg(VIVID_SKY_BLUE)),
                ])),
                Cell::from(Line::from(vec![
                    Span::styled("[Ctrl+X] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Stop Nodes", Style::default().fg(EUCALYPTUS)),
                ])),
                Cell::from(Line::from(vec![
                    Span::styled("[Ctrl+L] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Open Logs Folder", Style::default().fg(VERY_LIGHT_AZURE)),
                ])),
            ]),
            Row::new(vec![
                // Empty row for padding
                Cell::from(Span::raw(" ")),
                Cell::from(Span::raw(" ")),
                Cell::from(Span::raw(" ")),
            ]),
            Row::new(vec![
                Cell::from(Line::from(vec![
                    Span::styled("[Q] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Quit", Style::default().fg(VIVID_SKY_BLUE)),
                ])),
                Cell::from(Line::from(vec![
                    Span::styled("[Ctrl+R] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Reset All Nodes", Style::default().fg(EUCALYPTUS)),
                ])),
                Cell::from(""),
            ]),
        ];

        let table_keyboard_shortcuts = Table::new(
            rows_keyboard_shortcuts,
            vec![
                Constraint::Percentage(33),
                Constraint::Percentage(33),
                Constraint::Percentage(33),
            ],
        )
        .block(
            Block::new()
                .borders(Borders::ALL)
                .padding(Padding::uniform(1))
                .title(" Keyboard Shortcuts ")
                .title_style(Style::default().bold()),
        );

        f.render_widget(table_keyboard_shortcuts, layout[2]);

        Ok(())
    }

    fn update(&mut self, action: Action) -> Result<Option<Action>> {
        if let Action::SwitchScene(scene) = action {
            if let Scene::Help = scene {
                self.active = true;
                // make sure we're in navigation mode
                return Ok(Some(Action::SwitchInputMode(InputMode::Navigation)));
            } else {
                self.active = false;
            }
        }
        Ok(None)
    }
}