using System;
using System.IO;
using Электронная_Фармация.Properties;

namespace Электронная_Фармация.Classes
{
    /// <summary>
    /// Shared application paths and API settings.
    /// </summary>
    public static class AppConfig
    {
        public const string DefaultApiBaseUrl = "http://195.34.241.84:9988";
        private const string ApiBaseUrlEnvVar = "ELF_API_BASE_URL";

        public static string ApiBaseUrl
        {
            get
            {
                var fromSettings = Settings.Default.ApiBaseUrl;
                if (!string.IsNullOrWhiteSpace(fromSettings))
                {
                    return fromSettings.Trim().TrimEnd('/');
                }

                var fromEnv = Environment.GetEnvironmentVariable(ApiBaseUrlEnvVar);
                if (!string.IsNullOrWhiteSpace(fromEnv))
                {
                    return fromEnv.Trim().TrimEnd('/');
                }

                return DefaultApiBaseUrl;
            }
        }

        public static string SqliteDbPath
        {
            get
            {
                var baseDir = AppDomain.CurrentDomain.BaseDirectory;
                var root = Path.Combine(baseDir, "efClient.db");
                var data = Path.Combine(baseDir, "Data", "efClient.db");
                if (File.Exists(root))
                {
                    return root;
                }

                if (File.Exists(data))
                {
                    return data;
                }

                return root;
            }
        }

        public static string SqliteConnectionString
        {
            get { return $"Data Source={SqliteDbPath};Version=3;New=False;"; }
        }

        public static string LocationId
        {
            get { return Settings.Default.LocationId ?? string.Empty; }
        }
    }
}
