using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridView_Pagination
{
    /// <summary>
    /// Header template: renders an ImageButton that triggers a server-side
    /// toggle of all checkboxes on the current page.
    ///
    /// The click handler is injected via constructor so the Page (or any other
    /// class) can supply a method that has full access to Session, hidden fields,
    /// and the grid – avoiding the need to walk up the NamingContainer chain for
    /// every piece of state.
    /// </summary>
    public class HeaderButtonTemplate : ITemplate
    {
        private readonly ImageClickEventHandler _handler;

        // Defaults kept so the original sample still compiles unchanged.
        private readonly string _imageUrl;
        private readonly string _toolTip;

        public HeaderButtonTemplate(
            ImageClickEventHandler handler,
            string imageUrl = "~/tick.ico",
            string toolTip = "Select / deselect all on this page")
        {
            _handler = handler ?? throw new ArgumentNullException(nameof(handler));
            _imageUrl = imageUrl;
            _toolTip = toolTip;
        }

        public void InstantiateIn(Control container)
        {
            var btn = new ImageButton
            {
                ID = "checkallbutton",
                ImageUrl = _imageUrl,
                Width = new Unit(16, UnitType.Pixel),
                Height = new Unit(16, UnitType.Pixel),
                ToolTip = _toolTip
            };
            btn.Click += _handler;
            container.Controls.Add(btn);
        }
    }
}