mod raw_audio;
mod xa_audio;

use clap::{Args, ValueEnum};
use std::io::Write;
use tool_helper::{Error, Input};
use xa_audio::{LOW_FREQUENCY, HIGH_FREQUENCY};

#[derive(Args, Clone)]
pub struct Arguments {
    #[clap(short='f', long="frequency", value_enum, value_parser, default_value_t=Frequency::High)]
    frequency:    Frequency,
    #[clap(short='b', long="bitdepth", value_enum, value_parser, default_value_t=SampleDepth::Normal)]
    sample_depth: SampleDepth,
}

#[derive(Copy, Clone, ValueEnum)]
pub enum Frequency {
    High,
    Low,
}

#[derive(Copy, Clone, ValueEnum)]
pub enum SampleDepth {
    Normal,
    High
}

pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(), Error> {
    let prepared_xa_audio = raw_audio::load_as_i16_audio(input, frequency_to_value(args.frequency))?;
    xa_audio::encode(prepared_xa_audio, output, &args)
}

fn frequency_to_value(requested_freq: Frequency) -> u32 {
    match requested_freq {
        Frequency::High => HIGH_FREQUENCY,
        Frequency::Low  => LOW_FREQUENCY,
    }
}