#![feature(slice_group_by)]
use std::io;

use clap::Parser;
use command_line_interface::{Args, Command};
use subcommands::{add::add, list::list};
use types::{config::GradesConfigFile, data::GradesDataFile};
use xdg::BaseDirectories;

pub mod command_line_interface;

pub mod subcommands;
pub mod types;

const PREFIX: &str = "grades";
const DATA: &str = "grades.ron";
const CONFIG: &str = "config.yaml";

fn main() -> io::Result<()> {
    let args = Args::parse();
    let xdg_dirs = BaseDirectories::with_prefix(PREFIX)?;
    let data_file = xdg_dirs.place_data_file(DATA)?;
    let config_file = xdg_dirs.place_config_file(CONFIG)?;

    GradesDataFile::data_init(&data_file);
    GradesConfigFile::config_check(&config_file);

    match &args.cli {
        Some(Command::Add {
            class,
            grade,
            grade_type,
            message,
            unit,
        }) => {
            add(
                class,
                *grade,
                grade_type,
                message,
                unit,
                &data_file,
                &config_file,
            );
        }
        Some(Command::List { class, list_type }) => {
            list(class, list_type, &data_file, &config_file);
        }
        //Some(Command::Config { class }) => {
        //    todo!()
        //}
        None => {}
    }
    Ok(())
}
