#![allow(missing_docs)]
//! Wi-Fi enabled 4-character LED panel clock (12x4 pixels) with captive-portal setup.
//!
//! This example mirrors the WiFi/clock state machine from `clock_servos.rs` but drives a
//! 12x4 LED panel on GPIO3 instead of servos. The reset button is on GPIO13.

#![no_std]
#![no_main]
#![cfg(feature = "wifi")]
#![allow(clippy::future_not_send, reason = "single-threaded")]

use core::convert::Infallible;

use defmt::info;
use defmt_rtt as _;
use device_envoy::button::{PressDuration, PressedTo};
use device_envoy::button_watch;
use device_envoy::clock_sync::{
    ClockSync, ClockSyncStatic, ONE_DAY, ONE_MINUTE, ONE_SECOND, h12_m_s,
};
use device_envoy::flash_array::FlashArray;
use device_envoy::led_strip::Current;
use device_envoy::led_strip::Gamma;
use device_envoy::led_strip::colors;
use device_envoy::led2d;
use device_envoy::led2d::Frame2d;
use device_envoy::led2d::Led2dFont;
use device_envoy::led2d::layout::LedLayout;
use device_envoy::wifi_auto::fields::{TimezoneField, TimezoneFieldStatic};
use device_envoy::wifi_auto::{WifiAuto, WifiAutoEvent};
use device_envoy::{Error, Result};
use embassy_executor::Spawner;
use embassy_futures::select::{Either, select};
use embassy_time::Duration;
use heapless::String;
use panic_probe as _;
use smart_leds::RGB8;

// Single 12x4 panel wired serpentine column-major.
const LED_LAYOUT_12X4: LedLayout<48, 12, 4> = LedLayout::serpentine_column_major();

led2d! {
    Led12x4 {
        pio: PIO0,
        pin: PIN_3,
        dma: DMA_CH1,
        led_layout: LED_LAYOUT_12X4,
        max_current: Current::Milliamps(500),
        gamma: Gamma::Linear,
        max_frames: 32,
        font: Led2dFont::Font3x4Trim,
    }
}

const FAST_MODE_SPEED: f32 = 720.0;
const CONNECTING_COLOR: RGB8 = colors::SADDLE_BROWN;
const DIGIT_COLORS: [RGB8; 4] = [colors::NAVY, colors::GREEN, colors::TEAL, colors::MAROON];
const EDIT_COLORS: [RGB8; 4] = [
    colors::FIREBRICK,
    colors::DARK_ORANGE,
    colors::TEAL,
    colors::MAROON,
];

button_watch! {
    ButtonWatch13 {
        pin: PIN_13,
    }
}

#[embassy_executor::main]
pub async fn main(spawner: Spawner) -> ! {
    let err = inner_main(spawner).await.unwrap_err();
    core::panic!("{err}");
}

