using Foresight.Fleet.Services.OTRConfig;
using IronIntel.Contractor.OTRConfig;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace IronIntel.Contractor.Site.OTRConfig
{
    public class OTRConfigBasePage : ContractorBasePage
    {
        protected void ProcessRequest(string method)
        {
            object result = null;
            try
            {
                string methodName = Request.Params["MethodName"];
                if (methodName != null)
                {
                    switch (methodName.ToUpper())
                    {
                        case "GETSPEEDINGEVENTS":
                            result = GetSpeedingEvents();
                            break;
                        case "EXCLUDEDSPEEDINGEVENTS":
                            result = ExcludedSpeedingEvents();
                            break;
                        case "GETHARSHDRIVINGEVENTS":
                            result = GetHarshDrivingEvents();
                            break;
                        case "EXCLUDEDHARSHDRIVINGEVENTS":
                            result = ExcludedHarshDrivingEvents();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                SystemParams.WriteLog("error", "OTRConfigBasePage", ex.Message, ex.ToString());
                throw ex;
            }
            string json = JsonConvert.SerializeObject(result);
            Response.Write(json);
            Response.End();
        }

        #region Speeding

        private object GetSpeedingEvents()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var sdatestr = HttpUtility.HtmlDecode(clientdata[0]);
                    var edatestr = HttpUtility.HtmlDecode(clientdata[1]);

                    List<SpeedingItem> list = new List<SpeedingItem>();
                    return list.ToArray(); ;
                }

                return string.Empty;
            }
            catch (Exception ex)
            {
                SystemParams.WriteLog("error", "OTRConfigBasePage.GetSpeedingList", ex.Message, ex.ToString());
                return ex.Message;
            }
        }

        private object ExcludedSpeedingEvents()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var id = HttpUtility.HtmlDecode(clientdata[0]);
                    var notes = HttpUtility.HtmlDecode(clientdata[1]);

                    return "OK";
                }
                return "Failed";
            }
            catch (Exception ex)
            {
                return ex.Message; ;
            }
        }

        #endregion


        #region Harsh Driving

        private object GetHarshDrivingEvents()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var sdatestr = HttpUtility.HtmlDecode(clientdata[0]);
                    var edatestr = HttpUtility.HtmlDecode(clientdata[1]);
                    var searchtxt = HttpUtility.HtmlDecode(clientdata[2]);

                    DateTime startdate = Helper.DBMinDateTime;
                    DateTime enddate = DateTime.MaxValue;
                    if (!DateTime.TryParse(sdatestr, out startdate))
                        startdate = Helper.DBMinDateTime;
                    if (!DateTime.TryParse(edatestr, out enddate))
                        enddate = DateTime.MaxValue;

                    return HarshDrivingManagement.GetHarshDrivingEvents(session.SessionID, startdate, enddate, searchtxt, session.User.UID);
                }

                return new HarshDrivingItem[0];
            }
            catch (Exception ex)
            {
                SystemParams.WriteLog("error", "OTRConfigBasePage.GetHarshDrivingEvents", ex.Message, ex.ToString());
                return ex.Message;
            }
        }

        private object ExcludedHarshDrivingEvents()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    var clientdata = HttpUtility.HtmlDecode(Request.Form["ClientData"]);
                    HarshDrivintClient hd = JsonConvert.DeserializeObject<HarshDrivintClient>(clientdata);

                    HarshDrivingManagement.ExcludedHarshDrivingEvents(session.SessionID, hd, session.User.UID);
                    return "OK";
                }
                return "Failed";
            }
            catch (Exception ex)
            {
                return ex.Message; ;
            }
        }

        #endregion
    }

}
