use clap::Parser;
use std::fs;
use std::fs::File;
use std::path::Path;
use tilemake_rs::{Config, InputArguments, process};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    input: String,
    output: String,

    #[arg(short, long)]
    config: Option<String>,

    #[arg(short, long)]
    process: Option<String>,
}

fn main() {
    let args = Args::parse();

    let input_path = Path::new(&args.input);
    let input_file = File::open(&input_path).unwrap();

    let output_path = Path::new(&args.output);
    let output_file = File::create(&output_path).unwrap();

    let mut input_arguments = InputArguments::new(input_file, output_file);

    if let Some(config_arg) = &args.config {
        let config_path = Path::new(config_arg);
        let config_string = fs::read_to_string(config_path).unwrap();
        let config = Config::from_json(&config_string).unwrap();
        input_arguments.set_config(Some(config));
    }

    if let Some(process_arg) = &args.process {
        let process_path = Path::new(process_arg);
        let process_string = fs::read_to_string(process_path).unwrap();
        input_arguments.set_process(Some(process_string));
    }

    process(&input_arguments);
}
