using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;


public partial class Admin_ManagePolls : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Utility.IsNull(Session["Admin"]))
        {
            Response.Redirect("~/Home.aspx");
        }



        pnlResults.Visible = false;

        if (!IsPostBack)
        {
            DataBind();
        }
    }
    protected void DataBind() {
        MSPoll _poll = MSPoll.GetPollByID(3);
        txtTitle.Text = _poll.Title;
        txtQuestion.Text = _poll.QuestionString;

        gvAnswers.DataSource = _poll.Answers;
        gvAnswers.DataBind();

        //Table tbl = new Table();
        //tbl.Width = 300;
        //tbl.BackColor = System.Drawing.Color.FromArgb(133, 164, 193);
        //TableRow row = new TableRow();
        //TableCell c1 = new TableCell();
        //TableCell c2 = new TableCell();
        //Label label = new Label();
        //label.Text = "Poll Answers";
        //c1.Controls.Add(label);
        //c1.ColumnSpan = 2;
        //row.Controls.Add(c1);

        //tbl.Controls.Add(row);
        //HiddenField hid;
        //TextBox text;
        //Button btnDeleteAnswer;
        //int answersCount = 0;
        //foreach (MSPollAnswer a in _poll.Answers)
        //{
        //    c1 = new TableCell();
        //    row = new TableRow();

        //    hid = new HiddenField();
        //    hid.ID = "lbl_AnswerID_" + answersCount;
        //    hid.Value = a.ID.ToString();

        //    label = new Label();
        //    label.Text = a.ID.ToString();

        //    c1.Controls.Add(hid);
        //    c1.Controls.Add(label);

        //    c2 = new TableCell();
        //    text = new TextBox();
        //    text.ID = "txt_AnswerValue_" + answersCount;
        //    text.Text = a.Label;

        //    btnDeleteAnswer = new Button();
        //    btnDeleteAnswer.ID = "btnDelete_"+answersCount;
        //    btnDeleteAnswer.Text = "delete";
        //    btnDeleteAnswer.Command += new CommandEventHandler(this.DeleteAnswer);

        //    c2.Controls.Add(text);
        //    c2.Controls.Add(btnDeleteAnswer);

        //    row.Controls.Add(c1);
        //    row.Controls.Add(c2);

        //    tbl.Controls.Add(row);

        //    answersCount++;
        //}

        //pnlAnswerControls.Controls.Add(tbl);
        ////gvPolls.DataSource = MSEvent.AllPolls;
        ////gvPolls.DataBind();    
    
    }
    protected void DeleteAnswer(object sender, EventArgs e) {
        int i = 0;
        DataBind();
    }


    protected void btnAddAnswer_Click(object sender, EventArgs e)
    {

        MSPoll _poll = MSPoll.GetPollByID(3);
        _poll.AddAnswer(txtNewAnswer.Text);
        _poll.Save();
        DataBind();

    }
    protected void gvAnswers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        MSPoll _poll = MSPoll.GetPollByID(3);

        GridView gv = (GridView)sender;
        GridViewRow row = gv.Rows[e.RowIndex];


        TextBox txtAnswer = (TextBox)row.FindControl("txtAnswer");

        int _id = Int32.Parse(gv.DataKeys[gv.EditIndex].Value.ToString());

        MSPollAnswer _answer = _poll.GetAnswerByID(_id);
        _answer.Label = txtAnswer.Text;

        _poll.Save();
        gvAnswers.SelectedIndex = -1;
        gvAnswers.EditIndex = -1;
        gvAnswers.DataSource = _poll.Answers;
        gvAnswers.DataBind();


    }
    protected void btnSavePoll_Click(object sender, EventArgs e)
    {
        MSPoll _poll = MSPoll.GetPollByID(3);
        _poll.Title = txtTitle.Text;
        _poll.QuestionString = txtQuestion.Text;
        _poll.Save();

        gvAnswers.DataSource = _poll.Answers;
        gvAnswers.DataBind();

    }

    protected void gvAnswers_Command(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void gvAnswers_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        MSPoll _poll = MSPoll.GetPollByID(3);

        GridView gv = (GridView)sender;
        GridViewRow row = gv.Rows[gv.EditIndex];

        TextBox txtAnswer = (TextBox)row.FindControl("txtAnswer");

        int _id = Int32.Parse(gv.DataKeys[gv.EditIndex].Value.ToString());

        MSPollAnswer _answer = _poll.GetAnswerByID(_id);
        _answer.Label = txtAnswer.Text;

        _poll.Save();
        gvAnswers.SelectedIndex = -1;
        gvAnswers.EditIndex = -1;
        gvAnswers.DataSource = _poll.Answers;
        gvAnswers.DataBind();

    }

    protected void gvAnswers_CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvAnswers.SelectedIndex = -1;
        gvAnswers.EditIndex = -1;
        MSPoll _poll = MSPoll.GetPollByID(3);
        gvAnswers.DataSource = _poll.Answers;
        gvAnswers.DataBind();
    }
    protected void gvAnswers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        MSPoll _poll = MSPoll.GetPollByID(3);

        gvAnswers.SelectedIndex = e.NewEditIndex;
        gvAnswers.EditIndex = e.NewEditIndex;
        gvAnswers.DataSource = _poll.Answers ;
        gvAnswers.DataBind();  
    }

    protected void gvAnswers_Delete(object sender, GridViewDeleteEventArgs e)
    {

        GridView gv = (GridView)sender;
        int _id = Int32.Parse(gv.DataKeys[e.RowIndex].Value.ToString());

        MSPoll _poll = MSPoll.GetPollByID(3);
        _poll.RemoveAnswerByID( _id );
        _poll.Save();
        gv.SelectedIndex = -1;
        gvAnswers.DataSource = _poll.Answers;
        gvAnswers.DataBind();

    }


    /// <summary>
    /// ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvPolls_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //GridView gv = (GridView)sender;
        //GridViewRow row = gv.Rows[e.RowIndex];

        //TextBox txtTitle = (TextBox)row.FindControl("txtTitle");
        //TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
        //TextBox txtEventDate = (TextBox)row.FindControl("txtEventDate");

        //int _id = Int32.Parse(gv.DataKeys[e.RowIndex].Value.ToString());

        //MSEvent _event = MSEvent.GetEventByID(_id);
        //_event.Title = txtTitle.Text;
        //_event.Description = txtDescription.Text;
        //_event.EventDate = DateTime.Parse(txtEventDate.Text);

        //_event.Save();
        //gvPolls.SelectedIndex = -1;
        //gvPolls.EditIndex = -1;
        //gvPolls.DataSource = MSEvent.AllPolls;
        //gvPolls.DataBind();

    }
    protected void gvPolls_Command(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void gvPolls_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //GridView gv = (GridView)sender;
        //GridViewRow row = gv.Rows[gv.EditIndex];

        //TextBox txtTitle = (TextBox)row.FindControl("txtTitle");
        //TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
        //TextBox txtEventDate = (TextBox)row.FindControl("txtEventDate");

        //int _id = Int32.Parse(gv.DataKeys[gv.EditIndex].Value.ToString());

        //MSEvent _event = MSEvent.GetEventByID(_id);
        //_event.Title = txtTitle.Text;
        //_event.Description = txtDescription.Text;
        //_event.EventDate = DateTime.Parse(txtEventDate.Text);

        //_event.Save();
        //gvPolls.SelectedIndex = -1;
        //gvPolls.EditIndex = -1;
        //gvPolls.DataBind();

    }

    protected void gvPolls_CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvPolls.SelectedIndex = -1;
        gvPolls.EditIndex = -1;
        //gvPolls.DataSource = MSEvent.AllPolls;
        //gvPolls.DataBind();
    }
    protected void gvPolls_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //gvPolls.SelectedIndex = e.NewEditIndex;
        //gvPolls.EditIndex = e.NewEditIndex;
        //gvPolls.DataSource = MSEvent.AllPolls;
        //gvPolls.DataBind();  
    }

    protected void gvPolls_Delete(object sender, GridViewDeleteEventArgs e)
    {
        //GridView gv = (GridView)sender;
        //gv.SelectedIndex = -1;
        //int _id = Int32.Parse(gv.DataKeys[e.RowIndex].Value.ToString());

        //string sql = "DELETE FROM Polls WHERE ID =  "+_id;
        //SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
        //pnlResults.CssClass = "SuccessPanel";
        //pnlResults.Visible = true;
        //lblResults.Text = "That Event was deleted";

        //gv.DataSource = MSEvent.AllPolls;
        //gv.DataBind();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        MSPoll _poll = MSPoll.GetNewPoll();
        _poll.QuestionString = txtNewQuestion.Text;
        _poll.Title = txtNewTitle.Text;
        
        List<string> answers = new List<string>( txtNewAnswerList.Text.Split(Environment.NewLine.ToCharArray()));
        foreach (string answer in answers)
        {
            _poll.AddAnswer(answer);
        }


        DataBind();
        _poll.Save();

        //gvPolls.DataSource = MSEvent.AllPolls;
        //gvPolls.DataBind();     
    
    }
}