async fn inner_main(spawner: Spawner) -> Result<Infallible> {
    info!("Starting Wi-Fi 12x4 LED clock (WifiAuto)");
    let p = embassy_rp::init(Default::default());

    // Use two blocks of flash storage: Wi-Fi credentials + timezone
    let [wifi_credentials_flash_block, timezone_flash_block] = FlashArray::<2>::new(p.FLASH)?;

    // Define HTML to ask for timezone on the captive portal.
    static TIMEZONE_FIELD_STATIC: TimezoneFieldStatic = TimezoneField::new_static();
    let timezone_field = TimezoneField::new(&TIMEZONE_FIELD_STATIC, timezone_flash_block);

    // Set up Wifi via a captive portal. The button pin is used to reset stored credentials.
    let wifi_auto = WifiAuto::new(
        p.PIN_23,  // CYW43 power
        p.PIN_24,  // CYW43 clock
        p.PIN_25,  // CYW43 chip select
        p.PIN_29,  // CYW43 data pin
        p.PIO1,    // CYW43 PIO interface (swapped to show PIO not hardcoded)
        p.DMA_CH0, // CYW43 DMA channel
        wifi_credentials_flash_block,
        p.PIN_13, // Reset button pin
        PressedTo::Ground,
        "www.picoclock.net", // Captive-portal SSID
        [timezone_field],    // Custom fields to ask for
        spawner,
    )?;

    // Set up the 12x4 LED display on GPIO3.
    let led12x4 = Led12x4::new(p.PIN_3, p.PIO0, p.DMA_CH1, spawner)?;

    // Connect Wi-Fi, using the LED panel for status.
    let led12x4_ref = &led12x4;
    let (stack, button) = wifi_auto
        .connect(|event| {
            let led12x4_ref = led12x4_ref;
            async move {
                match event {
                    WifiAutoEvent::CaptivePortalReady => {
                        info!("WiFi: captive portal ready, displaying JOIN");
                        show_portal_ready(led12x4_ref).await?;
                    }
                    WifiAutoEvent::Connecting {
                        try_index,
                        try_count,
                    } => {
                        info!("WiFi: connecting (attempt {}/{})", try_index + 1, try_count);
                        show_connecting(led12x4_ref, try_index, try_count).await?;
                    }
                    WifiAutoEvent::ConnectionFailed => {
                        info!("WiFi: connection failed, displaying FAIL, device will reset");
                        show_connection_failed(led12x4_ref).await?;
                    }
                }
                Ok(())
            }
        })
        .await?;

    info!("WiFi: connected successfully, displaying DONE");
    show_connected(&led12x4).await?;

    // Convert the Button from WifiAuto into a ButtonWatch for background monitoring
    let button_watch13 = ButtonWatch13::from_button(button, spawner)?;

    // Read the timezone offset, an extra field that WiFi portal saved to flash.
    let offset_minutes = timezone_field
        .offset_minutes()?
        .ok_or(Error::MissingCustomWifiAutoField)?;

    // Create a ClockSync device that knows its timezone offset.
    static CLOCK_SYNC_STATIC: ClockSyncStatic = ClockSync::new_static();
    let clock_sync = ClockSync::new(
        &CLOCK_SYNC_STATIC,
        stack,
        offset_minutes,
        Some(ONE_MINUTE),
        spawner,
    );

    // Start in HH:MM mode
    let mut state = State::HoursMinutes { speed: 1.0 };
    loop {
        state = match state {
            State::HoursMinutes { speed } => {
                state
                    .execute_hours_minutes(speed, &clock_sync, button_watch13, &led12x4)
                    .await?
            }
            State::MinutesSeconds => {
                state
                    .execute_minutes_seconds(&clock_sync, button_watch13, &led12x4)
                    .await?
            }
            State::EditOffset => {
                state
                    .execute_edit_offset(&clock_sync, button_watch13, &timezone_field, &led12x4)
                    .await?
            }
        };
    }
}

// State machine for 24x4 LED clock display modes and transitions.

/// Display states for the 24x4 LED clock.
#[derive(Debug, defmt::Format, Clone, Copy, PartialEq)]
pub enum State {
    HoursMinutes { speed: f32 },
    MinutesSeconds,
    EditOffset,
}

impl State {
    async fn execute_hours_minutes(
        self,
        speed: f32,
        clock_sync: &ClockSync,
        button_watch13: &ButtonWatch13,
        led12x4: &Led12x4,
    ) -> Result<Self> {
        clock_sync.set_speed(speed).await;
        let (hours, minutes, _) = h12_m_s(&clock_sync.now_local());
        show_hours_minutes(led12x4, hours, minutes).await?;
        clock_sync.set_tick_interval(Some(ONE_MINUTE)).await;
        loop {
            match select(
                button_watch13.wait_for_press_duration(),
                clock_sync.wait_for_tick(),
            )
            .await
            {
                // Button pushes
                Either::First(press_duration) => {
                    info!(
                        "HoursMinutes: Button press detected: {:?}, speed_bits={}",
                        press_duration,
                        speed.to_bits()
                    );
                    match (press_duration, speed.to_bits()) {
                        (PressDuration::Short, bits) if bits == 1.0f32.to_bits() => {
                            info!("HoursMinutes -> MinutesSeconds");
                            return Ok(Self::MinutesSeconds);
                        }
                        (PressDuration::Short, _) => {
                            info!("HoursMinutes: Resetting speed to 1.0");
                            return Ok(Self::HoursMinutes { speed: 1.0 });
                        }
                        (PressDuration::Long, _) => {
                            info!("HoursMinutes -> EditOffset");
                            return Ok(Self::EditOffset);
                        }
                    }
                }
                // Clock tick
                Either::Second(tick) => {
                    let (hours, minutes, _) = h12_m_s(&tick.local_time);
                    show_hours_minutes(led12x4, hours, minutes).await?;
                }
            }
        }
    }

