use crate::feed_api::{FeedApiError, FeedApiResult};
use crate::models::Url;
use crate::password_encryption::PasswordEncryption;
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::io::Read;
use std::path::{Path, PathBuf};

static CONFIG_NAME: &str = "commafeed.json";

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AccountConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    user_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    password: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    url: Option<String>,
    #[serde(skip_serializing)]
    #[serde(skip_deserializing)]
    path: PathBuf,
}

impl AccountConfig {
    pub fn load(config_dir: &Path) -> FeedApiResult<Self> {
        let path = config_dir.join(CONFIG_NAME);
        if path.as_path().exists() {
            let mut contents = String::new();
            tracing::info!(?path, "Attempting to open config file");
            let mut file = File::open(&path).map_err(move |err| {
                tracing::error!("Failed to load config file");
                FeedApiError::IO(err)
            })?;
            file.read_to_string(&mut contents).map_err(move |error| {
                tracing::error!(%error, "Reading content of file failed");
                FeedApiError::IO(error)
            })?;
            let mut config: AccountConfig = serde_json::from_str(&contents)?;

            if let Some(password) = config.password {
                let password = PasswordEncryption::decrypt(&password)?;
                config.password = Some(password);
            }

            config.path = path;

            return Ok(config);
        }

        tracing::info!(?path, "Config file does not exist. Returning empty config");
        Ok(AccountConfig {
            user_name: None,
            password: None,
            url: None,
            path,
        })
    }

    pub fn save(&self) -> FeedApiResult<()> {
        let mut config = self.clone();
        if let Some(password) = config.get_password() {
            config.set_password(&PasswordEncryption::encrypt(&password));
        }
        let data = serde_json::to_string_pretty(&config)?;
        fs::write(&self.path, data).inspect_err(|error| tracing::error!(?self.path, %error, "Failed to write config"))?;
        Ok(())
    }

    pub fn delete(&self) -> FeedApiResult<()> {
        fs::remove_file(&self.path)?;
        Ok(())
    }

    pub fn get_user_name(&self) -> Option<String> {
        self.user_name.clone()
    }

    pub fn set_user_name(&mut self, user_name: &str) {
        self.user_name = Some(user_name.to_owned());
    }

    pub fn get_password(&self) -> Option<String> {
        self.password.clone()
    }

    pub fn set_password(&mut self, password: &str) {
        self.password = Some(password.to_owned());
    }

    pub fn get_url(&self) -> Option<String> {
        self.url.clone()
    }

    pub fn get_icon_base_url(&self) -> Result<Url, FeedApiError> {
        let base_url = self.url.clone().ok_or(FeedApiError::Login)?;
        let base_url = Url::parse(base_url.trim_end_matches("rest/"))?;
        Ok(base_url)
    }

    pub fn set_url(&mut self, url: &str) {
        self.url = Some(url.to_owned());
    }
}
