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
// 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::{InputMode, Scene},
};
use color_eyre::Result;
use crossterm::event::{Event, KeyCode, KeyEvent};
use ratatui::{prelude::*, widgets::*};
use tui_input::{backend::crossterm::EventHandler, Input};

#[derive(Default)]
pub struct Options {
    // state
    show_scene: bool,
    input_mode: InputMode,
    input: Input,
}

impl Component for Options {
    fn handle_key_events(&mut self, key: KeyEvent) -> Result<Vec<Action>> {
        // while in entry mode, keybinds are not captured, so gotta exit entry mode from here
        match key.code {
            KeyCode::Esc => {
                return Ok(vec![Action::SwitchInputMode(InputMode::Navigation)]);
            }
            KeyCode::Down => {
                // self.select_next_input_field();
            }
            KeyCode::Up => {
                // self.select_previous_input_field();
            }
            _ => {}
        }
        self.input.handle_event(&Event::Key(key));
        Ok(vec![])
    }

    fn update(&mut self, action: Action) -> Result<Option<Action>> {
        match action {
            Action::SwitchScene(scene) => match scene {
                Scene::Options => self.show_scene = true,
                _ => self.show_scene = false,
            },
            Action::SwitchInputMode(mode) => self.input_mode = mode,
            _ => {}
        };
        Ok(None)
    }

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

        // index 0 is reserved for tab; 2 is for keybindings
        let layer_zero = Layout::new(
            Direction::Vertical,
            [Constraint::Max(1), Constraint::Min(15), Constraint::Max(3)],
        )
        .split(area);

        // break the index 1 into sub sections
        let layer_one = Layout::new(
            Direction::Vertical,
            [
                Constraint::Length(3),
                Constraint::Length(3),
                Constraint::Length(3),
                Constraint::Length(3),
                Constraint::Length(3),
                Constraint::Length(3),
            ],
        )
        .split(layer_zero[1]);

        let input = Paragraph::new(self.input.value())
            .style(Style::default())
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("Peer MultiAddress"),
            );
        f.render_widget(input, layer_one[0]);
        let input = Paragraph::new(self.input.value())
            .style(Style::default())
            .block(Block::default().borders(Borders::ALL).title("Home Network"));
        f.render_widget(input, layer_one[1]);
        let input = Paragraph::new(self.input.value())
            .style(Style::default())
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("Data dir Path"),
            );
        f.render_widget(input, layer_one[2]);
        let input = Paragraph::new(self.input.value())
            .style(Style::default())
            .block(Block::default().borders(Borders::ALL).title("Log dir Path"));
        f.render_widget(input, layer_one[3]);
        let input = Paragraph::new(self.input.value())
            .style(Style::default())
            .block(Block::default().borders(Borders::ALL).title("Node Version"));
        f.render_widget(input, layer_one[4]);
        let input = Paragraph::new(self.input.value())
            .style(Style::default())
            .block(Block::default().borders(Borders::ALL).title("RPC Address"));
        f.render_widget(input, layer_one[5]);

        Ok(())
    }
}