using System;
using System.Collections.Generic;
using System.IO;
using Cloud.Alibaba.Oss;

namespace Cloud.Alibaba
{
    /// <summary>
    /// 账号配置
    /// </summary>
    public class AccountConfig
    {
        public readonly string AccessKey;
        public readonly string SecretKey;
        
        private static readonly Dictionary<Account,AccountConfig> Configs = new Dictionary<Account, AccountConfig>();

        static AccountConfig()
        {
            Configs.Add(Account.Oss, new AccountConfig("ALIBABA_CLOUD_ACCESS_KEY_ID","ALIBABA_CLOUD_ACCESS_KEY_SECRET","aliyun.config"));
            Configs.Add(Account.Ecs, new AccountConfig("ALIBABA_CLOUD_ACCESS_KEY_ID_ECS","ALIBABA_CLOUD_ACCESS_KEY_SECRET_ECS","ecsaliyun.config"));
        }

        public AccountConfig(string environmentAccessKey,string environmentSecretKey,string fileConfig)
        {
            string ak = EnvironmentVariableHelper.GetEnvironmentVariable(environmentAccessKey);
            string sk = EnvironmentVariableHelper.GetEnvironmentVariable(environmentSecretKey);
            
            if ((string.IsNullOrEmpty(ak) || string.IsNullOrEmpty(sk)) && File.Exists(fileConfig))
            {
                string[] accessContent = File.ReadAllLines(fileConfig);
                ak = accessContent[0];
                sk = accessContent[1];
            }
            
            AccessKey = ak;
            SecretKey = sk;
        }
        
        public static AccountConfig GetConfig(Account account)
        {
            return Configs[account];
        }
    }
}

