using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace digioz
{
    public class VisitorManagement
    {
        private static PortalSettings coPortalSettings;

        public VisitorManagement()
        { }

        public static bool SaveVisitor(Visitor visitor)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool isSuccess = false;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                int liCount = 0;
                DbParameter[] loParameters;

                loParameters = new DbParameter[4];
                loParameters[0] = new SqlParameter("@visitor_ip", SqlDbType.VarChar);
                loParameters[0].Value = visitor.IP;
                loParameters[1] = new SqlParameter("@visitor_country", SqlDbType.VarChar);
                loParameters[1].Value = visitor.Country;
                loParameters[2] = new SqlParameter("@visitor_browser", SqlDbType.VarChar);
                loParameters[2].Value = visitor.Browser;
                loParameters[3] = new SqlParameter("@visitor_os", SqlDbType.VarChar);
                loParameters[3].Value = visitor.OS;
                lsSQL = "spInsertVisitor";

                try
                {
                    loDB.openConnection();
                    loDB.ExecDBProc(lsSQL, out liCount, loParameters);
                    isSuccess = true;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return isSuccess;
        }

        /// <summary>
        /// Gets the visitor count for every day in the specified date range.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <returns></returns>
        public static Dictionary<string, int> GetDailyVisitorCount(DateTime startDate, DateTime endDate)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            Dictionary<string, int> result = new Dictionary<string, int>();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                int liCount = 0;
                DbParameter[] loParameters;

                loParameters = new DbParameter[2];
                loParameters[0] = new SqlParameter("@start_date", SqlDbType.DateTime);
                loParameters[0].Value = startDate;
                loParameters[1] = new SqlParameter("@end_date", SqlDbType.DateTime);
                loParameters[1].Value = endDate;
                lsSQL = "spGetDailyVisitorCount";

                try
                {
                    loDB.openConnection();
                    DataTable table = loDB.QueryDBDatasetProc(lsSQL, out liCount, loParameters);

                    foreach (DataRow row in table.Rows)
                    {
                        result.Add(((DateTime)row[0]).ToShortDateString(), (int)row[1]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return result;
        }

        public static Dictionary<DateTime, string> GetVisitorIps(DateTime startDate, DateTime endDate)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            Dictionary<DateTime, string> result = new Dictionary<DateTime, string>();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                int liCount = 0;
                DbParameter[] loParameters;

                loParameters = new DbParameter[2];
                loParameters[0] = new SqlParameter("@start_date", SqlDbType.DateTime);
                loParameters[0].Value = startDate;
                loParameters[1] = new SqlParameter("@end_date", SqlDbType.DateTime);
                loParameters[1].Value = endDate;
                lsSQL = "spGetVisitorIps";

                try
                {
                    loDB.openConnection();
                    DataTable table = loDB.QueryDBDatasetProc(lsSQL, out liCount, loParameters);

                    foreach (DataRow row in table.Rows)
                    {
                        result.Add((DateTime)row[0], (string)row[1]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return result;
        }

        public static Dictionary<string, int> GetVisitorCountries(DateTime startDate, DateTime endDate)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            Dictionary<string, int> result = new Dictionary<string, int>();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                int liCount = 0;
                DbParameter[] loParameters;

                loParameters = new DbParameter[2];
                loParameters[0] = new SqlParameter("@start_date", SqlDbType.DateTime);
                loParameters[0].Value = startDate;
                loParameters[1] = new SqlParameter("@end_date", SqlDbType.DateTime);
                loParameters[1].Value = endDate;
                lsSQL = "spGetVisitorCountries";

                try
                {
                    loDB.openConnection();
                    DataTable table = loDB.QueryDBDatasetProc(lsSQL, out liCount, loParameters);

                    foreach (DataRow row in table.Rows)
                    {
                        result.Add((string)row[0], (int)row[1]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return result;
        }

        public static Dictionary<string, int> GetVisitorBrowsers(DateTime startDate, DateTime endDate)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            Dictionary<string, int> result = new Dictionary<string, int>();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                int liCount = 0;
                DbParameter[] loParameters;

                loParameters = new DbParameter[2];
                loParameters[0] = new SqlParameter("@start_date", SqlDbType.DateTime);
                loParameters[0].Value = startDate;
                loParameters[1] = new SqlParameter("@end_date", SqlDbType.DateTime);
                loParameters[1].Value = endDate;
                lsSQL = "spGetVisitorBrowsers";

                try
                {
                    loDB.openConnection();
                    DataTable table = loDB.QueryDBDatasetProc(lsSQL, out liCount, loParameters);

                    foreach (DataRow row in table.Rows)
                    {
                        result.Add((string)row[0], (int)row[1]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return result;
        }

        public static Dictionary<string, int> GetVisitorOSs(DateTime startDate, DateTime endDate)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            Dictionary<string, int> result = new Dictionary<string, int>();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;
                int liCount = 0;
                DbParameter[] loParameters;

                loParameters = new DbParameter[2];
                loParameters[0] = new SqlParameter("@start_date", SqlDbType.DateTime);
                loParameters[0].Value = startDate;
                loParameters[1] = new SqlParameter("@end_date", SqlDbType.DateTime);
                loParameters[1].Value = endDate;
                lsSQL = "spGetVisitorOSs";

                try
                {
                    loDB.openConnection();
                    DataTable table = loDB.QueryDBDatasetProc(lsSQL, out liCount, loParameters);

                    foreach (DataRow row in table.Rows)
                    {
                        result.Add((string)row[0], (int)row[1]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return result;
        }
    }
}