//! Provides error handling to the Argentum Finance engine.

use std::process::exit;

use colored::*;

/// Enumerates the errors thrown by Argentum.
pub enum Error {
    InvalidMonth (u32),
    InvalidBinary,
    CouldNotReadFile,
    CouldNotWriteToFile,
    CouldNotCompile,
    InvalidDecryptionKey,
    InvalidDate,
    InvalidId,
    InvalidText,
    InvalidSelection,
    InvalidDollarAmount (String),
    InvalidSubcommand,
    InvalidConfig (String),
    KeysDontMatch,
}

impl Error {
    pub fn throw(&self) -> ! {
        let message: String = match self {
            Error::InvalidMonth (n) => format!("invalid month number {}", n),
            Error::InvalidBinary => format!("invalid binary input"),
            Error::CouldNotReadFile => format!("could not read input file"),
            Error::CouldNotWriteToFile => format!("could not write to file"),
            Error::CouldNotCompile => format!("could not compile TeX"),
            Error::InvalidDecryptionKey => format!("could not read file, try decrypting with the correct key"),
            Error::InvalidDate => format!("invalid date input"),
            Error::InvalidId => format!("invalid ID input"),
            Error::InvalidText => format!("invalid text input"),
            Error::InvalidSelection => format!("invalid selection"),
            Error::InvalidDollarAmount (a) => format!("could not parse {} as a dollar amount", a),
            Error::InvalidSubcommand => format!("invalid subcommand, type `ag help` for help"),
            Error::InvalidConfig (c) => format!("invalid configuration data {}", c),
            Error::KeysDontMatch => format!("encryption keys do not match"),
        };

        println!("{}: {}", "Error".bold().red(), message);
        println!("Argentum exiting");

        exit(0);
    }
}