using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Services;
using System.IO;
using Bitrix.UI;
using Bitrix.Configuration;

public partial class bitrix_install_Default : System.Web.UI.Page
{
    //FIELDS
	private BXWizardStep Step1;
	private BXWizardStep Step2;
	private BXWizardStep Step3;
	private BXWizardStep Step4;
	private BXWizardStep Step5;
	private BXWizardStep Step6;
	private BXWizardStep Step7;

	private WizardStep WizardStep1;
	private WizardStep WizardStep2;
	private WizardStep WizardStep3;
	private WizardStep WizardStep4;
	private WizardStep WizardStep5;
	private WizardStep WizardStep6;
	private WizardStep WizardStep7;

    private bool simpleMode = true;
	private string storedMode;
	const int LastStepSimple = 1;
	const int LastStepFull = 6;


    //PROPERTIES
    public bool SimpleMode
    {
        get {
            if (!string.IsNullOrEmpty(Request["mode"]) && Request["mode"].ToLower() == "full")
                return false;
			if (string.Equals(storedMode, "simple", StringComparison.OrdinalIgnoreCase))
				return true;
			if (string.Equals(storedMode, "full", StringComparison.OrdinalIgnoreCase))
				return false;

            return simpleMode; 
        }
        set { simpleMode = value; }
    }
	public int ActiveStepIndex
    {
        get
        {
            int step;
            if (!string.IsNullOrEmpty(Request["step"]) && int.TryParse(Request["step"], out step))
            {
				if (step < Wizard.WizardSteps.Count)
                    return step;
            }

			return Wizard.ActiveStepIndex;
        }
    }

    //METHODS
	protected void Page_Init(object sender, EventArgs e)
	{
		storedMode = Request.Form["@StoredMode"];

		BXWebConfig config = new BXWebConfig();
		config.Load(MapPath("~/web.config"));
		SimpleMode = config.ConnectionStrings.ContainsKey("BXConnectionString"); 

		if (SimpleMode)
			CreateSimpleWizard();
		else
			CreateFullWizard();
		Wizard.ActiveStepIndex = 0;
	}
    protected void Page_Load(object sender, EventArgs e)
    {
        Sidebar.SimpleMode = SimpleMode;
		bool installed = File.Exists(MapPath("~/global.asax"));

        if (!SimpleMode)
        {
			Wizard.ActiveStepIndex = installed ? LastStepFull : ActiveStepIndex;
            Sidebar.CurrentIndex = Wizard.ActiveStepIndex;

           

            ActivateWizardStep(Wizard.ActiveStep);
        }
        else
        {
            Wizard.ActiveStepIndex = installed ? LastStepSimple : ActiveStepIndex;
            Sidebar.CurrentIndex = Wizard.ActiveStepIndex;
            

            ActivateWizardStep(Wizard.ActiveStep);
        }

        Sidebar.DataBind();
    }
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
		bool installed = File.Exists(MapPath("~/global.asax"));
		if (installed)
			Wizard.ActiveStepIndex = SimpleMode ? LastStepSimple : LastStepFull;

		ActivateWizardStep(Wizard.ActiveStep);
        Sidebar.DataBind();
		Page.ClientScript.RegisterHiddenField("@StoredMode", SimpleMode ? "simple" : "full");
    }
	protected void Page_PreRender(object sender, EventArgs e)
	{
		Sidebar.CurrentIndex = Wizard.ActiveStepIndex;
		Sidebar.DataBind();
	}

	private void CreateFullWizard()
	{
		WizardStep1 = new WizardStep();
		WizardStep1.Controls.Add(Step1 = (BXWizardStep)LoadControl("~/bitrix/install/Step1.ascx"));
		Wizard.WizardSteps.Add(WizardStep1);

		WizardStep2 = new WizardStep();
		WizardStep2.Controls.Add(Step2 = (BXWizardStep)LoadControl("~/bitrix/install/Step2.ascx"));
		Wizard.WizardSteps.Add(WizardStep2);

		WizardStep3 = new WizardStep();
		WizardStep3.Controls.Add(Step3 = (BXWizardStep)LoadControl("~/bitrix/install/Step3.ascx"));
		Wizard.WizardSteps.Add(WizardStep3);

		WizardStep4 = new WizardStep();
		WizardStep4.Controls.Add(Step4 = (BXWizardStep)LoadControl("~/bitrix/install/Step4.ascx"));
		Wizard.WizardSteps.Add(WizardStep4);

		WizardStep5 = new WizardStep();
		WizardStep5.Controls.Add(Step5 = (BXWizardStep)LoadControl("~/bitrix/install/Step5.ascx"));
		Wizard.WizardSteps.Add(WizardStep5);

		WizardStep6 = new WizardStep();
		WizardStep6.Controls.Add(Step6 = (BXWizardStep)LoadControl("~/bitrix/install/Step6.ascx"));
		Wizard.WizardSteps.Add(WizardStep6);

		WizardStep7 = new WizardStep();
		WizardStep7.Controls.Add(Step7 = (BXWizardStep)LoadControl("~/bitrix/install/Step7.ascx"));
		Wizard.WizardSteps.Add(WizardStep7);

		Step1.Next += new EventHandler(Step1_Next);
		Step2.Next += new EventHandler(Step2_Next);
		Step3.Next += new EventHandler(Step3_Next);
		Step4.Next += new EventHandler(Step4_Next);
		Step5.Next += new EventHandler(Step5_Next);
		Step6.Next += new EventHandler(Step6_Next);
	}

	private void CreateSimpleWizard()
	{
		WizardStep1 = new WizardStep();
		WizardStep1.Controls.Add(Step1 = (BXWizardStep)LoadControl("~/bitrix/install/Step6.ascx"));
		Wizard.WizardSteps.Add(WizardStep1);

		WizardStep2 = new WizardStep();
		WizardStep2.Controls.Add(Step2 = (BXWizardStep)LoadControl("~/bitrix/install/Step7.ascx"));
		Wizard.WizardSteps.Add(WizardStep2);

		Step1.Next += new EventHandler(SimpleStep2_Next);
	}
    void ActivateWizardStep(WizardStepBase step)
    {
        foreach (Control ctr in step.Controls)
        {
            if ((ctr as BXWizardStep) != null)
            {
                (ctr as BXWizardStep).Activate();
            }
        }
    }

    void SimpleStep2_Next(object sender, EventArgs e)
    {
		Wizard.MoveTo(WizardStep2);
    }

    

    void Step6_Next(object sender, EventArgs e)
    {
        Wizard.MoveTo(WizardStep7);
    }

    void Step5_Next(object sender, EventArgs e)
    {
        Wizard.MoveTo(WizardStep6);
    }

    void Step4_Next(object sender, EventArgs e)
    {
        Wizard.MoveTo(WizardStep5);
    }

    void Step3_Next(object sender, EventArgs e)
    {
        Wizard.MoveTo(WizardStep4);
    }

    void Step2_Next(object sender, EventArgs e)
    {
        Wizard.MoveTo(WizardStep3);
    }

    void Step1_Next(object sender, EventArgs e)
    {
        Wizard.MoveTo(WizardStep2);
    }
}
