extern crate serde;
extern crate serde_json;

include!(concat!(env!("OUT_DIR"), "\\serde_types.rs"));

use std::io::prelude::*;
use std::fs::File;
use std::process::exit;

pub trait Settings {
    fn get_socket_port(&self) -> u16;
}

pub struct SettingsImpl {
    pub settings_file_path:String,
    pub settings_data: SettingsData,
}

impl SettingsImpl {
    pub fn 
(&mut self) {
        let mut file: File = match File::open(&self.settings_file_path) {
            Err(e) => {
                println!("Error: {0}. couldn't find file at location: {1}", e, self.settings_file_path);
                exit(1); // TODO: should probably return false, and then have process exit with one on context init failure
            },
            Ok(f) => f,
        };
        let mut json_string = String::new();
        match file.read_to_string(&mut json_string) {
            Err(e) => println!("Error: {0}. couldn't read from file at location: {1}", e, self.settings_file_path),
            Ok(bytes) => println!("bytes_read: {0}, string read:\n{1}", bytes, json_string),
        }
        
        let loaded_settings_data: SettingsData = match serde_json::from_str(json_string.as_ref()) {
            Err(e) => {
                println!("Error {0}, problem serializing into SettingsData from:\n{1}", e, json_string);
                return;
            },
            Ok(s) => s,
        };
        self.settings_data = loaded_settings_data;
    }
}

impl Settings for SettingsImpl {
    fn get_socket_port(&self) -> u16 {
        return self.settings_data.socket_port;
    }
}
init