    async fn execute_minutes_seconds(
        self,
        clock_sync: &ClockSync,
        button_watch13: &ButtonWatch13,
        led12x4: &Led12x4,
    ) -> Result<Self> {
        clock_sync.set_speed(1.0).await;
        let (_, minutes, seconds) = h12_m_s(&clock_sync.now_local());
        show_minutes_seconds(led12x4, minutes, seconds).await?;
        clock_sync.set_tick_interval(Some(ONE_SECOND)).await;
        loop {
            match select(
                button_watch13.wait_for_press_duration(),
                clock_sync.wait_for_tick(),
            )
            .await
            {
                // Button pushes
                Either::First(press_duration) => {
                    info!(
                        "MinutesSeconds: Button press detected: {:?}",
                        press_duration
                    );
                    match press_duration {
                        PressDuration::Short => {
                            info!("MinutesSeconds -> HoursMinutes (fast)");
                            return Ok(Self::HoursMinutes {
                                speed: FAST_MODE_SPEED,
                            });
                        }
                        PressDuration::Long => {
                            info!("MinutesSeconds -> EditOffset");
                            return Ok(Self::EditOffset);
                        }
                    }
                }
                // Clock tick
                Either::Second(tick) => {
                    let (_, minutes, seconds) = h12_m_s(&tick.local_time);
                    show_minutes_seconds(led12x4, minutes, seconds).await?;
                }
            }
        }
    }

    async fn execute_edit_offset(
        self,
        clock_sync: &ClockSync,
        button_watch13: &ButtonWatch13,
        timezone_field: &TimezoneField,
        led12x4: &Led12x4,
    ) -> Result<Self> {
        info!("Entering edit offset mode");
        clock_sync.set_speed(1.0).await;

        // Blink current hours and minutes with edit color accent.
        let (hours, minutes, _) = h12_m_s(&clock_sync.now_local());
        show_hours_minutes_indicator(led12x4, hours, minutes).await?;

        // Get the current offset minutes from clock (source of truth)
        let mut offset_minutes = clock_sync.offset_minutes();
        info!("Current offset: {} minutes", offset_minutes);

        clock_sync.set_tick_interval(None).await; // Disable ticks in edit mode
        loop {
            info!("Waiting for button press in edit mode");
            match button_watch13.wait_for_press_duration().await {
                PressDuration::Short => {
                    info!("Short press detected - incrementing offset");
                    // Increment the offset by 1 hour
                    offset_minutes += 60;
                    const ONE_DAY_MINUTES: i32 = ONE_DAY.as_secs() as i32 / 60;
                    if offset_minutes >= ONE_DAY_MINUTES {
                        offset_minutes -= ONE_DAY_MINUTES;
                    }
                    clock_sync.set_offset_minutes(offset_minutes).await;
                    info!("New offset: {} minutes", offset_minutes);

                    // Update display (atomic already updated, can use now_local)
                    let (hours, minutes, _) = h12_m_s(&clock_sync.now_local());
                    info!(
                        "Updated time after offset change: {:02}:{:02}",
                        hours, minutes
                    );
                    show_hours_minutes_indicator(led12x4, hours, minutes).await?;
                }
                PressDuration::Long => {
                    info!("Long press detected - saving and exiting edit mode");
                    // Save to flash and exit edit mode
                    timezone_field.set_offset_minutes(offset_minutes)?;
                    info!("Offset saved to flash: {} minutes", offset_minutes);
                    return Ok(Self::HoursMinutes { speed: 1.0 });
                }
            }
        }
    }
}

// Display helper functions for the 12x4 LED clock

async fn show_portal_ready(led12x4: &Led12x4) -> Result<()> {
    let on_frame = text_frame(led12x4, "JOIN", &DIGIT_COLORS)?;
    led12x4.animate([
        (on_frame, Duration::from_millis(700)),
        (Frame2d::new(), Duration::from_millis(300)),
    ])
}

async fn show_connecting(led12x4: &Led12x4, try_index: u8, _try_count: u8) -> Result<()> {
    let clockwise = try_index % 2 == 0;
    const FRAME_DURATION: Duration = Duration::from_millis(90);
    let animation = perimeter_chase_animation(clockwise, CONNECTING_COLOR, FRAME_DURATION)?;
    led12x4.animate(animation)
}

async fn show_connected(led12x4: &Led12x4) -> Result<()> {
    led12x4.write_text("DONE", &DIGIT_COLORS).await
}

async fn show_connection_failed(led12x4: &Led12x4) -> Result<()> {
    led12x4.write_text("FAIL", &DIGIT_COLORS).await
}

async fn show_hours_minutes(led12x4: &Led12x4, hours: u8, minutes: u8) -> Result<()> {
    let (hours_tens, hours_ones) = hours_digits(hours);
    let (minutes_tens, minutes_ones) = two_digit_chars(minutes);
    let text = chars_to_text([hours_tens, hours_ones, minutes_tens, minutes_ones]);
    led12x4.write_text(text.as_str(), &DIGIT_COLORS).await
}

