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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use anyhow::anyhow;
use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout},
    style::{Color, Style, Stylize},
    text::Text,
    widgets::{Block, BorderType, Borders, Clear, Padding, Paragraph},
    Frame,
};
use std::{
    error,
    process::exit,
    sync::{atomic::AtomicBool, Arc},
};
use tui_input::Input;

use tracing::error;

use async_channel::{Receiver, Sender};
use futures::FutureExt;
use iwdrs::{agent::Agent, session::Session};

use crate::{adapter::Adapter, config::Config, help::Help, notification::Notification};

pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FocusedBlock {
    Device,
    Station,
    AccessPoint,
    KnownNetworks,
    NewNetworks,
    Help,
    AuthKey,
    AdapterInfos,
    AccessPointInput,
    AccessPointConnectedDevices,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ColorMode {
    Dark,
    Light,
}

#[derive(Debug)]
pub struct App {
    pub running: bool,
    pub focused_block: FocusedBlock,
    pub help: Help,
    pub color_mode: ColorMode,
    pub notifications: Vec<Notification>,
    pub session: Arc<Session>,
    pub adapter: Adapter,
    pub agent_manager: iwdrs::agent::AgentManager,
    pub authentication_required: Arc<AtomicBool>,
    pub passkey_sender: Sender<String>,
    pub cancel_signal_sender: Sender<()>,
    pub passkey_input: Input,
    pub mode: Option<String>,
    pub selected_mode: String,
    pub current_mode: String,
}

pub async fn request_confirmation(
    authentication_required: Arc<AtomicBool>,
    rx_key: Receiver<String>,
    rx_cancel: Receiver<()>,
) -> Result<String, Box<dyn std::error::Error>> {
    authentication_required.store(true, std::sync::atomic::Ordering::Relaxed);

    tokio::select! {
    r = rx_key.recv() =>  {
            match r {
                Ok(key) => Ok(key),
                Err(_) => Err(anyhow!("Failed to receive the key").into()),
            }
        }

    r = rx_cancel.recv() => {
            match r {
                Ok(_) => {
                        Err(anyhow!("Operation Canceled").into())},
                Err(_) => Err(anyhow!("Failed to receive cancel signal").into()),
            }

        }

    }
}

impl App {
    pub async fn new(config: Arc<Config>, mode: Option<String>) -> AppResult<Self> {
        let session = {
            match iwdrs::session::Session::new().await {
                Ok(session) => Arc::new(session),
                Err(e) => {
                    error!("Can not access the iwd service");
                    error!("{}", e.to_string());
                    exit(1);
                }
            }
        };

        let adapter = Adapter::new(session.clone()).await.unwrap();

        let current_mode = adapter.device.mode.clone();

        let selected_mode = String::from("station");

        let (passkey_sender, passkey_receiver) = async_channel::unbounded();
        let (cancel_signal_sender, cancel_signal_receiver) = async_channel::unbounded();

        let authentication_required = Arc::new(AtomicBool::new(false));
        let authentication_required_caller = authentication_required.clone();

        let agent = Agent {
            request_passphrase_fn: Box::new(move || {
                {
                    let auth_clone = authentication_required_caller.clone();
                    request_confirmation(
                        auth_clone,
                        passkey_receiver.clone(),
                        cancel_signal_receiver.clone(),
                    )
                }
                .boxed()
            }),
        };

        let agent_manager = session.register_agent(agent).await?;

        let color_mode = match terminal_light::luma() {
            Ok(luma) if luma > 0.6 => ColorMode::Light,
            Ok(_) => ColorMode::Dark,
            Err(_) => ColorMode::Dark,
        };

        Ok(Self {
            running: true,
            focused_block: FocusedBlock::Device,
            help: Help::new(config),
            color_mode,
            notifications: Vec::new(),
            session,
            adapter,
            agent_manager,
            authentication_required: authentication_required.clone(),
            passkey_sender,
            cancel_signal_sender,
            passkey_input: Input::default(),
            mode,
            selected_mode,
            current_mode,
        })
    }

    pub async fn reset(mode: String) -> AppResult<()> {
        let session = {
            match iwdrs::session::Session::new().await {
                Ok(session) => Arc::new(session),
                Err(e) => {
                    error!("Can not access the iwd service");
                    error!("{}", e.to_string());
                    exit(1);
                }
            }
        };

        let adapter = Adapter::new(session.clone()).await.unwrap();
        adapter.device.set_mode(mode).await?;
        Ok(())
    }

    pub fn render(&self, frame: &mut Frame) {
        let popup_layout = Layout::default()
            .direction(Direction::Vertical)
            .constraints(
                [
                    Constraint::Percentage(45),
                    Constraint::Min(8),
                    Constraint::Percentage(45),
                ]
                .as_ref(),
            )
            .split(frame.size());

        let area = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(
                [
                    Constraint::Length((frame.size().width - 20) / 2),
                    Constraint::Min(40),
                    Constraint::Length((frame.size().width - 20) / 2),
                ]
                .as_ref(),
            )
            .split(popup_layout[1])[1];

        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints(
                [
                    Constraint::Length(1),
                    Constraint::Length(3),
                    Constraint::Length(1),
                    Constraint::Length(1),
                    Constraint::Length(1),
                    Constraint::Length(1),
                ]
                .as_ref(),
            )
            .split(area);

        let (message_area, station_choice_area, ap_choice_area) = (chunks[1], chunks[2], chunks[3]);

        let station_choice_area = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(
                [
                    Constraint::Length(2),
                    Constraint::Fill(1),
                    Constraint::Length(2),
                ]
                .as_ref(),
            )
            .split(station_choice_area)[1];

        let ap_choice_area = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(
                [
                    Constraint::Length(2),
                    Constraint::Fill(1),
                    Constraint::Length(2),
                ]
                .as_ref(),
            )
            .split(ap_choice_area)[1];

        let message_area = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(
                [
                    Constraint::Length(2),
                    Constraint::Fill(1),
                    Constraint::Length(2),
                ]
                .as_ref(),
            )
            .split(message_area)[1];

        let (ap_text, station_text) = match self.selected_mode.as_str() {
            "ap" => match self.current_mode.as_str() {
                "ap" => (
                    Text::from("  Access Point (current)"),
                    Text::from("   Station"),
                ),
                "station" => (
                    Text::from("  Access Point"),
                    Text::from("   Station (current)"),
                ),
                _ => (Text::from("  Access Point"), Text::from("   Station")),
            },
            "station" => match self.current_mode.as_str() {
                "ap" => (
                    Text::from("   Access Point (current)"),
                    Text::from("  Station"),
                ),
                "station" => (
                    Text::from("   Access Point"),
                    Text::from("  Station (current)"),
                ),
                _ => (Text::from("  Access Point"), Text::from("   Station")),
            },
            _ => panic!("unknwon mode"),
        };

        let message = Paragraph::new("Choose a mode: ")
            .alignment(Alignment::Center)
            .style(Style::default().fg(Color::White))
            .block(Block::new().padding(Padding::uniform(1)));

        let station_choice = Paragraph::new(station_text)
            .style(Style::default().fg(Color::White))
            .block(Block::new().padding(Padding::horizontal(4)));

        let ap_choice = Paragraph::new(ap_text)
            .style(Style::default().fg(Color::White))
            .block(Block::new().padding(Padding::horizontal(4)));

        frame.render_widget(Clear, area);

        frame.render_widget(
            Block::new()
                .borders(Borders::ALL)
                .border_type(BorderType::Thick)
                .style(Style::default().green())
                .border_style(Style::default().fg(Color::Green)),
            area,
        );
        frame.render_widget(message, message_area);
        frame.render_widget(ap_choice, ap_choice_area);
        frame.render_widget(station_choice, station_choice_area);
    }

    pub async fn send_passkey(&mut self) -> AppResult<()> {
        let passkey: String = self.passkey_input.value().into();
        self.passkey_sender.send(passkey).await?;
        self.authentication_required
            .store(false, std::sync::atomic::Ordering::Relaxed);
        self.passkey_input.reset();
        Ok(())
    }

    pub async fn cancel_auth(&mut self) -> AppResult<()> {
        self.cancel_signal_sender.send(()).await?;
        self.authentication_required
            .store(false, std::sync::atomic::Ordering::Relaxed);
        self.passkey_input.reset();
        Ok(())
    }

    pub async fn tick(&mut self) -> AppResult<()> {
        self.notifications.retain(|n| n.ttl > 0);
        self.notifications.iter_mut().for_each(|n| n.ttl -= 1);

        self.adapter.refresh().await?;

        Ok(())
    }

    pub fn quit(&mut self) {
        self.running = false;
    }
}