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 PollsProvider
/// </summary>
public class PollsProvider
{
    static private PollsProvider _instance = null;

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

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

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

    public MSPollList GetAllPolls()
    {

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

    }
    public MSPollList GetCurrentPolls()
    {

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

    }

    public MSPoll GetPollByID(int ID)
    {
        MSPoll _poll = null;
        using (SqlConnection connection = new SqlConnection(SqlC))
        {
            SqlCommand cmd = new SqlCommand("ms_Polls_GetPollByID", 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())
                _poll = GetPollFromReader(reader);
            connection.Close();
        }
        return _poll;
    }

    public MSPollList GetPollsByWhere(string where)
    {
        MSPollList ret = new MSPollList();
        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( GetPollFromReader(reader));
            connection.Close();
        }
        return ret;
    }

    private MSPoll GetPollFromReader(IDataReader reader)
    {
        MSPoll _poll = new MSPoll(
                Int32.Parse(reader["ID"].ToString()),
                reader["Title"].ToString(),
                new MSPollQuestion(reader["Question"].ToString()),
                (MSPollAnswerList)Utility.Deserialize(typeof(MSPollAnswerList),reader["Answers"].ToString())
            );

        return _poll;
    }}
