use std::{convert::TryInto, fs};

use serde_derive::{Deserialize, Serialize};
use toml;
use vulkano::image::SampleCount;

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct LogConfigInput {
    pub mouse_motion: bool,
    pub buttons: bool,
    pub touch: bool,
    pub missing_bindings: bool
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct LogConfig {
    pub vulkan_validation_layers: bool,
    pub mesh_load_info: bool,
    pub input: LogConfigInput
}

impl LogConfig {
    pub fn from_file(path: &str) -> Self {
        toml::from_slice(&fs::read(path).expect("Failed to read log config!")).expect("Failed to parse log config!")
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct RenderConfig {
    pub msaa_samples: u32
}

impl RenderConfig {
    pub fn from_file(path: &str) -> Self {
        toml::from_slice(&fs::read(path).expect("Failed to read render config!")).expect("Failed to parse render config!")
    }

    pub fn get_msaa(&self) -> Option<SampleCount> {
        if self.msaa_samples > 0 { self.msaa_samples.try_into().ok() } else { None }
    }
}