//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Points;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Settings
{
	/// <summary>
	/// Summary description for PointSystem.
	/// </summary>
	public class PointSystem : BaseToolsPage
	{

		#region Members

		protected YesNoRadioButtonList EnablePointSystem;
		protected DisplayLevelDropDownList PointsPostDisplayLevel;
		protected DisplayLevelDropDownList PointsUserDisplayLevel;
		protected TextBox PointsPostFactor;
		protected TextBox PointsReplyFactor;
		protected TextBox PointsReplierFactor;
		protected TextBox PointsRatingFactor;
		protected TextBox PointsDownloadFactor;
		protected TextBox PointsDownloaderFactor;
		protected TextBox PointsFavoritePostFactor;
		protected TextBox PointsFavoriteUserFactor;
		protected TextBox PointsRaterFactor;

		protected RequiredFieldValidator PointsPostFactorValidator;
		protected RequiredFieldValidator PointsReplyFactorValidator;
		protected RequiredFieldValidator PointsReplierFactorValidator;
		protected RequiredFieldValidator PointsRatingFactorValidator;
		protected RequiredFieldValidator PointsDownloadFactorValidator;
		protected RequiredFieldValidator PointsDownloaderFactorValidator;
		protected RequiredFieldValidator PointsFavoritePostFactorValidator;
		protected RequiredFieldValidator PointsFavoriteUserFactorValidator;
		protected RequiredFieldValidator PointsRaterFactorValidator;

		protected Button CalculateButton;

		protected StatusMessage LicenseMessage;
		protected StatusMessage Status;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected ResourceLinkButton SaveButton;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			this.CalculateButton.Click += new EventHandler(CalculateButton_Click);
			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			// Validate License for Points Add-On
			if (!Telligent.Registration.CommunityServer.IsStandard)
			{
				LicenseMessage.Success = false;
				LicenseMessage.IsLicenseMessage = true;
				LicenseMessage.ResourceName = "Admin_SiteSettings_Points_RequireLicense";
				LicenseMessage.Visible = true;
			}

			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}


		protected void Bind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
			Status.Visible = false;
			this.SaveButton.Text = ResourceManager.GetString( "Save" );
			this.CalculateButton.Text = ResourceManager.GetString( "Calculate" );

			EnablePointSystem.SelectedValue = settings.EnablePointSystem;
			PointsPostDisplayLevel.SelectedValue = settings.PointsPostDisplayLevel;
			PointsUserDisplayLevel.SelectedValue = settings.PointsUserDisplayLevel;
			PointsPostFactor.Text = settings.PointsPostFactor.ToString();
			PointsReplyFactor.Text = settings.PointsReplyFactor.ToString();
			PointsReplierFactor.Text = settings.PointsReplierFactor.ToString();
			PointsRatingFactor.Text = settings.PointsRatingFactor.ToString();
			PointsDownloadFactor.Text = settings.PointsDownloadFactor.ToString();
			PointsDownloaderFactor.Text = settings.PointsDownloaderFactor.ToString();
			PointsFavoritePostFactor.Text = settings.PointsFavoritePostFactor.ToString();
			PointsFavoriteUserFactor.Text = settings.PointsFavoriteUserFactor.ToString();
			PointsRaterFactor.Text = settings.PointsRaterFactor.ToString();

		}

		private void CalculateButton_Click(object sender, EventArgs e)
		{
			if (PointsHelper.CalculateAllPoints())
			{
				Status.Success = true;
				Status.ResourceName = "Admin_SiteSettings_Points_Calculate_Success";
			}
			else
			{
				Status.Success = false;
				Status.ResourceName = "Admin_SiteSettings_Points_Calculate_Failure";
			}
			
			Status.Visible = true;
		}

		private void SaveButton_Click(object sender, EventArgs e) 
		{
			SiteSettings settings = CSContext.Current.SiteSettings;

			SaveSettings( settings );

			if(Page.IsValid)
			{
				Status.Success = true;
				Status.ResourceName = "Admin_SiteSettings_StatusSuccess";

				DataBind();
			}
			else
			{
				Status.Success = false;
				Status.ResourceName = "Admin_SiteSettings_StatusFailed";
			}

			Status.Visible = true;
		}

		private void InitializeComponent()
		{
		
		}


		protected virtual void SaveSettings( SiteSettings settings ) 
		{

			settings.EnablePointSystem = EnablePointSystem.SelectedValue;
			settings.PointsPostDisplayLevel = PointsPostDisplayLevel.SelectedValue;
			settings.PointsUserDisplayLevel = PointsUserDisplayLevel.SelectedValue;

			try 
			{
				settings.PointsPostFactor = Int32.Parse(PointsPostFactor.Text); 
				if (settings.PointsPostFactor < 0)
					PointsPostFactorValidator.IsValid = false;
			}
			catch { PointsPostFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsReplyFactor = Int32.Parse(PointsReplyFactor.Text); 
				if (settings.PointsReplyFactor < 0)
					PointsReplyFactorValidator.IsValid = false;
			}
			catch { PointsReplyFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsReplierFactor = Int32.Parse(PointsReplierFactor.Text); 
				if (settings.PointsReplierFactor < 0)
					PointsReplierFactorValidator.IsValid = false;
			}
			catch { PointsReplierFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsRatingFactor = Int32.Parse(PointsRatingFactor.Text); 
				if (settings.PointsRatingFactor < 0)
					PointsRatingFactorValidator.IsValid = false;
			}
			catch { PointsRatingFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsDownloadFactor = Int32.Parse(PointsDownloadFactor.Text); 
				if (settings.PointsDownloadFactor < 0)
					PointsDownloadFactorValidator.IsValid = false;
			}
			catch { PointsDownloadFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsDownloaderFactor = Int32.Parse(PointsDownloaderFactor.Text); 
				if (settings.PointsDownloaderFactor < 0)
					PointsDownloaderFactorValidator.IsValid = false;
			}
			catch { PointsDownloaderFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsFavoritePostFactor = Int32.Parse(PointsFavoritePostFactor.Text); 
				if (settings.PointsFavoritePostFactor < 0)
					PointsFavoritePostFactorValidator.IsValid = false;
			}
			catch { PointsFavoritePostFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsFavoriteUserFactor = Int32.Parse(PointsFavoriteUserFactor.Text); 
				if (settings.PointsFavoriteUserFactor < 0)
					PointsFavoriteUserFactorValidator.IsValid = false;
			}
			catch { PointsFavoriteUserFactorValidator.IsValid = false; }

			try 
			{
				settings.PointsRaterFactor = Int32.Parse(PointsRaterFactor.Text); 
				if (settings.PointsRaterFactor < 0)
					PointsRaterFactorValidator.IsValid = false;
			}
			catch { PointsRaterFactorValidator.IsValid = false; }

			if(Page.IsValid)
				SiteSettingsManager.Save(settings);

		}
	}
}
