using System;
using System.Data;
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 System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Collections.Generic;


/// <summary>
/// Summary description for EventsProvider
/// </summary>
public class EventsProvider
{
    static private EventsProvider _instance = null;

    private string _sqlc;
    protected string SqlC
    {
        get
        {
            return _sqlc;
        }
        set
        {
            _sqlc = value;
        }
    }

    static public EventsProvider Instance
    {
        get
        {
            if (_instance == null)
                _instance = (EventsProvider)Activator.CreateInstance(
                     typeof(EventsProvider));
            return _instance;
        }
    }

    public EventsProvider()
    {
        _sqlc = ConfigurationManager.ConnectionStrings["SabrinContent"].ToString();
    }

    public MSEventList GetAllEvents()
    {

        MSEventList _eventList = new MSEventList();
        using (SqlConnection connection = new SqlConnection(SqlC))
        {
            connection.Open();
            IDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "ms_Events_GetAllEvents");
            while (reader.Read())
            {
                _eventList.Add(GetEventFromReader(reader));
            }
            connection.Close();
        }
        _eventList.Reverse();
        return _eventList;

    }
    public MSEventList GetCurrentEvents()
    {

        MSEventList _eventList = new MSEventList();
        using (SqlConnection connection = new SqlConnection(SqlC))
        {
            connection.Open();
            IDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "ms_Events_GetCurrentEvents");
            while (reader.Read())
            {
                _eventList.Add(GetEventFromReader(reader));
            }
            connection.Close();
        }
        return _eventList;

    }

    public MSEvent GetEventByID(int ID)
    {
        MSEvent _event = null;
        using (SqlConnection connection = new SqlConnection(SqlC))
        {
            SqlCommand cmd = new SqlCommand("ms_Events_GetEventByID", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
            connection.Open();
            IDataReader reader = SqlHelper.ExecuteReader(cmd, CommandBehavior.SingleRow);
            if (reader.Read())
                _event = GetEventFromReader(reader);
            connection.Close();
        }
        return _event;
    }

    public MSEventList GetEventssByWhere(string where)
    {
        MSEventList ret = new MSEventList();
        using (SqlConnection connection = new SqlConnection(SqlC))
        {
            string sql = "select * from vuser where " + where;
            connection.Open();
            IDataReader reader = SqlHelper.ExecuteReader(CommandType.Text,sql);
            while (reader.Read())
                ret.Add( GetEventFromReader(reader));
            connection.Close();
        }
        return ret;
    }

    private MSEvent GetEventFromReader(IDataReader reader)
    {
        MSEvent _event = new MSEvent(
                Int32.Parse(reader["id"].ToString()),
                DateTime.Parse(reader["EventDate"].ToString()),
                reader["Title"].ToString(),
                reader["Description"].ToString() );

        return _event;
    }}
