#![allow(dead_code)]

use clap::Parser;

// use crate::renderer::LuaRenderer;

mod lua;
mod renderer;

use lua::LuaDecoder;

use crate::renderer::prelude::VideoRenderer;

// mod tasks;

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

/// A fictional versioning CLI
#[derive(Debug, Parser)]
#[command(name = "luavid")]
#[command(about = "Tool for scanning Minecraft servers", long_about = None)]
struct Args {
    file: String,
}

fn main() -> Result<()> {
    let args = Args::parse();

    if args.file.is_empty() {
        return Err("File path cannot be empty".into());
    }

    if !std::path::Path::new(&args.file).exists() {
        return Err(format!("File '{}' does not exist", args.file).into());
    }

    let file = std::fs::File::open(&args.file)?;

    let (tasks, config) = LuaDecoder::run(&file)?;

    // println!("config: {:?}", config);
    println!("tasks: {}", tasks.len());

    let mut renderer = VideoRenderer::new(config);

    renderer.process_tasks(tasks);

    // let info = renderer.export_info();
    // println!("Video Info: {:?}", info);

    // Render the complete video
    renderer.render_to_video("output.mp4")?;

    Ok(())
}

// use mlua::{Error, Lua, MultiValue};
// use rustyline::DefaultEditor;

// fn main() {
//     let lua = Lua::new();
//     let mut editor = DefaultEditor::new().expect("Failed to create editor");

//     loop {
//         let mut prompt = "> ";
//         let mut line = String::new();

//         loop {
//             match editor.readline(prompt) {
//                 Ok(input) => line.push_str(&input),
//                 Err(_) => return,
//             }

//             match lua.load(&line).eval::<MultiValue>() {
//                 Ok(values) => {
//                     editor.add_history_entry(line).unwrap();
//                     println!(
//                         "{}",
//                         values
//                             .iter()
//                             .map(|value| format!("{:#?}", value))
//                             .collect::<Vec<_>>()
//                             .join("\t")
//                     );
//                     break;
//                 }
//                 Err(Error::SyntaxError {
//                     incomplete_input: true,
//                     ..
//                 }) => {
//                     // continue reading input and append it to `line`
//                     line.push_str("\n"); // separate input lines
//                     prompt = ">> ";
//                 }
//                 Err(e) => {
//                     eprintln!("error: {}", e);
//                     break;
//                 }
//             }
//         }
//     }
// }
