use crate::config::Config;
use iwdrs::modes::Mode;
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Direction, Layout, Margin},
    style::{Style, Stylize},
    text::{Line, Span, Text},
    widgets::{Block, BorderType, Borders, Clear, Padding, Paragraph},
};
use std::sync::Arc;

#[derive(Debug)]
pub struct Reset {
    pub enable: bool,
    pub selected_mode: Mode,
    pub current_mode: Mode,
}

impl Reset {
    pub fn new(current_mode: Mode) -> Self {
        Self {
            enable: false,
            selected_mode: Mode::Station,
            current_mode,
        }
    }

    pub fn render(&self, frame: &mut Frame, config: Arc<Config>) {
        let popup_layout = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Fill(1),
                Constraint::Length(10),
                Constraint::Fill(1),
            ])
            .flex(ratatui::layout::Flex::SpaceBetween)
            .split(frame.area());

        let area = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Fill(1),
                Constraint::Length(50),
                Constraint::Fill(1),
            ])
            .flex(ratatui::layout::Flex::SpaceBetween)
            .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),
                    Constraint::Length(1),
                ]
                .as_ref(),
            )
            .split(area);

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

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

        let ap_choice_area = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Length(2),
                Constraint::Fill(1),
                Constraint::Length(2),
            ])
            .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 {
            Mode::Ap => match self.current_mode {
                Mode::Ap => (
                    Text::from("→ Access Point (current)"),
                    Text::from("  Station"),
                ),
                Mode::Station => (
                    Text::from("→ Access Point"),
                    Text::from("  Station (current)"),
                ),
            },
            Mode::Station => match self.current_mode {
                Mode::Ap => (
                    Text::from("  Access Point (current)"),
                    Text::from("→ Station"),
                ),
                Mode::Station => (
                    Text::from("  Access Point"),
                    Text::from("→ Station (current)"),
                ),
            },
        };

        let message = Paragraph::new("Select the desired mode:")
            .alignment(Alignment::Center)
            .style(Style::default().fg(config.theme.text_color))
            .block(Block::new().padding(Padding::uniform(1)));

        let station_choice = Paragraph::new(station_text)
            .style(Style::default().fg(config.theme.text_color))
            .block(Block::new().padding(Padding::horizontal(10)));

        let ap_choice = Paragraph::new(ap_text)
            .style(Style::default().fg(config.theme.text_color))
            .block(Block::new().padding(Padding::horizontal(10)));

        let help = Text::from(Line::from(vec![
            Span::from("k,↑").bold(),
            Span::from("  Up"),
            Span::from(" | "),
            Span::from("j,↓").bold(),
            Span::from("  Down"),
            Span::from(" | "),
            Span::from("Enter").bold(),
            Span::from(" Confirm"),
        ]))
        .style(Style::default().blue())
        .centered();

        frame.render_widget(Clear, area);

        frame.render_widget(
            message,
            message_area.inner(Margin {
                horizontal: 1,
                vertical: 0,
            }),
        );
        frame.render_widget(ap_choice, ap_choice_area);
        frame.render_widget(station_choice, station_choice_area);
        frame.render_widget(help, help_area);

        frame.render_widget(
            Block::new()
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(config.theme.border)),
            area,
        );
    }
}