async fn show_hours_minutes_indicator(led12x4: &Led12x4, hours: u8, minutes: u8) -> Result<()> {
    let (hours_tens, hours_ones) = hours_digits(hours);
    let (minutes_tens, minutes_ones) = two_digit_chars(minutes);
    let text = chars_to_text([hours_tens, hours_ones, minutes_tens, minutes_ones]);
    led12x4.write_text(text.as_str(), &EDIT_COLORS).await
}

async fn show_minutes_seconds(led12x4: &Led12x4, minutes: u8, seconds: u8) -> Result<()> {
    let (minutes_tens, minutes_ones) = two_digit_chars(minutes);
    let (seconds_tens, seconds_ones) = two_digit_chars(seconds);
    let text = chars_to_text([minutes_tens, minutes_ones, seconds_tens, seconds_ones]);
    led12x4.write_text(text.as_str(), &DIGIT_COLORS).await
}

const PERIMETER_LENGTH: usize = (Led12x4::WIDTH * 2) + ((Led12x4::HEIGHT - 2) * 2);

fn chars_to_text(chars: [char; 4]) -> String<4> {
    let mut text = String::new();
    for ch in chars {
        text.push(ch).expect("text buffer has capacity");
    }
    text
}

fn text_frame(led12x4: &Led12x4, text: &str, colors: &[RGB8]) -> Result<Frame2d<12, 4>> {
    let mut frame = Frame2d::new();
    led12x4.write_text_to_frame(text, colors, &mut frame)?;
    Ok(frame)
}

fn perimeter_chase_animation(
    clockwise: bool,
    color: RGB8,
    duration: Duration,
) -> Result<heapless::Vec<(Frame2d<12, 4>, Duration), PERIMETER_LENGTH>> {
    assert!(
        duration.as_micros() > 0,
        "perimeter animation duration must be positive"
    );
    let coordinates = perimeter_coordinates(clockwise);
    let mut frames = heapless::Vec::new();
    for frame_index in 0..PERIMETER_LENGTH {
        let mut frame = Frame2d::new();
        let (x_index, y_index) = coordinates[frame_index];
        frame[(x_index, y_index)] = color;
        frames
            .push((frame, duration))
            .map_err(|_| Error::FormatError)?;
    }
    Ok(frames)
}

fn perimeter_coordinates(clockwise: bool) -> [(usize, usize); PERIMETER_LENGTH] {
    let mut coordinates = [(0_usize, 0_usize); PERIMETER_LENGTH];
    let mut write_index = 0;
    let mut push = |x_index: usize, y_index: usize| {
        coordinates[write_index] = (x_index, y_index);
        write_index += 1;
    };

    for x_index in 0..Led12x4::WIDTH {
        push(x_index, 0);
    }
    for y_index in 1..Led12x4::HEIGHT {
        push(Led12x4::WIDTH - 1, y_index);
    }
    for x_index in (0..(Led12x4::WIDTH - 1)).rev() {
        push(x_index, Led12x4::HEIGHT - 1);
    }
    for y_index in (1..(Led12x4::HEIGHT - 1)).rev() {
        push(0, y_index);
    }

    debug_assert_eq!(write_index, PERIMETER_LENGTH);

    if clockwise {
        coordinates
    } else {
        let mut reversed = [(0_usize, 0_usize); PERIMETER_LENGTH];
        for (reverse_index, &(x_index, y_index)) in coordinates.iter().enumerate() {
            reversed[PERIMETER_LENGTH - 1 - reverse_index] = (x_index, y_index);
        }
        reversed
    }
}

#[inline]
fn two_digit_chars(value: u8) -> (char, char) {
    assert!(value < 100);
    (tens_digit(value), ones_digit(value))
}

#[inline]
fn hours_digits(hours: u8) -> (char, char) {
    assert!(hours >= 1 && hours <= 12);
    if hours >= 10 {
        ('1', ones_digit(hours))
    } else {
        (' ', ones_digit(hours))
    }
}

#[inline]
#[expect(
    clippy::arithmetic_side_effects,
    clippy::integer_division_remainder_used,
    reason = "Value < 100 ensures division is safe"
)]
fn tens_digit(value: u8) -> char {
    ((value / 10) + b'0') as char
}

#[inline]
#[expect(
    clippy::arithmetic_side_effects,
    clippy::integer_division_remainder_used,
    reason = "Value < 100 ensures division is safe"
)]
fn ones_digit(value: u8) -> char {
    ((value % 10) + b'0') as char
}
