using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Globalization;
using SPC.Kiosk.Base;
using System.Windows.Media;
using System.Collections.ObjectModel;
using Cosmos.UserFrame;
namespace SPC.Kiosk.Common
{
    public static class CommonFunction
    {
        /// <summary>
        /// Reregister Name an Object
        /// </summary>
        /// <param name="_frameworkElement"></param>
        /// <param name="_registerName"></param>
        /// <param name="_registerObject"></param>
        public static void ReregisterName(this FrameworkElement _frameworkElement, string _registerName,object _registerObject)
        {
            try
            {
                if (_frameworkElement.FindName(_registerName) != null)
                {
                    _frameworkElement.UnregisterName(_registerName);
                }
                _frameworkElement.RegisterName(_registerName, _registerObject);
            }
            catch 
            {
            }
        }

        /// <summary>
        /// Find Parent object by name
        /// </summary>
        /// <param name="_child"></param>
        /// <param name="_findName"></param>
        /// <returns></returns>
        public static object FindParent(this object _child, string _findName)
        {
            object result = null;
            try
            {
                var _parent = _child;
                while (_parent != null)
                {
                    var getType = _parent.GetType();
                    var nameProperty = getType.GetProperty("Name");
                    var getName = nameProperty.GetValue(_parent, null);
                    if (getName.Equals(_findName) && getType.Name.Equals("Grid"))
                    {
                        result = _parent;
                        break;
                    }
                    var parentProperty = getType.GetProperty("Parent");
                    _parent = parentProperty.GetValue(_parent, null);
                }
            }
            catch
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// String Text Show Rander Size Caculate
        /// </summary>
        /// <param name="_inString"></param>
        /// <param name="_languageType"></param>
        /// <param name="_fontSize"></param>
        /// <param name="_fontFamily"></param>
        /// <param name="_fontStyle"></param>
        /// <param name="_fontWeight"></param>
        /// <param name="_fontStretch"></param>
        /// <returns></returns>
        public static Size GetFormatedTextSize(this string _inString
                                             , SupportLanguageType _languageType
                                             , double _fontSize
                                             , FontFamily _fontFamily
                                             , FontStyle _fontStyle
                                             , FontWeight _fontWeight
                                             , FontStretch _fontStretch)
        {
            var result = new Size(0,0);
            try
            {
                if (string.IsNullOrEmpty(_inString))
                {
                    result = new Size(0, 0);
                }
                else
                {
                    var formattedText = new FormattedText(_inString,
                                        CultureInfo.GetCultureInfo(_languageType.ToString()),
                                        FlowDirection.LeftToRight,
                                        new Typeface(_fontFamily, _fontStyle, _fontWeight, _fontStretch),
                                        _fontSize,
                                        Brushes.Black);
                    result = new Size(formattedText.Width, formattedText.Height);
                }
            }
            catch
            {
                result = new Size(0, 0);
            }
            return result;
        }
        /// <summary>
        /// 문자열 길이를 목적 폭에 마춰서 자르기 (... 뒤에 추가)
        /// </summary>
        /// <param name="_inString"></param>
        /// <param name="_languageType"></param>
        /// <param name="_fontSize"></param>
        /// <param name="_fontFamily"></param>
        /// <param name="_fontStyle"></param>
        /// <param name="_fontWeight"></param>
        /// <param name="_fontStretch"></param>
        /// <param name="_targetWidth"></param>
        /// <returns></returns>
        public static string TextTrimming(this string _inString
                                        , SupportLanguageType _languageType
                                        , double _fontSize
                                        , FontFamily _fontFamily
                                        , FontStyle _fontStyle
                                        , FontWeight _fontWeight
                                        , FontStretch _fontStretch
                                        , double _targetWidth)
        {
            var result = _inString;
            try
            {
                if (!string.IsNullOrEmpty(_inString)
                    && !_targetWidth.Equals(double.NaN)
                    && _targetWidth > 0)
                {
                    var sourceWidth = _inString.GetFormatedTextSize(_languageType
                                                                   , _fontSize
                                                                   , _fontFamily
                                                                   , _fontStyle
                                                                   , _fontWeight
                                                                   , _fontStretch).Width;
                    if (sourceWidth > _targetWidth)
                    {
                        int subCount = 1;
                        var subString = _inString;
                        var newStringWidth = 0d;
                        do
                        {
                            subCount++;
                            if (subString.Length > subCount)
                            {
                                subString = subString.Substring(0, _inString.Length - subCount) + "...";
                                newStringWidth = subString.GetFormatedTextSize(_languageType
                                                                             , _fontSize
                                                                             , _fontFamily
                                                                             , _fontStyle
                                                                             , _fontWeight
                                                                             , _fontStretch).Width;
                            }
                            else
                            {
                                break;
                            }
                        } while (newStringWidth > _targetWidth);
                        result = subString;
                    }
                }
            }
            catch
            {
                result = _inString;
            }
            return result;
        }
        /// <summary>
        /// Make Option Label
        /// </summary>
        /// <param name="_option"></param>
        /// <returns></returns>
        public static List<M_Language> GetOptionLabel(List<M_ItemOptionGroup> _option)
        {
            List<M_Language> result = null;
            try
            {
                if (_option is List<M_ItemOptionGroup>)
                {
                    var index = 0;
                    result = new List<M_Language>();
                    foreach (var aGroup in _option)
                    {
                        foreach (var aOption in aGroup.Options)
                        {
                            if (aOption.IsSelected)
                            {
                                if (aOption.Name is List<M_Language>)
                                {
                                    if (index.Equals(0))
                                    {
                                        foreach (var aName in aOption.Name)
                                        {
                                            result.Add(aName.Clone());
                                        }
                                    }
                                    else
                                    {
                                        foreach (var aName in result)
                                        {
                                            var addLanguageData = Languages.GetLanguageData(aOption.Name, aName.Type);
                                            aName.LanguageData += "/" + addLanguageData;
                                            if (aOption.Kind.Equals(OptionKind.Count))
                                            {
                                                aName.LanguageData += string.Format("({0})", aOption.SelectValue.ToString("#,##0"));
                                            }
                                        }
                                    }
                                    index++;
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                result = null;
            }
            return result;
        }
        /// <summary>
        /// GetDate from string
        /// </summary>
        /// <param name="_dateString"></param>
        /// <returns></returns>
        public static DateTime GetDate(string _dateString)
        {
            var result = DateTime.MinValue;
            try
            {

                if (_dateString.Length.Equals(6))
                {
                    result = DateTime.Parse(String.Format("{0}-{1}-{2}"
                                            , _dateString.Substring(0, 2), _dateString.Substring(2, 2), _dateString.Substring(4, 2))).Date;
                }
                else if (_dateString.Length.Equals(8))
                {
                    result = DateTime.Parse(String.Format("{0}-{1}-{2}"
                                         , _dateString.Substring(0, 4), _dateString.Substring(4, 2), _dateString.Substring(6, 2))).Date;
                }
                else
                {
                    result = DateTime.Parse(_dateString).Date;
                }
            }
            catch
            {
                result = DateTime.MinValue;
            }
            if (result.Equals(DateTime.MinValue))
            {
                result = DateTime.Now.Date;
            }
            return result;
        }


    }
}