/* Copyright 2021 Daniel Mowitz
 * This file is part of Mention2Mail.
 *
 * Mention2Mail is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Mention2Mail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Mention2Mail.  If not, see <https://www.gnu.org/licenses/>.
 */ 

use std::{
	path::PathBuf,
	io::Read,
	fs::read_dir,
};
use toml::{
	value::Value,
	map::Map,
};

fn parse_toml_to_table(path: PathBuf)
	-> Result<Map<String, Value>, Box<dyn std::error::Error>> {
	// I once read that this multiple let style 
	// is the "rusty" way to do things.
	// Personally, I find this specific instance to be somewhat sketchy.
	let mut config = String::new();
	std::fs::File::open(path)?
		.read_to_string(&mut config)?;
	let mut config = config.parse::<Value>()?;
	// The important thing here is that config is a mut Table in the end.
	Ok(config.as_table_mut().unwrap().clone())
}

fn merge_config_maps(mut receiver: Map<String, Value>, donor: Map<String, Value>) 
	-> Result<Map<String, Value>, Box<dyn std::error::Error>> {
	for key in donor.keys() {
		if receiver.contains_key(&key.clone()) {
			receiver[&key.clone()] = donor[key].clone();
		} else {
			receiver.insert(key.clone(), donor[key].clone());
		}
	}
	Ok(receiver)
}

fn parse_server_dir(mut config: Value, server_config_path: PathBuf) 
	-> Result<Value, Box<dyn std::error::Error>> {
	let mut config = config.as_table_mut().unwrap().clone();
	if server_config_path.is_dir() {
		for entry in read_dir(server_config_path)? {
			let entry = entry?;
			match entry.file_name().to_str().ok_or("Could not read file name")? {
				"config.toml" => {
					let server_config = parse_toml_to_table(entry.path())?;
					config = merge_config_maps(config, server_config)?;
				},
				"clients.toml" => {
					let client_config = parse_toml_to_table(entry.path())?;
					config = merge_config_maps(config, client_config)?;
				},
				"channels.toml" => {
					let channels = Value::from(parse_toml_to_table(entry.path())?);
					if config.contains_key(&"channels".to_owned()) {
						config[&"channels".to_owned()] = channels;
					} else {
						config.insert("channels".to_owned(), channels);
					}
				},
				_ => (),
			}
		}
	}

	Ok(Value::from(config))
}

/// parses a toml config file and sets some default values
/// if the corresponding fields are not set.
// TODO: Document config file options once they are stable.
pub fn get_main_config(config_path: PathBuf) 
	-> Result<Value, Box<dyn std::error::Error>> {
	let mut config = parse_toml_to_table(config_path)?;	
	
	if config.get("port") == None {
		config.insert(String::from("port"), Value::from("6697"));
	}

	if config.get("tls") == None {
		if config["port"].as_integer() == Some(194) ||
			config["port"].as_integer() == Some(6667) {
			config.insert(String::from("tls"), Value::from(false));
		}else if config["port"].as_integer() == Some(6696) ||
			config["port"].as_integer() == Some(6697) {
			config.insert(String::from("tls"), Value::from(true));
		}
	}
	
	Ok(Value::from(config))
}

pub fn get_server_configs(config: Value, config_path: PathBuf)
	-> Result<Vec<Value>, Box<dyn std::error::Error>> {
	let mut config_vec = vec![];
	if config_path.is_dir() {
		for entry in read_dir(config_path)? {
			let entry = entry?;
			let server_path = entry.path();
			if server_path.is_dir(){
				config_vec.push(parse_server_dir(config.clone(), server_path)?);
			}
		}
	}
	Ok(config_vec)
}