using System;
using Bitrix.Components;
using Bitrix.Security;
using Bitrix.UI;
using System.Collections.Generic;

public partial class AuthComponent : BXComponent
{
    protected void Page_Load(object sender, EventArgs e)
    {
		string page;

		if (!Parameters.ContainsKey("ProfilePath"))
			Parameters["ProfilePath"] = string.Empty;

		bool first = true;
		bool containsParams = false;
		string url = Request.FilePath;

		foreach (string s in Request.QueryString.AllKeys)
		{
			if (s != null && !s.Equals("auth_page", StringComparison.InvariantCultureIgnoreCase)
				&& !s.Equals("checkword", StringComparison.InvariantCultureIgnoreCase))
			{
				if (first)
					url += "?";
				else
					url += "&";
				url += s + "=" + Request.QueryString[s];
				containsParams = true;
				first = false;
			}
		}

		Results["LoginRedirectPath"] = url;
		if (Request["back_url"] != null)
			Results["LoginRedirectPath"] = Request["back_url"];

		Results["PasswordRecoveryPath"] = url + (containsParams ? "&" : "?") + "auth_page=recovery";
		Results["PasswordRecoveryCodePath"] = url + (containsParams ? "&" : "?") + "auth_page=recoverycode";
		Results["RegisterPath"] = url + (containsParams ? "&" : "?") + "auth_page=register";
		Results["LoginPath"] = url + (containsParams ? "&" : "?") + "auth_page=login";
		Results["LoginLink"] = Results["LoginPath"];

		Results["EnablePasswordRecovery"] = BXUserManager.Provider.EnablePasswordReset;
		Results["RequiresQuestionAndAnswer"] = BXUserManager.Provider.RequiresQuestionAndAnswer;
		Results["RequiresCheckWord"] = BXUserManager.Provider.RequiresCheckWord;

		if (Request["auth_page"] != null)
		{
			switch (Request["auth_page"].ToLowerInvariant())
			{
				case "recovery":
					page = "PasswordRecovery";
					break;
				case "recoverycode":
					page = "PasswordRecoveryCode";
					break;
				case "register":
					page = "register";
					break;
				case "profile":
					page = "profile";
					break;
				default:
					page = "login";
					break;
			}
		}
		else
		{
			page = "login";
		}

		IncludeComponentTemplate(page);
    }

    protected override void PreLoadComponentDefinition()
    {
        Title = GetMessage("SystemAuth.Title");
        Description = GetMessage("SystemAuth.Description");
        Icon = "images/system_auth.gif";

		Group = new BXComponentGroup("Auth", GetMessage("Group"), 100, BXComponentGroup.Utility);

        ParamsDefinition.Add(
            "ProfilePath",
            new BXParamText(
                GetMessageRaw("UserProfilePath"),
                String.Empty,
                BXCategory.Main
            )
        );

        ParamsDefinition.Add(
            "RegistrationDoAuthentication",
            new BXParamYesNo(
                GetMessageRaw("RegistrationDoAuthentication"),
                false,
                BXCategory.Main
                )
            );

        ParamsDefinition.Add(
            "RegistrationRedirectUrl",
            new BXParamText(
                GetMessageRaw("RegistrationRedirectUrl"),
                string.Empty,
                BXCategory.Main
                )
            );
   
		BXCategory fields = new BXCategory(GetMessageRaw("FieldSettings"), "fields", 200);

		ParamsDefinition.Add(
			"FirstNameFieldMode",
			new BXParamSingleSelection(
				GetMessageRaw("FirstNameFieldMode"),
				"require",
				fields
				)
			);

		ParamsDefinition.Add(
			"LastNameFieldMode",
			new BXParamSingleSelection(
				GetMessageRaw("LastNameFieldMode"),
				"require",
				fields
				)
			);

		ParamsDefinition.Add(
			"DisplayNameFieldMode",
			new BXParamSingleSelection(
				GetMessageRaw("DisplayNameFieldMode"),
				"hide",
				fields
				)
			);
	}
	protected override void LoadComponentDefinition()
	{
		ParamsDefinition["FirstNameFieldMode"].Values = new List<BXParamValue>(new BXParamValue[] {
				new BXParamValue(GetMessageRaw("Field.Hide"), "hide"),
				new BXParamValue(GetMessageRaw("Field.Show"), "show"),
				new BXParamValue(GetMessageRaw("Field.Require"), "require")
		});
		ParamsDefinition["LastNameFieldMode"].Values = new List<BXParamValue>(new BXParamValue[] {
				new BXParamValue(GetMessageRaw("Field.Hide"), "hide"),
				new BXParamValue(GetMessageRaw("Field.Show"), "show"),
				new BXParamValue(GetMessageRaw("Field.Require"), "require")
		});
		ParamsDefinition["DisplayNameFieldMode"].Values = new List<BXParamValue>(new BXParamValue[] {
				new BXParamValue(GetMessageRaw("Field.Hide"), "hide"),
				new BXParamValue(GetMessageRaw("Field.Show"), "show"),
				new BXParamValue(GetMessageRaw("Field.Require"), "require")
		});
	}
}
