using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using digioz;
using digioz.admin;

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

        public static SiteLogList GetSiteLogList()
        {
            SiteLogList loSiteLogList = new SiteLogList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                SiteLog loSiteLog;
                DataTable loSiteLogTable;

                string lsSQL = "SELECT TOP 200* FROM tblSiteLog ORDER BY log_timestamp DESC;";

                loDB.openConnection();
                loSiteLogTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                foreach (DataRow loDR in loSiteLogTable.Rows)
                {
                    loSiteLog = new SiteLog();
                    loSiteLog.ID = (int)loDR["log_id"];
                    loSiteLog.Username = (string)loDR["log_username"];
                    loSiteLog.ClientIP = (string)loDR["log_client_ip"];
                    loSiteLog.Description = (string)loDR["log_description"];
                    if (!loDR["log_timestamp"].Equals(DBNull.Value))
                    {
                        loSiteLog.Timestamp = (DateTime)loDR["log_timestamp"];
                    }

                    loSiteLogList.Add(loSiteLog);
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("SiteLog");                
                foreach (object obj in loObjs)
                {
                    loSiteLogList.Add((SiteLog)obj);
                }
                loDB.closeEntity();
            }
            return loSiteLogList;
        }

        public static bool DeleteSiteLog(DateTime pdDate)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;

                lsSQL = "EXEC spDeleteSiteLogByDate "
                        + "@date = '" + pdDate + "';";
                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
                lbResult = true;
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                IEnumerable loObjs = loDB.GetEntity("SiteLog");
                foreach (object obj in loObjs)
                {
                    SiteLog loLog = (SiteLog)obj;
                    if (loLog.Timestamp < pdDate)
                    {
                        loDB.DeleteEntity(loLog);
                    }                    
                }                
            }
            return lbResult;
        }
    }
}
