using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Globalization;
using SPC.Kiosk.Base;
namespace SPC.Kiosk.Common
{
    /// <summary>
    /// 다국어 지원 Helper
    /// </summary>
    public static class Languages
    {
        private static List<M_MultiLanguage> MultiLanguagesStore = new List<M_MultiLanguage>();
        private static bool Init { get; set; } = false;
        private static void initClass()
        {
            SetMessage("CommonYes", SupportLanguageType.en, "Yes");
            SetMessage("CommonYes", SupportLanguageType.ko, "예");
            SetMessage("CommonOK", SupportLanguageType.en, "OK");
            SetMessage("CommonOK", SupportLanguageType.ko, "확인");
            SetMessage("CommonNo", SupportLanguageType.en, "No");
            SetMessage("CommonNo", SupportLanguageType.ko, "아니오");
            SetMessage("CommonCancel", SupportLanguageType.en, "Cancel");
            SetMessage("CommonCancel", SupportLanguageType.ko, "취소");
            SetMessage("CommonClear", SupportLanguageType.en, "Clear");
            SetMessage("CommonClear", SupportLanguageType.ko, "삭제");
            SetMessage("CommonPrevious", SupportLanguageType.en, "Previous");
            SetMessage("CommonPrevious", SupportLanguageType.ko, "이전으로");
            SetMessage("CommonNext", SupportLanguageType.en, "Next");
            SetMessage("CommonNext", SupportLanguageType.ko, "다음으로");
            SetMessage("CommonRetry", SupportLanguageType.en, "Retry");
            SetMessage("CommonRetry", SupportLanguageType.ko, "재시도");
            SetMessage("CommonDetail", SupportLanguageType.en, "Detail");
            SetMessage("CommonDetail", SupportLanguageType.ko, "상세보기");
            SetMessage("CommonPayment", SupportLanguageType.en, "Payment");
            SetMessage("CommonPayment", SupportLanguageType.ko, "결제하기");
            SetMessage("CommonPopup", SupportLanguageType.en, "Popup");
            SetMessage("CommonPopup", SupportLanguageType.ko, "팝업");
            SetMessage("CommonExist", SupportLanguageType.en, "Exist");
            SetMessage("CommonExist", SupportLanguageType.ko, "종료");
            SetMessage("CommonToggle", SupportLanguageType.en, "Toggle");
            SetMessage("CommonToggle", SupportLanguageType.ko, "토글");
            SetMessage("TradeHeader", SupportLanguageType.en, "Test Item List");
            SetMessage("TradeHeader", SupportLanguageType.ko, "테스트 상품 목록");

            SetMessage("PopupHeader", SupportLanguageType.en, "PopupTest");
            SetMessage("PopupHeader", SupportLanguageType.ko, "팝업 테스트");
            SetMessage("PopupMessage", SupportLanguageType.en, "Test Message");
            SetMessage("PopupMessage", SupportLanguageType.ko, "테스트 메세지");
            SetMessage("TimePopupMessage", SupportLanguageType.en, "Time Popup Message");
            SetMessage("TimePopupMessage", SupportLanguageType.ko, "타임 팝업 메세지");

            SetMessage("MemberLoginTitle", SupportLanguageType.en, "Happy Point Member Login");
            SetMessage("MemberLoginTitle", SupportLanguageType.ko, "해피 포인트 회원 확인");
            SetMessage("MobileNumberMessage", SupportLanguageType.en, "Input HP number.");
            SetMessage("MobileNumberMessage", SupportLanguageType.ko, "휴대폰 번호");
            SetMessage("CompanyNumberMessage", SupportLanguageType.en, "Input Compay number.");
            SetMessage("CompanyNumberMessage", SupportLanguageType.ko, "사업자번호");
            SetMessage("VibrationBellNumber", SupportLanguageType.en, "Vibration bell number");
            SetMessage("VibrationBellNumber", SupportLanguageType.ko, "진동벨번호");

            SetMessage("InputBarCodeMessage", SupportLanguageType.en, "Input BarCode number.");
            SetMessage("InputBarCodeMessage", SupportLanguageType.ko, "바코드 번호");
            SetMessage("InputPassword", SupportLanguageType.en, "Input Password");
            SetMessage("InputPassword", SupportLanguageType.ko, "비밀번호 입력");
            SetMessage("TimeOver", SupportLanguageType.en, "Operation Time Over");
            SetMessage("TimeOver", SupportLanguageType.ko, "입력 시간이 초과 되었습니다.");
            SetMessage("CommonReserve", SupportLanguageType.en, "Reserve");
            SetMessage("CommonReserve", SupportLanguageType.ko, "적립");
            SetMessage("CommonUsing", SupportLanguageType.en, "Using");
            SetMessage("CommonUsing", SupportLanguageType.ko, "사용");
            SetMessage("CommonUsingAll", SupportLanguageType.en, "Using All");
            SetMessage("CommonUsingAll", SupportLanguageType.ko, "모두 사용");
            SetMessage("InputUsingPoints", SupportLanguageType.en, "Input Using Points");
            SetMessage("InputUsingPoints", SupportLanguageType.ko, "사용포인트");
            SetMessage("PriceForPayment", SupportLanguageType.en, "Price for payment");
            SetMessage("PriceForPayment", SupportLanguageType.ko, "최종 결제금액");
            SetMessage("ProcessingOK", SupportLanguageType.en, "Processing Sucess.");
            SetMessage("ProcessingOK", SupportLanguageType.ko, "정상 처리 되었습니다.");

            SetMessage("CommonPaymentAgain", SupportLanguageType.en, "Retry");
            SetMessage("CommonPaymentAgain", SupportLanguageType.ko, "다시 결제");
            SetMessage("CommonPaymentCancel", SupportLanguageType.en, "Cancel Payment");
            SetMessage("CommonPaymentCancel", SupportLanguageType.ko, "결제 취소");

            SetMessage("CommonPerson", SupportLanguageType.en, "Person");
            SetMessage("CommonPerson", SupportLanguageType.ko, "개인");
            SetMessage("CommonCompany", SupportLanguageType.en, "Company");
            SetMessage("CommonCompany", SupportLanguageType.ko, "사업자");
            Init = true;
        }
        /// <summary>
        /// Get Language Native Name
        /// </summary>
        /// <param name="_languageType"></param>
        /// <returns></returns>
        public static string GetLanguageNativeName(this SupportLanguageType _languageType)
        {
            string result = string.Empty;
            try
            {
                result = _languageType.ToString().GetLanguageNativeName();
            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }
        /// <summary>
        /// Get Language Native Name
        /// </summary>
        /// <param name="_languageTypeString"></param>
        /// <returns></returns>
        public static string GetLanguageNativeName(this string _languageTypeString)
        {
            string result = string.Empty;
            try
            {
                var cultureInfo = CultureInfo.GetCultureInfo(_languageTypeString);
                result = cultureInfo.NativeName;

            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }
        /// <summary>
        /// 특정 코드 다국어 객체 가져오기
        /// </summary>
        /// <param name="_code"></param>
        /// <returns></returns>
        public static List<M_Language> GetMessages(this string _code)
        {
            List<M_Language> result = new List<M_Language>();
            try
            {
                if (!Init) initClass();
                if (MultiLanguagesStore != null && MultiLanguagesStore.Count > 0)
                {
                    var getLanguage = MultiLanguagesStore.Where(r => r.MessageCode.Equals(_code)).FirstOrDefault();
                    if (getLanguage is M_MultiLanguage)
                    {
                        result = getLanguage.MessageData;
                    }
                }
                if (result.Count.Equals(0))
                {
                    foreach (var aLanguage in Enum.GetValues(typeof(SupportLanguageType)).Cast<SupportLanguageType>())
                    {
                        result.Add(new M_Language { Type = aLanguage, LanguageData = string.Format("{0}_{1}", _code, aLanguage.ToString()) });
                    }

                }
            }
            catch
            {
                result = new List<M_Language>();
            }
            return result;
        }
        /// <summary>
        /// 다국어 정보 등록
        /// </summary>
        /// <param name="_code"></param>
        /// <param name="_languageType"></param>
        /// <param name="_messageData"></param>
        public static void SetMessage(this string _code, SupportLanguageType _languageType, string _messageData)
        {
            try
            {
                if (string.IsNullOrEmpty(_code)) return;
                if (string.IsNullOrEmpty(_messageData)) _messageData=string.Empty;
                if (MultiLanguagesStore == null) MultiLanguagesStore = new List<M_MultiLanguage>();
                var getLanguage = MultiLanguagesStore.Where(r => r.MessageCode.Equals(_code)).FirstOrDefault();
                if (getLanguage != null)
                {
                    var getMessage = getLanguage.MessageData.Where(r => r.Type.Equals(_languageType)).FirstOrDefault();
                    if (getMessage is M_Language)
                    {
                        getMessage.LanguageData = _messageData; //update

                    }
                    else //Language Type Data 가 없는 경우 추가
                    {
                        getLanguage.MessageData.Add
                        (
                            new M_Language
                            {
                                Type = _languageType,
                                LanguageData = _messageData
                            }
                        );
                    }
                }
                else //해당 Language Code Data가 없는 경우
                {
                    MultiLanguagesStore.Add
                    (
                        new M_MultiLanguage
                        {
                            MessageCode = _code,
                            MessageData = new List<M_Language>
                            {
                                new M_Language
                                {
                                    Type = _languageType,
                                    LanguageData = _messageData
                                }
                            }
                        }
                    );
                }
            }
            catch 
            {
            }
        }
        /// <summary>
        /// 특정 코드, 대상 언어 정보 가져오기
        /// </summary>
        /// <param name="_code"></param>
        /// <param name="_languageType"></param>
        /// <returns></returns>
        public static string GetLanguageData(this string _code, SupportLanguageType _supportLanguageType)
        {
            string result = string.Empty;
            try
            {
                if (!Init) initClass();
                if (MultiLanguagesStore != null && MultiLanguagesStore.Count > 0)
                {
                    var getLanguage = MultiLanguagesStore.Where(r => r.MessageCode.Equals(_code)).FirstOrDefault().MessageData;
                    if (getLanguage  is List<M_Language>)
                    {
                        result = getLanguage.GetLanguageData(_supportLanguageType);
                    }
                }
            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }
        /// <summary>
        /// 다국어 객체에서 특정 언어 정보 가져오기
        /// </summary>
        /// <param name="_multiLanguageData"></param>
        /// <param name="_supportLanguageType"></param>
        /// <returns></returns>
        public static string GetLanguageData(this List<M_Language> _multiLanguageData, SupportLanguageType _supportLanguageType)
        {
            string result = string.Empty;
            try
            {
                if (_multiLanguageData != null)
                {
                    var getMessage = _multiLanguageData.Where(r => r.Type.Equals(_supportLanguageType)).FirstOrDefault();
                    if (getMessage == null)
                    {
                        getMessage = _multiLanguageData.Where(r => r.Type.Equals(SupportLanguageType.ko)).FirstOrDefault();
                    }
                    if (getMessage is M_Language) result = getMessage.LanguageData.Replace("\\n", "\n"); 
                }
            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }
        /// <summary>
        /// 다국어 문자열 연결
        /// </summary>
        /// <param name="_multiLanguageData1"></param>
        /// <param name="_multiLanguageData2"></param>
        /// <param name="_format"></param>
        /// <returns></returns>
        public static List<M_Language> CombineLanguageData(this List<M_Language> _multiLanguageData1, List<M_Language> _multiLanguageData2, string _format = "")
        {
            List<M_Language> result = null;
            try
            {

                if (_multiLanguageData1 != null && _multiLanguageData2 !=null)
                {
                    result = new List<M_Language>();
                    foreach (var aLanguage in Enum.GetValues(typeof(SupportLanguageType)).Cast<SupportLanguageType>())
                    {
                        if (string.IsNullOrEmpty(_format)) _format = "{0}{1}";
                        var addedData = new M_Language
                        {
                            Type = aLanguage,
                            LanguageData = string.Format(_format, _multiLanguageData1.GetLanguageData(aLanguage), _multiLanguageData2.GetLanguageData(aLanguage))
                        };
                        result.Add(addedData);
                    }
                }
            }
            catch
            {
                result = null;
            }
            return result;

        }
        /// <summary>
        /// 다국어 문자열 연결
        /// </summary>
        /// <param name="_multiLanguageDatas"></param>
        /// <param name="_joinString"></param>
        /// <returns></returns>
        public static List<M_Language> JoinLanguage(this List<M_Language>[] _multiLanguageDatas, string _joinString = "")
        {
            List<M_Language> result = null;
            try
            {

                if (_multiLanguageDatas != null )
                {
                    result = new List<M_Language>();
                    foreach (var aLanguageData in _multiLanguageDatas)
                    {
                        if (_multiLanguageDatas.Last().Equals(aLanguageData))
                        {
                            result = result.CombineLanguageData(aLanguageData);
                        }
                        else
                        {
                            result = result.CombineLanguageData(aLanguageData, "{0}{1}" + _joinString);
                        }
                    }
                }
            }
            catch
            {
                result = null;
            }
            return result;

        }
        /// <summary>
        /// 다국어 정보 모두 삭제
        /// </summary>
        public static void ClearMessage()
        {
            if (MultiLanguagesStore != null)
            {
                MultiLanguagesStore.Clear();
            }
        }
        /// <summary>
        /// 다국어 정보 모두 삭제 후  해제
        /// </summary>
        public static void Dispose()
        {
            if (MultiLanguagesStore != null)
            {
                MultiLanguagesStore.Clear();
                MultiLanguagesStore = null;
            }

        }

    }
}