use std::{collections::HashMap, net::SocketAddr};

use serde::Deserialize;
use trigger_sv::config::{ConfigurationLoadError, TomlConfig};

#[derive(Deserialize)]
pub struct NetworkSetting {
    pub http_addr: SocketAddr,
}

#[derive(Deserialize)]
pub struct RegionSetting {
    pub name: String,
    pub title: String,
    pub ping_url: String,
    pub dispatch_url: String,
    pub biz: String,
    pub env: u8,
    pub area: u8,
}

#[derive(Deserialize)]
pub struct BoundRegionSetting {
    pub name: String,
    pub title: String,
    pub addr: String,
    pub port: u16,
    pub is_kcp: bool,
    pub seed: String,
}

#[derive(Deserialize)]
pub struct DispatchConfig {
    pub network: NetworkSetting,
    #[serde(rename = "region")]
    pub regions: Vec<RegionSetting>,
    pub bound_server: BoundRegionSetting,
}

#[derive(Deserialize)]
pub struct ResVersionConfig {
    pub design_data_url: String,
    pub design_data_revision: String,
    pub design_data_files: String,
    pub game_res_url: String,
    pub game_res_branch: String,
    pub game_audio_revision: String,
    pub game_res_revision: String,
    pub game_res_files: String,
    pub silence_url: String,
    pub silence_revision: String,
    pub silence_files: String,
}

impl ResVersionConfig {
    pub fn load_version_map_from_file(
        path: &str,
    ) -> Result<HashMap<String, Self>, ConfigurationLoadError> {
        Ok(toml::from_str(&std::fs::read_to_string(path)?)?)
    }
}

impl TomlConfig for DispatchConfig {
    const DEFAULT_TOML: &str = include_str!("../dispatch.default.toml");
}
