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

namespace ZionVault.Util
{
    public class StringFormat
    {
        private static string getEnviromentValue(string variable)
        {
            if (!ZionVault.Environment.VariableFactory.isVariable(variable.Trim('%')))
                return variable;

            return ZionVault.Environment.VariableFactory.construct(variable).Value();
        }

        public static string Apply(string value, string mask, string pattern = null, string dateFormatIn = null)
        {
            if (value.Length == 0)
                return value;

            value = getEnviromentValue(value);

            if (pattern != null && pattern.Length > 0)
                value = Regex.Replace(value, pattern, string.Empty);

            if (mask != null && mask != string.Empty)
            {
                value = Regex.Replace(value.ToUpper(), @"MARCO", @"MARÇO");

                System.Globalization.CultureInfo cultInfo =
                    (dateFormatIn != null && Regex.IsMatch(dateFormatIn, @"dd\WMM\Wyyyy")) || 
                    (dateFormatIn == null || dateFormatIn == string.Empty) ?
                        new System.Globalization.CultureInfo(@"pt-BR") : new System.Globalization.CultureInfo(@"en-US");

                DateTime date;
                if (DateTime.TryParse(value, cultInfo, System.Globalization.DateTimeStyles.None, out date))
                {
                    return date.ToString(mask);
                }


                /*System.Windows.Forms.MaskedTextBox textBox = new System.Windows.Forms.MaskedTextBox();
                textBox.Mask = mask;
                textBox.Text = value;

                return textBox.Text;*/
                return value;
            }

            return value;
        }

        public static string ApplyDate(string value, string culture, string mask)
        {
            if (value.Length == 0)
                return value;
            
            value = Regex.Replace(value.ToUpper(), @"MARCO", @"MARÇO");

            System.Globalization.CultureInfo cultInfo =
                (culture != null && culture == @"pt-BR") || (culture == null || culture == string.Empty) ?
                    new System.Globalization.CultureInfo(@"pt-BR") : new System.Globalization.CultureInfo(@"en-US");

            DateTime date;
            if (DateTime.TryParse(value, cultInfo, System.Globalization.DateTimeStyles.None, out date))
            {
                return date.ToString(mask);
            }

            /*System.Windows.Forms.MaskedTextBox textBox = new System.Windows.Forms.MaskedTextBox();
            textBox.Mask = mask;
            textBox.Text = value;

            return textBox.Text;*/
            return value;
        }

        public static string ApplyCurrency(string value, string cultureOut)
        {
            if (cultureOut == @"" || cultureOut == null)
                cultureOut = @"pt-BR";

            if (value.Length == 0)
                return value;

            var regex = Regex.Matches(value, @"[^a-zA-Z0-9_\s]");


            string c = regex[regex.Count - 1].Value;

            int a = (value.LastIndexOf(c) + 1 - regex.Count);

            value = Regex.Replace(value, @"[\W]", string.Empty);

            Decimal deci = Decimal.Parse(value.Insert(a, @","));

            return string.Format(new System.Globalization.CultureInfo(cultureOut), "{0:N2}", deci);
        }

        public static string Replace(string value, List<string> partterns)
        {
            partterns.ForEach(
                parttern =>
                {
                    string[] p = parttern.Split(',');

                    value = Regex.Replace(value, p.First(), p.Last());
                });

            return value;
        }

        public static string RemoveChars(string value, List<string> patterns)
        {
            patterns.ForEach(
                pattern =>
                {
                    value = Regex.Replace(value, pattern, string.Empty);
                });

            return value;
        }
    }
}