use std::{env, path::Path, process};

mod gnucash;
mod ica_csv;
use crate::ica_csv::parse_csv;

fn print_help() {
    println!("Error: Missing arguments!");
    println!();
    println!("Usage:");
    println!();
    println!("This program requires two arguments:");
    println!("Argument 1: Input CSV file from ICA Banken");
    println!("Argument 2: Output CSV file formatted for GnuCash import");
    println!();
    println!("Example:");
    println!("icabanken2gnucash ica.csv ica-gnucash.csv");
    println!();
    println!("If compiled with `pipe` feature, pipe in and out instead");
    println!("icabanken2gnucash < ica.csv > ica-gnucash.csv");
}

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.is_empty() {
        print_help();
        process::exit(1);
    }
    println!(" Input file: {}", args[1]);
    println!("Output file: {}", args[2]);
    println!("Extra arguments: {}", args[3]);
    if !args[3].is_empty() {
        println!("Importing using 'Utdrag' ordering")
    }

    let input_path = Path::new(&args[1]);
    if let Some(extension_type) = input_path.extension() {
        match extension_type.to_str() {
            Some("csv") => {
                if let Err(err) = parse_csv(args) {
                    println!("Unable to parse CSV: {}", err);
                    process::exit(1);
                } else {
                    println!("Done! CSV Parsed");
                }
            }
            _ => {
                println!("Unsupported file format, CSV supported. Exiting");
                process::exit(1);
            }
        }
    }
}