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 MSPoll
/// </summary>
public class MSPoll
{

    private int _id = 0;
    public int ID { get { return _id;  } }

    private string _title = "";
    public string Title {
        get { return _title; }
        set { _title = value; }
    }

    private MSPollAnswerList _answers;
    public MSPollAnswerList Answers
    {
        get { return _answers; }
    }

    private MSPollQuestion _question;
    public MSPollQuestion Question {
        get { return _question; }
        set { _question = value; }
    }
    public MSPoll() { }

	public MSPoll( int ID, string Title, MSPollQuestion Question, MSPollAnswerList Answers)
	{
        _id = ID;
        _title = Title;
        _question = Question;
        _answers = Answers;

        string sql = "SELECT AnswerID FROM PollVotes WHERE PollID = " + _id;
        IDataReader reader = SqlHelper.ExecuteReader(CommandType.Text, sql);
        while (reader.Read())
        {
            try
            {
                int _answerID = Int32.Parse(reader["AnswerID"].ToString());
                GetAnswerByID(_answerID).Votes++;
            }
            catch (Exception ex) { }
        }
	}

    public string QuestionString
    {
        get { return _question.Question; }
        set { _question.Question = value; }
    }
    public int GetNextAnswerID() {
        int newID = 0;
        foreach (MSPollAnswer a in _answers)
        {
            if (a.ID > newID)
            {
                newID = a.ID;
            }
        }

        newID++;
        return newID;
    }
    public void AddAnswer(string newAnswer)
    {
          _answers.Add(new MSPollAnswer(GetNextAnswerID(),newAnswer));
    }
    public void RemoveAnswer(MSPollAnswer removeAnswer)
    {
        _answers.Remove(removeAnswer);
        //do any necessary DB updates here
    }

    public void RemoveAnswerByID(int AnswerID ) {
        foreach (MSPollAnswer ans in _answers)
        {
            if (ans.ID == AnswerID)
            {
                _answers.Remove(ans);
                break;
            }
        }
    }
    public MSPollAnswer GetAnswerByID(int AnswerID)
    {
        foreach (MSPollAnswer ans in _answers)
        {
            if (ans.ID == AnswerID)
            {
                return ans;
                break;
            }
        }
        return null;
    }

    public void Save() {

        string xmlAnswers = Utility.XMLize(_answers);

        string sql;
        if ( _id == 0)
        {
            sql = "insert into Polls (AddedDate) values (GetDate());select @@identity";
            _id = Int32.Parse(SqlHelper.ExecuteScalar(CommandType.Text, sql).ToString());
        }

        SqlParameterList spl = new SqlParameterList();
        spl.AddParameter("Title", _title, SqlDbType.VarChar);
        spl.AddParameter("Question", _question.Question, SqlDbType.VarChar);
        spl.AddParameter("Answers", xmlAnswers, SqlDbType.Text);

        sql = "UPDATE Polls SET " +
            " Title=@Title, " +
            " Question=@Question , " +
            " Answers=@Answers " +
            " WHERE id=" + _id;

        //add expire date
        //add active

        SqlHelper.ExecuteNonQuery(CommandType.Text, sql, spl.ToArray());


    }

    public static MSPoll GetNewPoll() {
        return new MSPoll(0,"", new MSPollQuestion(""), new MSPollAnswerList());
    }
    public static MSPoll GetPollByID(int ID)
    {
        return SiteProvider.Polls.GetPollByID(ID);
    }
}
