using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TM.Shared.Components;

namespace TM.TransSmart.Components
{
    public class Helper
    {
        public string AdjustDeliveryDateTimeFrom(string loadingDate)
        {
            string result = GetAdjustedDeliveyDate(loadingDate);
            result += "T07:00:00";

            return result;
        }

        public string AdjustDeliveryDateTimeTo(string loadingDate)
        {
            string result = GetAdjustedDeliveyDate(loadingDate);
            result += "T16:00:00";

            return result;
        }

        private static string GetAdjustedDeliveyDate(string loadingDate)
        {
            var curdate = DateTime.Now.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);

            var dateCompareResult = String.Compare(curdate, loadingDate);
            string result;

            DateTime lDate = DateTime.ParseExact(loadingDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);

            if (dateCompareResult <= 0)
            {
                result = lDate.AddDays(2).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
            }
            else
            {
                result = DateTime.Now.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);

            }
            return result;
        }
    
        public string GetDelimitedValueByIndex(string input, string delimiter, string xsltIndex)
        {

            string result = "";

            try
            {
                int index = Convert.ToInt16(xsltIndex);

                if (input.EndsWith(delimiter))
                {
                    input = input.Remove(input.Length - 1, 1);
                }
                ArrayList array_reference = new ArrayList(input.Split(Convert.ToChar(delimiter)));

                if (array_reference.Count - 1 >= index)
                {
                    result = Convert.ToString(array_reference[index]);
                }
            }
            catch (Exception) { }
 
            return result;
        }
    }
}
