/**************************************************************************************************

NetPolls - The web poll engine
Copyright (c) 2007 Data Illusion (http://www.dataillusion.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

************************************************************************************************/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DataIllusion.NetPolls.DataAccess;
using DataIllusion.NetPolls.BusinessRules;
using DataIllusion.NetPolls.Data;
using DataIllusion.NetPolls.WebAdmin.UserControls;

namespace DataIllusion.NetPolls.WebAdmin
{
	/// <summary>
	/// Summary description for PollStats.
	/// </summary>
	public partial class PollSettings : PageBase
	{
		protected PollListControl PollList;
		protected HeaderControl PollHeader;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			MessageLabel.Visible = false;

			if (!Page.IsPostBack)
			{
				PollList.PollId = PollId;
				PollList.BindDropDown();
				PollHeader.PollId = PollId;

				BindData();
			}
		}

		private void BindData()
		{
			Polls polls = new Polls();
			PollData pollData = polls.GetPollById(PollId);

			TitleLabel.Text = pollData.Title;
			CreationDateLabel.Text = pollData.CreationDate.ToString();
			TotalOfVotersLabel.Text = pollData.VoterNumber.ToString();
			PollDisplayTimesLabel.Text = pollData.PollDisplayTimes.ToString() + " times";
			ResultsDisplayTimesLabel.Text = pollData.ResultsDisplayTimes.ToString() + " times";
			TitleTextBox.Text = pollData.Title;
			ArchiveCheckBox.Checked = pollData.Archive;
			ActivatedCheckBox.Checked = pollData.Activated;
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void DeleteButton_Click(object sender, System.EventArgs e)
		{
			Poll poll = new Poll();
			poll.DeletePoll(PollId);

			Response.Redirect("PollSettings.aspx");
		}

		protected void ResetButton_Click(object sender, System.EventArgs e)
		{
			Poll poll = new Poll();
			poll.ResetVotes(PollId);
			BindData();
			MessageLabel.Visible = true;
			MessageLabel.Text = "Voter count set to 0";
		}

		protected void ReleaseIPButton_Click(object sender, System.EventArgs e)
		{
			IPs ips = new IPs();
			ips.DeleteIPAddresses(PollId);
			MessageLabel.Visible = true;
			MessageLabel.Text = "IP's deleted";
		}

		protected void UpdateButton_Click(object sender, System.EventArgs e)
		{
			Poll poll = new Poll();
			PollData pollData = new PollData();
			pollData.Title = TitleTextBox.Text;
			pollData.Id = PollId;
			pollData.Archive = ArchiveCheckBox.Checked;
			pollData.Activated = ActivatedCheckBox.Checked;
			poll.UpdatePoll(pollData);
			MessageLabel.Visible = true;
			MessageLabel.Text = "Poll updated";
		}
	}
}
