using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bitrix.Modules;
using Bitrix.Main;
using System.Collections.Specialized;
using System.Text;
using Bitrix.UI;

using Bitrix;
using Bitrix.Services;
using Bitrix.Security;

public partial class bitrix_admin_Modules : Bitrix.UI.BXAdminPage
{
	bool canManageSettings;
	
	protected void Page_Init(object sender, EventArgs e)
	{
		if (!BXUser.IsCanOperate(BXRoleOperation.Operations.ProductSettingsView))
			BXAuthentication.AuthenticationRequired();
		canManageSettings = BXUser.IsCanOperate(BXRoleOperation.Operations.ProductSettingsManage);
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		Title = GetMessage("TableTitleModules");
		MasterTitle = GetMessage("TableTitleModules");
		List<ModuleDescription> modules = new List<ModuleDescription>();

		foreach (BXModuleConfig module in BXModuleManager.AllModules)
		{
			ModuleDescription desc = new ModuleDescription();
			desc.Id = module.ModuleId;
			desc.Name = BXLoc.GetModuleMessage(module.ModuleId, "Module.Name", true) ?? string.Format(GetMessageRaw("Module"), module.ModuleId);
			desc.Description = BXLoc.GetModuleMessage(module.ModuleId, "Module.Description", true) ?? "---";
			desc.Version = module.Version;
			desc.IsInstalled = module.Installed;
			if (desc.IsInstalled)
			{
				desc.StatusImageUrl = BXUri.ToRelativeUri("~/bitrix/admin/img/logstatus1.png");
				desc.Description = BXModuleManager.GetModule(module.ModuleId).Description;
			}
			else
				desc.StatusImageUrl = BXUri.ToRelativeUri("~/bitrix/admin/img/logstatus4.png");

			modules.Add(desc);
		}

		modules.Sort(delegate(ModuleDescription a, ModuleDescription b)
		{
			int aRank = string.Equals(a.Id, "main", StringComparison.OrdinalIgnoreCase) ? 0 : 1;
			int bRank = string.Equals(b.Id, "main", StringComparison.OrdinalIgnoreCase) ? 0 : 1;
			int result = aRank.CompareTo(bRank);
			if (result == 0)
				result = string.Compare(a.Id, b.Id, true);
			return result;
		});

		GridView.DataSource = modules;
		GridView.DataBind();
	}

	//NESTED TYPES
	public class ModuleDescription
	{
		private string id;

		public string Id
		{
			get
			{
				return id;
			}
			set
			{
				id = value;
			}
		}
		private string name;

		public string Name
		{
			get
			{
				return name;
			}
			set
			{
				name = value;
			}
		}
		private string description;

		public string Description
		{
			get
			{
				return description;
			}
			set
			{
				description = value;
			}
		}
		private string version;

		public string Version
		{
			get
			{
				return version;
			}
			set
			{
				version = value;
			}
		}

		private bool isInstalled;

		public bool IsInstalled
		{
			get
			{
				return isInstalled;
			}
			set
			{
				isInstalled = value;
			}
		}

		private string statusImageUrl;

		public string StatusImageUrl
		{
			get
			{
				return statusImageUrl;
			}
			set
			{
				statusImageUrl = value;
			}
		}


	}
	protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		BXGridViewRow row = (BXGridViewRow)e.Row;
		ModuleDescription desc = (ModuleDescription)row.DataItem;
		if (row.RowType != DataControlRowType.DataRow)
			return;

		List<string> allowed = new List<string>();

		if (string.Equals(desc.Id, "main", StringComparison.OrdinalIgnoreCase) || !canManageSettings)
			allowed.Add("nop");
		else if (desc.IsInstalled)
			allowed.Add("uninstall");
		else
			allowed.Add("install");

		row.AllowedCommandsList = allowed.ToArray();
	}
	protected void GridView_PopupMenuClick(object sender, BXPopupMenuClickEventArgs e)
	{
		if (!canManageSettings)
			return;

		string moduleId = (string)GridView.DataKeys[e.EventRowIndex].Value;

		if (e.CommandName.ToLower() == "install")
		{
			Response.Redirect("~/bitrix/admin/modulesinstall.aspx?action=install&module=" + moduleId + "&token=" + BXCsrfToken.GenerateToken());
		}
		else
		{

			Response.Redirect("~/bitrix/admin/modulesinstall.aspx?action=uninstall&module=" + moduleId + "&token=" + BXCsrfToken.GenerateToken());
		}
	}
}
