mod opts;

use crate::opts::Options;
use clap::Parser;
use std::fs::File;
use thyme::{data::Config, draw::Pattern, file::ThymeFile};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // read the command-line options
    let opts = Options::parse();

    // load the thyme file
    let thyme = {
        let mut f = File::open(opts.file)?;
        ThymeFile::from_stream(&mut f)?
    };

    let config = Config {
        grid_every: opts.grid,
        line_weight: opts.line_weight,
        major_line_weight: opts.major_line_weight,
        grid_size: opts.size,
        font: opts.font.clone(),
    };

    let surf = cairo::SvgSurface::new(
        config.scale(thyme.width),
        config.scale(thyme.height),
        Some(opts.output.unwrap_or_else(|| "output.svg".to_string())),
    )?;

    Pattern { thyme, config }.draw(cairo::Context::new(&surf)?)?;

    surf.finish();

    Ok(())
}