use std::{ fs, path::Path, time::{ SystemTime, UNIX_EPOCH } };

use image::{ buffer, ImageBuffer, Rgb };
use qirust::{
    helper::{
        encode_base64,
        frameqr_to_svg_string,
        generate_frameqr,
        generate_frameqr_buffer,
        generate_image,
        generate_image_buffer,
        generate_svg_string,
        print_qr,
        to_svg_string,
    },
    qrcode::{ Mask, QrCode, QrCodeEcc, Version },
};

fn generate_image_buffer_custom_mask(
    content: &str,
    border: Option<i32>,
    fg_color: Option<Rgb<u8>>,
    bg_color: Option<Rgb<u8>>,
    mask: Option<Mask>
) -> ImageBuffer<Rgb<u8>, Vec<u8>> {
    let border = border.unwrap_or(4);
    let foreground_color = fg_color.unwrap_or(Rgb([0, 0, 0]));
    let background_color = bg_color.unwrap_or(Rgb([255, 255, 255]));
    let errcorlvl = QrCodeEcc::Low;

    let mut outbuffer = vec![0u8; Version::MAX.buffer_len()];
    let mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];

    let qr = QrCode::encode_text(
        content,
        &mut tempbuffer,
        &mut outbuffer,
        errcorlvl,
        Version::MIN,
        Version::MAX,
        mask, // Use the specified mask
        true
    ).unwrap();

    std::mem::drop(tempbuffer);

    let size = (qr.size() + 2 * border) as u32;
    let mut img = ImageBuffer::new(size, size);

    for (x, y, pixel) in img.enumerate_pixels_mut() {
        let qr_x = (x as i32) - border;
        let qr_y = (y as i32) - border;
        *pixel = if qr.get_module(qr_x, qr_y) { foreground_color } else { background_color };
    }

    img
}

fn main() {
    let mut outbuffer = vec![0u8; Version::MAX.buffer_len()];
    let mut tempbuffer = vec![0u8; Version::MAX.buffer_len()];
    let qr = QrCode::encode_text(
        "https://example.com",
        &mut tempbuffer,
        &mut outbuffer,
        QrCodeEcc::High,
        Version::MIN,
        Version::MAX,
        None,
        true
    ).unwrap();

    // // let buffer = generate_frameqr_buffer(
    // //     qr,
    // //     "src/logo.png",
    // //     Some(16),
    // //     None,
    // //     Some(40),
    // //     None,
    // //     Some("square")
    // // );

    // let buffer = generate_image_buffer("HANAHAHAKA", Some(10), None, None, None).expect(
    //     "Failed to generate image buffer"
    // );

    // buffer.save("generated/styled_qr.png").expect("Failed to save image");
    // let svg = generate_svg_string("www.google.com");
    let svg = frameqr_to_svg_string(
        qr,
        "src/logo.png",
        Some(16),
        None,
        Some(5),
        Some(10),
        Some(qirust::helper::FrameStyle::Square)
    );
    println!("{}", svg.unwrap());
}
