using System.Web;

namespace ReportingServices.Shared.HelperClasses
{
    public static class APIHelperFunctions
    {
        private readonly static string fabTimeServer = "http://messa004.infineon.com/fabtime717service/GetChartData.aspx?";

        public static string GetBeginningOfWeekAsAPIString()
        {
            DateTime date = DateTime.Now;

            int dayOfWeek = (int)date.DayOfWeek;

            date = dayOfWeek switch
            {
                0 => date.AddDays(-6),
                1 => date,
                _ => date.AddDays(1 - dayOfWeek)
            };

            return GetDateTimeAsAPIString(date.ToString(), false);
        }

        public static string GetDateWithOffsetAsAPIString(string dateString, float offset)
        {
            DateTime date = DateTime.Parse(dateString);

            date = date.AddHours(offset);

            return GetDateTimeAsAPIString(date.ToString(), true);
        }

        public static string GetDateTimeAsAPIString(string dateString, bool fullDateTime)
        {
            DateTime date = DateTime.Parse(dateString);

            if (fullDateTime)
                dateString = date.ToString("yyyy-M-d HH:mm:ss");
            else
                dateString = date.Year + "-" + date.Month + "-" + date.Day + " 0:0:0";

            return dateString;
        }

        public static Dictionary<string, string> SetParameters(string startDate = "", string endDate = "", string chart = "", string periodLen = "", 
            string areasLike = "", string toolsLike = "", string operationsLike = "", string capacityTypesLike = "")
        {
            Dictionary<string, string> parameters = new();

            startDate = startDate == "" ? HttpUtility.UrlEncode(GetBeginningOfWeekAsAPIString()) : startDate;
            endDate = endDate == "" ? HttpUtility.UrlEncode(GetDateTimeAsAPIString(DateTime.Now.ToString(), true)) : endDate;

            parameters.Add("chart", chart);
            parameters.Add("starttime", startDate);
            parameters.Add("endtime", endDate);
            parameters.Add("periodlen", periodLen);
            parameters.Add("areaslike", areasLike);
            parameters.Add("toolslike", toolsLike);
            parameters.Add("operationslike", operationsLike);
            parameters.Add("capacitytypeslike", capacityTypesLike);
            parameters.Add("login", "administrator");
            parameters.Add("password", "admin");
            parameters.Add("fabtimeauthentication", "1");

            return parameters;
        }

        public static string GenerateURL(Dictionary<string, string> parameters)
        {
            int count = 0;
            string url = fabTimeServer;

            foreach (KeyValuePair<string, string> pair in parameters)
            {
                if (pair.Value != "")
                    url = url + pair.Key + "=" + pair.Value;

                if (count != parameters.Count - 1 && !string.IsNullOrEmpty(pair.Value))
                    url += "&";

                count++;
            }

            return url;
        }

        public static string GenerateURLWithParameters(string startDate = "", string endDate = "", string chart = "", string periodLen = "", 
            string areasLike = "", string toolsLike = "", string operationsLike = "", string capacityTypesLike = "")
        {
            Dictionary<string, string> parameters = SetParameters(startDate, endDate, chart, 
                periodLen, areasLike, toolsLike, operationsLike, capacityTypesLike);

            return GenerateURL(parameters);
        }

        public static List<T> ReverseList<T>(List<T> inputList)
        {
            List<T> temp = new();

            for (int i = inputList.Count - 1; i >= 0; i--)
            {
                temp.Add(inputList[i]);
            }

            return temp;
        }
    }
}
