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
// 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::Component;
use crate::{
    action::Action,
    mode::Scene,
    style::{COOL_GREY, EUCALYPTUS, GHOST_WHITE, LIGHT_PERIWINKLE, VERY_LIGHT_AZURE},
};
use color_eyre::eyre::Result;
use ratatui::{prelude::*, widgets::*};

pub struct Footer {
    current_scene: Scene,
    nodes_to_start_configured: bool,
}

impl Footer {
    pub fn new(nodes_to_start_configured: bool) -> Self {
        Self {
            current_scene: Scene::Home,
            nodes_to_start_configured,
        }
    }
}

impl Component for Footer {
    fn update(&mut self, action: Action) -> Result<Option<Action>> {
        match action {
            Action::SwitchScene(scene) => {
                self.current_scene = scene;
            }
            Action::StoreNodesToStart(count) => {
                self.nodes_to_start_configured = count > 0;
            }
            _ => {}
        }
        Ok(None)
    }

    fn draw(&mut self, f: &mut crate::tui::Frame<'_>, area: Rect) -> Result<()> {
        let layer_zero = Layout::new(
            Direction::Vertical,
            [
                // for the rest of the home scene
                Constraint::Min(1),
                // our footer
                Constraint::Max(5),
            ],
        )
        .split(area);
        let border = Paragraph::new("").block(
            Block::default()
                .title("Key Commands")
                .borders(Borders::ALL)
                .border_style(Style::default().fg(COOL_GREY)),
        );

        let layer_one = Layout::new(
            Direction::Vertical,
            [
                // border
                Constraint::Length(1),
                // line1
                Constraint::Length(2),
                // line2
                Constraint::Length(1),
                // border
                Constraint::Length(1),
            ],
        )
        .split(layer_zero[1]);

        let text_style = if self.nodes_to_start_configured {
            Style::default().fg(EUCALYPTUS)
        } else {
            Style::default().fg(LIGHT_PERIWINKLE)
        };

        let command_style = if self.nodes_to_start_configured {
            Style::default().fg(GHOST_WHITE)
        } else {
            Style::default().fg(LIGHT_PERIWINKLE)
        };

        let (line1, line2) = match self.current_scene {
            Scene::Home
            | Scene::BetaProgramme
            | Scene::HelpPopUp
            | Scene::ManageNodes
            | Scene::ResetPopUp => {
                let line1 = Line::from(vec![
                    Span::styled(" [Ctrl+S] ", command_style),
                    Span::styled("Start all Nodes       ", text_style),
                    Span::styled("[Ctrl+X] ", command_style),
                    Span::styled("Stop all Nodes          ", text_style),
                    Span::styled("[H] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Help", Style::default().fg(EUCALYPTUS)),
                ]);

                let line2 = Line::from(vec![
                    Span::styled(" [Ctrl+G] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Manage Nodes          ", Style::default().fg(EUCALYPTUS)),
                    Span::styled("[Ctrl+B] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled(
                        "Beta Rewards Program    ",
                        Style::default().fg(VERY_LIGHT_AZURE),
                    ),
                    Span::styled("[Q] ", Style::default().fg(GHOST_WHITE)),
                    Span::styled("Quit", Style::default().fg(EUCALYPTUS)),
                ]);

                (line1, line2)
            }
            Scene::Options => (Line::from("none"), Line::from("none")),
        };

        f.render_widget(Paragraph::new(line1), layer_one[1]);
        f.render_widget(Paragraph::new(line2), layer_one[2]);
        // render the border after the text so we don't get white spaces at the left border
        f.render_widget(border, layer_zero[1]);

        Ok(())
    }
}