using System;
using System.Globalization;

namespace SepehrTourApiSampleClient.BuiltinTypeExtensions
{
    public static class StringExtension
    {
        public static T DeserializeFromJson<T>(this string input)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(input);
        }

        public static DateTime ParseAsGregorianDate(this string date)
        {
            try
            {
                return DateTime.ParseExact(date, "yyyy-M-d", CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                throw new Exception($"'{date}' is not valid date format. Valid format is yyyy-MM-dd Technical Error: {exception.Message}");
            }
        }

        public static DateTime ParseFlightDate(this string date)
        {
            return DateTime.ParseExact(date, "yyyy-MM-dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
        }
    }
}
