mod backend;
use backend::database;

use clap::Clap;

#[derive(Clap)]
#[clap(
    name = clap::crate_name!(),
    version = clap::crate_version!(),
    author = clap::crate_authors!(),
    about = clap::crate_description!(),
)]
struct Opts {
    /// Path to database file. If the file does not exist, it will be created.
    #[clap(required(true))]
    filename: String,
    /// Logging level scales from is the least verbose ("error") to the most verbose ("trace").
    #[clap(long, possible_values(&["error", "warn", "info", "debug", "trace"]),
           default_value("error"))]
    log_level: log::Level,
}

fn main() {
    let opts = Opts::parse();
    log::info!("Log level: {}", &opts.log_level);
    let _db = database::Database::open(&opts.filename);

    // TODO: Use rustyline Config, completer, colors and history.
    let mut editor = rustyline::Editor::<()>::new();
    editor.bind_sequence(
        rustyline::KeyEvent::alt('N'),
        rustyline::Cmd::HistorySearchForward,
    );
    editor.bind_sequence(
        rustyline::KeyEvent::alt('P'),
        rustyline::Cmd::HistorySearchBackward,
    );
    loop {
        let readline = editor.readline(&format!("{}> ", clap::crate_name!()));
        match readline {
            Ok(line) => {
                println!("Command: {}", line);
            }
            Err(rustyline::error::ReadlineError::Interrupted) => {
                println!("CTRL-C");
                break;
            }
            Err(rustyline::error::ReadlineError::Eof) => {
                println!("EOF");
                break;
            }
            Err(err) => {
                panic!("{}", err);
            }
        }
    }
}
