using System;
using System.Collections.Generic;
using System.Text;
using Yanesdk.System;

namespace Yamalib.Util
{
    /// <summary>
    /// JAVAのPropertiesみたいなクラス
    /// </summary>
    public class Properties
    {
        public static Properties GetInstance(string filename)
        {
            string lowFilename = filename.ToLower();

            if (INSTANCE_MAP.ContainsKey(lowFilename))
            {
                return INSTANCE_MAP[lowFilename];
            }

            Properties prop = new Properties();
            if (prop.Load(filename))
            {
                INSTANCE_MAP[lowFilename] = prop;
            }
            return prop;
        }

        /// プロパティの取得
        public string GetProperty(string key)
        {
            if (mp.ContainsKey(key))
            {
                return mp[key];
            }
            return null;
        }

        /// プロパティの取得
        public string GetProperty(string key, string def)
        {
            if (mp.ContainsKey(key))
            {
                return mp[key];
            }
            return def;
        }

        /// プロパティの取得
        public long GetPropertyNum(string key, long def)
        {
            if (mp.ContainsKey(key))
            {
                try
                {
                    return Convert.ToInt64(mp[key]);

                }
                catch (Exception)
                {
                    return def;
                }
            }
            return def;
        }

        /// デジマルの取得
        public double GetPropertyDecimal(string key, double def)
        {
            if (mp.ContainsKey(key))
            {
                try
                {
                    return Convert.ToDouble(mp[key]);

                }
                catch (Exception)
                {
                    return def;
                }
            }
            return def;
        }

        /// プロパティのすべてのキーを取得
        public string[] GetPropertyNames()
        {
            string[] result = new string[mp.Count];
            int i = 0;
            foreach(string str in mp.Keys) {
                result[i++] = str;
            }
            return result;
        }

        private Properties() { }

        /// ファイルの最終変更時間を取得
        private static long GetModTime(string filename)
        {
            long modTime = -1L;
            try
            {
                System.IO.DirectoryInfo fileInfo = new System.IO.DirectoryInfo(filename);
                modTime = fileInfo.LastAccessTime.Ticks;
            }
            catch
            {
                // ファイルにアクセス失敗...
            }
            return modTime;
        }

        /// ファイルの最終変更時間をチェックし変更されていれば再読込を行う
        private void CheckLastMod()
        {
            if (GetModTime(propname) > lastWriteTime)
            {
                Load(propname);
            }
        }

        /// 読み込みます
        public bool Load(string filename)
        {
            propname = filename;
            lastWriteTime = GetModTime(filename);

            string data = Yamalib.Util.YamalibUtility.ReadTextFile(filename);
            string[] lineData = data.Split(new char[] { '\r','\n' });

            foreach (string str in lineData)
            {
                string[] buf = str.Split(new char[] { '#' });
                if (buf.Length < 1)
                {
                    continue;
                }
                string linebuf = buf[0];
                int pos  = linebuf.IndexOf('=');
	
                if ( -1 == pos ) {
                    continue;
                }
	
                string key = linebuf.Substring(0,pos);
                string value = string.Empty;
                if ( linebuf.Length >= pos + 1 ) {
                    value = linebuf.Substring((pos+1), linebuf.Length-pos-1);
                }
                mp[key] = value;
            }
            return true;
        }

        private static Dictionary<string, Properties> INSTANCE_MAP = new Dictionary<string, Properties>();
        private Dictionary<string, string> mp = new Dictionary<string, string>();
        private string propname;
        private long lastWriteTime = -1L;

    }
}
