#![no_std]
#![no_main]

//use defmt::info;
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
//use esp_hal::clock::CpuClock;
use esp_hal::timer::timg::TimerGroup;
use panic_rtt_target as _;

extern crate alloc;

use esp_hal::{
    clock::CpuClock,
    //gpio::Io,
    i2c::master::{I2c, Config},
    //peripherals::Peripherals,
    //prelude::*,
    //rtc_cntl::Rtc,
    //delay::Delay,
};
use embedded_graphics::{
    Drawable,
    draw_target::DrawTarget,
    geometry::Dimensions,
    mono_font::{
        ascii::{FONT_6X10, FONT_10X20},
        MonoTextStyleBuilder,
    },
    pixelcolor::BinaryColor,
    prelude::Point,
    text::{Alignment, Text},
};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
use esp_println::println;

#[esp_hal_embassy::main]
async fn main(spawner: Spawner) {
    // generator version: 0.3.1

    rtt_target::rtt_init_defmt!();

    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
    let peripherals = esp_hal::init(config);

    esp_alloc::heap_allocator!(size: 72 * 1024);

    let timer0 = TimerGroup::new(peripherals.TIMG1);
    esp_hal_embassy::init(timer0.timer0);

    println!("Embassy initialized!");

    let timer1 = TimerGroup::new(peripherals.TIMG0);
    let _init = esp_wifi::init(
        timer1.timer0,
        esp_hal::rng::Rng::new(peripherals.RNG),
        peripherals.RADIO_CLK,
    )
    .unwrap();

    // TODO: Spawn some tasks
    let _ = spawner;

    // Create a new peripheral object with the described wiring
    // and standard I2C clock speed
    // https://docs.rs/esp32-hal/latest/esp32_hal/i2c/struct.I2C.html
    let i2c = I2c::new(
        peripherals.I2C0,
        Config::default(),
    )
    .unwrap()
    .with_sda(peripherals.GPIO21)
    .with_scl(peripherals.GPIO22);

    // Initialize display
    let interface = I2CDisplayInterface::new(i2c);
    let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
        .into_buffered_graphics_mode();
    display.init().unwrap();

    // Specify different text styles
    let text_style = MonoTextStyleBuilder::new()
        .font(&FONT_6X10)
        .text_color(BinaryColor::On)
        .build();
    let text_style_big = MonoTextStyleBuilder::new()
        .font(&FONT_10X20)
        .text_color(BinaryColor::On)
        .build();

    loop {
        // Fill display bufffer with a centered text with two lines (and two text
        // styles)
        Text::with_alignment(
            "esp-hal",
            display.bounding_box().center() + Point::new(0, 0),
            text_style_big,
            Alignment::Center,
        )
        .draw(&mut display).unwrap();
        println!("esp-hal");

        Text::with_alignment(
            "Chip: ESP32",
            display.bounding_box().center() + Point::new(0, 14),
            text_style,
            Alignment::Center,
        )
        .draw(&mut display).unwrap();
        println!("Chip: ESP32...");

        // Write buffer to display
        display.flush().unwrap();
        // Clear display buffer
        let _ = display.clear(BinaryColor::Off);

        // Wait 1 seconds
        Timer::after(Duration::from_secs(1)).await;

        // Write single-line centered text "Haruo World" to buffer
        Text::with_alignment(
            "Haruo World!",
            display.bounding_box().center(),
            text_style_big,
            Alignment::Center,
        )
        .draw(&mut display).unwrap();
        println!("Haruo World!...");

        // Write buffer to display
        display.flush().unwrap();
        // Clear display buffer
        let _ = display.clear(BinaryColor::Off);

        // Wait 3 seconds
        Timer::after(Duration::from_secs(3)).await;
    }

    // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0-beta.0/examples/src/bin
}