using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using TelerikTestGrid.Code;

namespace TelerikTestGrid
{
    public partial class Default : Page
    {
        private Control _externalControl;

        protected override void OnInit(EventArgs e)
        {
            const string controlPath = "~/TelerikTestUserControl.ascx";
            _externalControl = Page.LoadControl(controlPath);
            (_externalControl as ITelerikTestUserControl)?.SetData();

            ExternalControl.Controls.Add(_externalControl);
            ExternalControl.DataBind();
             //RadGrid2.Rebind();
        }

        protected void Wizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Wizard.ActiveStepIndex++;
        }

        protected void Wizard_ActiveStepChanged(object sender, EventArgs e)
        {
            switch (Wizard.WizardSteps[Wizard.ActiveStepIndex].ID)
            {
                case "Step1":
                    break;

                case "Step2":
                    (_externalControl as ITelerikTestUserControl)?.SetData();

                    //RadGrid2.Rebind();
                    break;

                case "Step3":
                    break;
            }
        }

        protected void RadGrid2_OnNeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid2.DataSource = LoadData();
        }

        private static IEnumerable<TestItem> LoadData()
        {
            for (var i = 0; i < 50; i++)
            {
                yield return new TestItem(i, $"Item {i}");
            }
        }

        protected void RadGrid2_DataBinding(object sender, EventArgs e)
        {
            RadGrid2.DataSource = LoadData();
        }
    }
}
