using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls.UI;

namespace TelerikTestSelection
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private List<TestItem> items = new List<TestItem>();

		private void MainForm_Load(object sender, EventArgs e)
		{
			var gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
			gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
			gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridRowBehavior());

			//radGridView1.AutoGenerateColumns = false;
			items.Add(new TestItem(1, "Name 1", "Comment 1", "Text 11", "Text 21", "Text 31"));
			items.Add(new TestItem(2, "Name 2", "Comment 2", "Text 12", "Text 22", "Text 32"));
			items.Add(new TestItem(3, "Name 3", "Comment 3", "Text 13", "Text 23", "Text 33"));
			items.Add(new TestItem(4, "Name 4", "Comment 4", "Text 14", "Text 24", "Text 34"));
			items.Add(new TestItem(5, "Name 5", "Comment 5", "Text 15", "Text 25", "Text 35"));
			bindSrc.DataSource = items;
			radGridView1.Refresh();
		}

		private void timerUpdate_Tick(object sender, EventArgs e)
		{
			sslStatus.Text = $"SelectedRows: {radGridView1.SelectedRows.Count}, SelectedCells: {radGridView1.SelectedCells.Count}";
		}
	}

	public class CustomGridRowBehavior : GridRowBehavior
	{
		protected override bool OnMouseDownLeft(MouseEventArgs e)
		{
			var cell = GetCellAtPoint(e.Location);

			if (cell is GridRowHeaderCellElement)
				SwitchSelectionMode(GridViewSelectionMode.FullRowSelect);

			if (cell is GridDataCellElement)
				SwitchSelectionMode(GridViewSelectionMode.CellSelect);

			return base.OnMouseDownLeft(e);
		}

		protected override bool ProcessLeftKey(KeyEventArgs keys)
		{
			SwitchSelectionMode(GridViewSelectionMode.CellSelect);

			return base.ProcessLeftKey(keys);
		}

		protected override bool ProcessRightKey(KeyEventArgs keys)
		{
			SwitchSelectionMode(GridViewSelectionMode.CellSelect);

			return base.ProcessRightKey(keys);
		}

		private void SwitchSelectionMode(GridViewSelectionMode selectionMode)
		{
			if (GridViewElement.GridControl.SelectionMode != selectionMode)
				GridViewElement.GridControl.SelectionMode = selectionMode;
		}
	}

	public class TestItem
	{
		[Browsable(false)]
		public int ID { get; set; }

		public string Name { get; set; }
		public string Comment { get; set; }
		public string Text1 { get; set; }
		public string Text2 { get; set; }
		public string Text3 { get; set; }

		public TestItem(int id, string name, string comment, string text1, string text2, string text3)
		{
			ID = id;
			Name = name;
			Comment = comment;
			Text1 = text1;
			Text2 = text2;
			Text3 = text3;
		}

		public TestItem()
		{
		}
	}
}
