using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project
{
    public static class Lang
    {
        ////main
        public static string Question_Exit { get; set; }

        public static string Ok { get; set; }
        public static string Error { get; set; }
        public static string No { get; set; }
        public static string Yes { get; set; }
        public static string Exit { get; set; }
        

        public static string Result { get; set; }
        public static string Log { get; set; }
        public static string History { get; set; }
        public static string Setting { get; set; }
        public static string ModelEdit { get; set; }

        public static string NotRegistUser { get; set; }
        public static string NoData { get; set; }

        public static void CreateFile()
        {
            iniHelper.CreateFile(); //System.IO.File.WriteAllText(FileName, "//INI FILE", System.Text.Encoding.Unicode);

            int i = 1;
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Question_Exit);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Ok);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Error);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.No);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Yes);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Exit);

            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Result);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Log);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.History);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.Setting);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.ModelEdit);

            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.NotRegistUser);
            SetINIValue(string.Format("MSG{0:0000}", i++), Lang.NoData);
            iniHelper.Flush();
        }

        public static bool Loading(string langName)
        {
            Lang.SelectedLang = langName;
            if (iniHelper == null) iniHelper = new arUtil.INIHelper(FileName);
            else iniHelper.SetFileName(FileName);


            int i = 1;

            Lang.Question_Exit = GetINIValue(string.Format("MSG{0:0000}", i++), "종료하시겠습니까?");

            Lang.Ok = GetINIValue(string.Format("MSG{0:0000}", i++), "Ok");
            Lang.Error = GetINIValue(string.Format("MSG{0:0000}", i++), "Error");
            Lang.No = GetINIValue(string.Format("MSG{0:0000}", i++), "No");
            Lang.Yes = GetINIValue(string.Format("MSG{0:0000}", i++), "Yes");
            Lang.Exit = GetINIValue(string.Format("MSG{0:0000}", i++), "Exit");

            Lang.Result = GetINIValue(string.Format("MSG{0:0000}", i++), "Result");
            Lang.Log = GetINIValue(string.Format("MSG{0:0000}", i++), "Log");
            Lang.History = GetINIValue(string.Format("MSG{0:0000}", i++), "History");
            Lang.Setting = GetINIValue(string.Format("MSG{0:0000}", i++), "Setting");
            Lang.ModelEdit = GetINIValue(string.Format("MSG{0:0000}", i++), "Model");

            Lang.NotRegistUser = GetINIValue(string.Format("MSG{0:0000}", i++), "Not a registered user.");
            Lang.NoData = GetINIValue(string.Format("MSG{0:0000}", i++), "No Data");

            if (!isExist()) CreateFile();
            return true;
        }

        #region "Common "

        public static Boolean ReLoad()
        {
            return Loading(SelectedLang);
        }
        private static arUtil.INIHelper iniHelper;
        private static string GetINIValue(string key, string def = "")
        {
            if (!System.IO.File.Exists(Lang.FileName))
                return def;

            string retval = iniHelper.get_Data("lang", key, def);
            return retval;
        }
        private static void SetINIValue(string key, string value)
        {
            iniHelper.set_Data("lang", key, value);
        }
        public static Boolean isExist()
        {
            return System.IO.File.Exists(FileName);
        }

        ////local data
        private static string _path = "";
        private static string _selectedLang = "";
        public static string SelectedLang
        {
            get
            {
                if ((string.IsNullOrWhiteSpace(_selectedLang)))
                {
                    return "kor";
                }
                else
                {
                    string fullname = Path + "\\" + _selectedLang + ".ini";
                    //if ((!System.IO.File.Exists(fullname)))
                    //{
                    //    CreateFile();
                    //}
                    return _selectedLang;
                }
            }
            set { _selectedLang = value; }
        }
        public static string Path
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_path))
                {
                    _path = AppDomain.CurrentDomain.BaseDirectory + "\\Language";
                    if (!System.IO.Directory.Exists(_path)) System.IO.Directory.CreateDirectory(_path);
                }
                return _path;
            }
            set { _path = value; }
        }
        public static List<string> LangList
        {
            get
            {
                List<string> retval = new List<string>();
                if ((System.IO.Directory.Exists(Path)))
                {
                    string[] files = System.IO.Directory.GetFiles(Path, "*.ini");
                    foreach (string file in files)
                    {
                        retval.Add(file.Substring(file.LastIndexOf("\\") + 1));
                    }
                }
                return retval;
            }
        }
        public static string FileName
        {
            get
            {
                string fn = Path + "\\" + SelectedLang;
                if(fn.ToLower().LastIndexOf(".ini") == -1) fn += ".ini";
                return fn.Replace("\\\\", "\\");
            }
        }
        #endregion


    }
}

