using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace SPC.Kiosk.Common
{
    /// <summary>
    /// Double 값을 Percent에 따라 변환
    /// </summary>
    public class DoublePercentValue : IValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double result = double.NaN;
            try
            {
                if (value is double getValue)
                {
                    if (parameter != null && getValue > 0 )
                    {
                        result = getValue * double.Parse(parameter.ToString()) / 100;
                    }
                    else
                    {
                        result = getValue;
                    }
                }
            }
            catch 
            {
                result = double.NaN;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// bool 값을 Visibility 값으로 변환
    /// </summary>
    public class BoolToVisibility : IValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility result = Visibility.Collapsed;
            try
            {
                if (value is bool getValue)
                {
                    result = getValue ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            catch
            {
                result = Visibility.Collapsed;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class BoolToUnvisibility : IValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility result = Visibility.Collapsed;
            try
            {
                if (value is bool getValue)
                {
                    result = getValue ? Visibility.Collapsed : Visibility.Visible;
                }
            }
            catch
            {
                result = Visibility.Collapsed;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 숫자 을 문자열로
    /// </summary>
    public class NumericToString : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = string.Empty;
            try
            {
                if (value is double getValue)
                {
                    if (parameter is string getFormetString)
                    {
                        result = getValue.ToString(getFormetString);
                    }
                    else
                    {
                        result = getValue.ToString();
                    }
                }
                else if (value is int getInt)
                {
                    if (parameter is string getFormetString )
                    {
                        result = getInt.ToString(getFormetString);
                    }
                    else
                    {
                        result = getInt.ToString();
                    }
                }
                else if (value is long getlong)
                {
                    if (parameter is string getFormetString)
                    {
                        result = getlong.ToString(getFormetString);
                    }
                    else
                    {
                        result = getlong.ToString();
                    }
                }
            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Numeric To  Visibility 값으로 변환
    /// </summary>
    public class NumericExistToVisibility : IValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility result = Visibility.Collapsed;
            try
            {
                double checkValue = 0d;
                try
                {
                    checkValue = double.Parse(value.ToString());
                }
                catch 
                {
                    checkValue = 0d;
                }
                bool setValue = false;
                try
                {

                    setValue = bool.Parse(parameter.ToString());

                }
                catch 
                {
                    setValue = true;
                }
                setValue = checkValue > 0d ? setValue : !setValue;
                result = setValue ? Visibility.Visible : Visibility.Collapsed;
            }
            catch
            {
                result = Visibility.Collapsed;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// Numeric To  Bool 값으로 변환
    /// </summary>
    public class NumericExistToBool : IValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool result = false;
            try
            {
                double checkValue = 0d;
                try
                {
                    checkValue = double.Parse(value.ToString());
                }
                catch
                {
                    checkValue = 0d;
                }
                bool setValue = false;
                try
                {

                    setValue = bool.Parse(parameter.ToString());

                }
                catch
                {
                    setValue = true;
                }
                result = checkValue > 0d ? setValue : !setValue;
            }
            catch
            {
                result = false;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Langeuage Code To Multi Language Data
    /// </summary>
    public class CodeToMultiLanguage : IValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            List<M_Language> result = null;
            try
            {
                if (parameter is string _parameter)
                {
                    if (_parameter.Equals(string.Empty))
                    {
                        result = null;
                    }
                    else
                    {
                        result = Languages.GetMessages(_parameter);
                    }
                }
                else
                {
                    result = null;
                }
            }
            catch
            {
                result = null;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Langeuage Code To Multi Language Data(자동계산 혹은 바코드계산)
    /// </summary>
    public class CodeToMultiLanguageAutoOrBarcode : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            List<M_Language> result = null;
            var opt810 = values[0];
            var opt811 = values[1];

            try
            {
                if (parameter is string _parameter)
                {
                    // OPT810값이 0이고 OPT811값이 1인경우(바코드계산)
                    if (opt810.ToString() == "0"
                        && opt811.ToString() == "1")
                    {
                        result = new List<M_Language>() { new M_Language { LanguageData = "바코드 계산", Type = SupportLanguageType.ko } };
                    }
                    else if (_parameter.Equals(string.Empty))
                    {
                        result = null;
                    }
                    else
                    {
                        result = Languages.GetMessages(_parameter);
                    }
                }
                else
                {
                    result = null;
                }
            }
            catch
            {
                result = null;
            }
            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Pos Option Value값에 따라 Barcode 하단 버튼 및 텍스트 Visible처리
    /// </summary>
    public class PosOptionValuesBarcodeToVisibility : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var opt810 = values[0];
            var opt811 = values[1];

            if (parameter?.ToString() == "bottomButtonText")
            {
                if (opt810.ToString() == "0"
                && opt811.ToString() == "1")
                {
                    return Visibility.Collapsed;
                }
            }
            else if (parameter?.ToString() == "toggleGrid")
            {
                if (opt810.ToString() == "0"
                && opt811.ToString() == "0")
                {
                    return Visibility.Collapsed;
                }
            }

            return Visibility.Visible;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Pos Option Value값에 따라 Barcode이미지 Margin처리
    /// </summary>
    public class PosOptionValuesBarcodeToMargin : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var opt810 = values[0];
            var opt811 = values[1];

            if (parameter?.ToString() == "image")
            {
                if (opt810.ToString() == "0"
                    && opt811.ToString() == "1")
                {
                    // 센터정렬
                    return new Thickness(0, 71 + 50, 0, 0);
                }
            }
            else if (parameter?.ToString() == "textBlock")
            {
                if (opt810.ToString() == "0"
                    && opt811.ToString() == "1")
                {
                    // 센터정렬
                    return new Thickness(57, 245 + 50, 0, 0);
                }
            }

            return parameter?.ToString() == "image" ? new Thickness(0, 71, 0, 0) : new Thickness(57, 245, 0, 0);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Point Saving Popup우측화면 Visible처리
    /// </summary>
    public class PointSavingAndStoreAgreeViewToVisibility : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var pointSaving = values[0];
            var storeAgreeView = values[1];

            if (pointSaving?.ToString().ToLower() == "true")
            {
                return Visibility.Collapsed;
            }
            else if (pointSaving?.ToString().ToLower() == "false"
                && storeAgreeView?.ToString().ToLower() == "true")
            {
                return Visibility.Collapsed;
            }

            return Visibility.Visible;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// string 값을 Visibility 값으로 변환
    /// </summary>
    public class StringToVisibility : IValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {            
            try
            {
                if (value?.ToString() == "0")
                {
                    return Visibility.Collapsed;
                }
            }
            catch
            {
                return Visibility.Collapsed;
            }

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
