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.Collections.Generic;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace testGridview260203
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public BindingList<TestClass> testData = new BindingList<TestClass>();
        int previouseIndex = -1;
        KeyValuePair<int, bool> previousePair = new KeyValuePair<int, bool>();

        public RadForm1()
        {
            InitializeComponent();
        }

        private void RadForm1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; ++i)
            {
                testData.Add(new TestClass(i, "Item " + i, "Description " + i, false));
            }

            radGridViewMain.DataSource = testData;
            previousePair = new KeyValuePair<int, bool>(-1, false);
        }

        private void radGridViewMain_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            if (e != null && e.RowElement != null)
            {
                Color defaultBackColor = Color.FromArgb(41, 41, 41);

                if (e.RowElement.IsSelected)
                {
                    e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                    e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                    e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
                }
                else
                {
                    int layerIndex = (e.RowElement.RowInfo.Cells["LayerIndex"].Value != null) ?
                        Convert.ToInt32(e.RowElement.RowInfo.Cells["LayerIndex"].Value) : -1;

                    bool isSelected = (e.RowElement.RowInfo.Cells["Selected"].Value != null) ?
                        Convert.ToBoolean(e.RowElement.RowInfo.Cells["Selected"].Value) : false;
                    if (0 <= layerIndex)
                    {
                        if (layerIndex == 4)
                        {
                            e.RowElement.DrawFill = true;
                            e.RowElement.GradientStyle = GradientStyles.Solid;
                            e.RowElement.BackColor = Color.FromArgb(100, 242, 179, 69);
                        }
                        else if (isSelected)
                        {
                            e.RowElement.DrawFill = true;
                            e.RowElement.GradientStyle = GradientStyles.Solid;
                            e.RowElement.BackColor = Color.FromArgb(41, 188, 41);

                        }
                        else
                        {
                            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
                            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                        }
                    }
                    else
                    {
                        e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
                        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                    }
                }
            }
        }

        private void radGridViewMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (previousePair.Key != -1 && previousePair.Value == false)
            {
                previousePair = new KeyValuePair<int, bool>(previousePair.Key, true);
            }

            radGridViewMain.AllowRowReorder = false;
            var cell = radGridViewMain.ElementTree.GetElementAtPoint(e.Location) as Telerik.WinControls.UI.GridDataCellElement;
            if (cell != null && cell.RowInfo != null)
            {
                if (0 <= cell.RowIndex)
                {
                    bool isSelected = Convert.ToBoolean(radGridViewMain.Rows[cell.RowIndex].Cells["Selected"].Value?.ToString());

                    previouseIndex = cell.RowIndex;
                    if (previousePair.Key == -1 && previousePair.Value == false)
                    {
                        previousePair = new KeyValuePair<int, bool>(cell.RowIndex, false);
                    }

                    radGridViewMain.ClearSelection();
                    if (radGridViewMain.CurrentRow != null)
                    {
                        radGridViewMain.CurrentRow = null;
                    }
                }
            }
            else
            {
                previouseIndex = -1;
            }
        }

        private void radGridViewMain_MouseUp(object sender, MouseEventArgs e)
        {
            bool shiftPressed = (Control.ModifierKeys & Keys.Shift) == Keys.Shift;

            bool isRangeSelection = false;
            int curIndex = -1;
            if (0 <= previouseIndex)
            {
                var cell = radGridViewMain.ElementTree.GetElementAtPoint(e.Location) as Telerik.WinControls.UI.GridDataCellElement;
                if (cell != null && cell.RowInfo != null)
                {
                    curIndex = cell.RowIndex;
                    if (shiftPressed && (previousePair.Key != -1 && previousePair.Value))
                    {
                        isRangeSelection = true;
                    }
                    else if (0 <= cell.RowIndex)
                    {
                        isRangeSelection = cell.RowIndex != previouseIndex;
                    }
                }
            }
            else
            {
                foreach (var row in radGridViewMain.Rows)
                {
                    row.Cells["Selected"].Value = false;
                }

                if (previousePair.Key != -1 && previousePair.Value)
                {
                    previousePair = new KeyValuePair<int, bool>(-1, false);
                }

                radGridViewMain.ClearSelection();
                if (radGridViewMain.CurrentRow != null)
                {
                    radGridViewMain.CurrentRow = null;
                }
                return;
            }

            if (isRangeSelection)
            {
                bool isAllSelcted = true;

                int min = Math.Min(previouseIndex, previousePair.Key);
                int max = Math.Max(previouseIndex, previousePair.Key);

                for (; min <= max; ++min)
                {
                    var row = radGridViewMain.Rows[min];
                    if (row.Tag != null)
                        continue;

                    bool v = Convert.ToBoolean(row.Cells["Selected"].Value?.ToString());
                    if (v == false)
                    {
                        isAllSelcted = false;
                        break;
                    }
                }

                min = Math.Min(previouseIndex, previousePair.Key);
                for (; min <= max; ++min)
                {
                    var row = radGridViewMain.Rows[min];
                    row.Cells["Selected"].Value = !isAllSelcted;
                }
            }
            else
            {
                var element = radGridViewMain.ElementTree.GetElementAtPoint(e.Location) as Telerik.WinControls.UI.GridDataCellElement;
                if (element != null && element.RowInfo.Tag == null)
                {
                    int layerIndex = Convert.ToInt32(element.RowInfo.Cells["LayerIndex"].Value);

                    bool v = Convert.ToBoolean(this.radGridViewMain.Rows[layerIndex].Cells["Selected"].Value?.ToString());
                    this.radGridViewMain.Rows[layerIndex].Cells["Selected"].Value = !v;
                }
            }

            if (previousePair.Key != -1 && previousePair.Value)
            {
                previousePair = new KeyValuePair<int, bool>(-1, false);
            }

            radGridViewMain.ClearSelection();
            if (radGridViewMain.CurrentRow != null)
            {
                radGridViewMain.CurrentRow = null;
            }
        }
    }

    public class TestClass
    {
        public TestClass(int layerIndex, string name, string description, bool isSelected)
        {
            this.LayerIndex = layerIndex;
            this.Name = name;
            this.Description = description;
            this.isSelected = isSelected;
        }

        public int LayerIndex { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public bool isSelected { get; set; }
    }
}
