using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.RadControlSpy;
using Telerik.WinControls.UI;
using Telerik.WinControls.Layouts;
using Telerik.WinControls.Data;
using Telerik.WinControls.Enumerations;

namespace _491270
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool updating = false;

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nwindDataSet.Customers' table. You can move, or remove it, as needed.
            this.customersTableAdapter.Fill(this.nwindDataSet.Customers);

            radGridView1.Columns.Move(radGridView1.Columns.Count - 1, 0);
            radGridView1.Columns.Move(9, 1);

            radGridView1.ViewCellFormatting += new CellFormattingEventHandler(radGridView1_ViewCellFormatting);
            radGridView1.GroupDescriptors.CollectionChanged += new NotifyCollectionChangedEventHandler(GroupDescriptors_CollectionChanged);
            radGridView1.RowsChanged += new GridViewCollectionChangedEventHandler(radGridView1_RowsChanged);

            radGridView1.Rows[11].Cells["Bool"].Value = true;
            radGridView1.Rows[53].Cells["Bool"].Value = true;
            radGridView1.Rows[63].Cells["Bool"].Value = true;

            GroupDescriptor desc = new GroupDescriptor("Country");
            radGridView1.GroupDescriptors.Add(desc);

            radGridView1.ValueChanged += new EventHandler(radGridView1_ValueChanged);
       
        }

        private void RadGridView1_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
        {
            UpdateGroupTag(radGridView1.CurrentRow.Group.GroupRow);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            var col = radGridView1.Columns["Bool"] as GridViewCheckBoxColumn;
            col.EnableHeaderCheckBox = true;
        }

        void radGridView1_ValueChanged(object sender, EventArgs e)
        {
            RadCheckBoxEditor editor = radGridView1.ActiveEditor as RadCheckBoxEditor;
            if (editor != null)
            {
                this.radGridView1.EndEdit();
                UpdateGroupTag(radGridView1.CurrentRow.Group.GroupRow);
            }
        }

        void radGridView1_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
        {
            
                foreach (GridViewRowInfo row in radGridView1.Rows)
                {
                    if (row.Group != null)
                    {
                        UpdateGroupTag(row.Group.GroupRow);
                    }
                }
          
        }

        void GroupDescriptors_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (GridViewRowInfo row in radGridView1.ChildRows)
                {
                    GridViewGroupRowInfo groupRow = row as GridViewGroupRowInfo;
                    if (groupRow != null)
                    {
                        UpdateGroupTag(groupRow);
                    }
                }
            }
        }

        private void UpdateGroupTag(GridViewGroupRowInfo groupRow)
        {
            bool foundOffRow = false;
            foreach (GridViewRowInfo r in groupRow.ChildRows)
            {
                if (!(bool)r.Cells["Bool"].Value)
                {
                    foundOffRow = true;
                    break;
                }
            }

            if (foundOffRow)
            {
                groupRow.Tag = false;
            }
            else
            {
                groupRow.Tag = true;
            }
            groupRow.InvalidateRow();
        }

        void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            GridGroupContentCellElement groupCell = e.CellElement as GridGroupContentCellElement;
            if (groupCell != null && groupCell.Parent is GridGroupHeaderRowElement)
            {
                groupCell.MinSize = new Size(35, groupCell.Size.Height);
                RadCheckBoxElement checkBox = null;

                if (groupCell.Children.Count == 0)
                {
                    checkBox = new RadCheckBoxElement();
                    checkBox.CheckAlignment = ContentAlignment.MiddleLeft;
                    checkBox.ToggleStateChanged += new StateChangedEventHandler(checkBox_ToggleStateChanged);
                    groupCell.Children.Add(checkBox);
                    groupCell.DrawText = false;
                }
                else
                {
                    checkBox = groupCell.Children[0] as RadCheckBoxElement;
                }
                checkBox.Text = groupCell.Value.ToString();
                if (groupCell.RowInfo.Tag != null)
                {
                    updating = true;
                    checkBox.Checked = (bool)groupCell.RowInfo.Tag;
                    updating = false;
                }
            }
        }

        void checkBox_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (!updating)
            {
                RadCheckBoxElement element = sender as RadCheckBoxElement;
                GridGroupContentCellElement groupCell = ((RadCheckBoxElement)sender).Parent as GridGroupContentCellElement;
                bool state = args.ToggleState == ToggleState.On ? true : false;
                groupCell.RowInfo.Tag = state;
                foreach (GridViewRowInfo row in groupCell.RowInfo.ChildRows)
                {
                    row.Cells["Bool"].Value = state;
                }
                element.Checked = state;
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            radGridView1.Rows.Add(false, "Argentina", Guid.NewGuid().ToString().Substring(0, 5));
        }
    }
}