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
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::{utils::centered_rect_fixed, Component};
use crate::{
    action::Action,
    mode::{InputMode, Scene},
    style::{clear_area, EUCALYPTUS, GHOST_WHITE, VIVID_SKY_BLUE},
    tui::Frame,
    widgets::hyperlink::Hyperlink,
};
use color_eyre::eyre::Result;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    prelude::{Rect, *},
    widgets::*,
};

#[derive(Default)]
pub struct HelpPopUp {
    /// Whether the component is active right now, capturing keystrokes + drawing things.
    active: bool,
}

impl Component for HelpPopUp {
    fn handle_key_events(&mut self, key: KeyEvent) -> Result<Vec<Action>> {
        if !self.active {
            return Ok(vec![]);
        }

        let send_back = match key.code {
            KeyCode::Esc => {
                debug!("Got Esc, exiting HelpPopUp");
                vec![Action::SwitchScene(Scene::Home)]
            }
            _ => {
                vec![]
            }
        };

        Ok(send_back)
    }

    fn update(&mut self, action: Action) -> Result<Option<Action>> {
        let send_back = match action {
            Action::SwitchScene(scene) => match scene {
                Scene::HelpPopUp => {
                    self.active = true;
                    Some(Action::SwitchInputMode(InputMode::Entry))
                }
                _ => {
                    self.active = false;
                    None
                }
            },
            _ => None,
        };
        Ok(send_back)
    }

    fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
        if !self.active {
            return Ok(());
        }

        let layer_zero = centered_rect_fixed(50, 12, area);

        let layer_one = Layout::new(
            Direction::Vertical,
            [
                // for the layer 0 border
                Constraint::Length(2),
                // lines
                Constraint::Length(1),
                Constraint::Length(1),
                Constraint::Length(1),
                Constraint::Length(1),
                Constraint::Length(1),
                Constraint::Length(1),
                // dash
                Constraint::Min(1),
                // button
                Constraint::Length(1),
                Constraint::Length(1),
            ],
        )
        .split(layer_zero);
        clear_area(f, layer_zero);

        let pop_up_border = Paragraph::new("").block(
            Block::default()
                .borders(Borders::ALL)
                .title("Get Help")
                .title_style(style::Style::default().fg(EUCALYPTUS))
                .border_style(Style::new().fg(EUCALYPTUS)),
        );

        let line1 = Paragraph::new(" See the quick start guides:");
        f.render_widget(line1.fg(GHOST_WHITE), layer_one[1]);
        let link1 = Hyperlink::new(
            Span::styled(
                " https://docs.autonomi.com/getstarted",
                Style::default().fg(VIVID_SKY_BLUE),
            ),
            "https://docs.autonomi.com/getstarted",
        );
        f.render_widget_ref(link1, layer_one[2]);

        let line2 = Paragraph::new(" Get direct help via Discord:").fg(GHOST_WHITE);
        f.render_widget(line2, layer_one[3]);
        let link2 = Hyperlink::new(
            Span::styled(
                " https://discord.gg/autonomi",
                Style::default().fg(VIVID_SKY_BLUE),
            ),
            "https://discord.gg/autonomi",
        );
        f.render_widget_ref(link2, layer_one[4]);

        let line3 = Paragraph::new(" To join the Beta Rewards Program:").fg(GHOST_WHITE);
        f.render_widget(line3, layer_one[5]);
        let link3 = Hyperlink::new(
            Span::styled(
                " https://autonomi.com/beta",
                Style::default().fg(VIVID_SKY_BLUE),
            ),
            "https://autonomi.com/beta",
        );
        f.render_widget_ref(link3, layer_one[6]);

        let dash = Block::new()
            .borders(Borders::BOTTOM)
            .border_style(Style::new().fg(GHOST_WHITE));
        f.render_widget(dash, layer_one[7]);

        let button = Paragraph::new("  Close [Esc]").style(Style::default().fg(GHOST_WHITE));
        f.render_widget(button, layer_one[8]);

        f.render_widget(pop_up_border, layer_zero);

        Ok(())
    }
}