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

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

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

		private void MainForm_Load(object sender, EventArgs e)
		{
			var col = radGridView1.Columns["colTestType"] as GridViewComboBoxColumn;
			col.ValueMember = "Value";
			col.DisplayMember = "Display";
			col.DataSource = ((TestType[])Enum.GetValues(typeof(TestType))).Select(v => new { Value = v, Display = v.ToString() }).ToList();
			radGridView1.Relations.AddSelfReference(radGridView1.MasterTemplate, "colGridID", "colGridParentID");

			items.Add(new TestParItem() { PosIndex = 0, TestType = TestType.AbsPos, R = 5, Fi = 45, Min = 11, Max = 21, Comment = "test 1", Disabled = false, GridID = 1, GridParentID = 0 });
			items.Add(new TestParItem() { PosIndex = 1, TestType = TestType.RelPos, R = 6, Fi = 46, Min = 12, Max = 22, Comment = "test 2", Disabled = false, GridID = 2, GridParentID = 0 });
			items.Add(new TestParItem() { PosIndex = 2, TestType = TestType.AbsPos, R = 7, Fi = 47, Min = 13, Max = 23, Comment = "test 3", Disabled = true, GridID = 3, GridParentID = 0 });
			items.Add(new TestParItem() { PosIndex = 3, TestType = TestType.TestSub, R = 8, Fi = 48, Min = 14, Max = 24, Comment = "test 4", Disabled = false, GridID = 4, GridParentID = 0 });
			items.Add(new TestParItem() { PosIndex = 4, TestType = TestType.TestSub, R = 9, Fi = 49, Min = 15, Max = 25, Comment = "test 5", Disabled = false, GridID = 5, GridParentID = 0 });


			items.Add(new TestParItem() { PosIndex = 3, TestType = TestType.None, R = 8, Fi = 48, Min = 14, Max = 24, Comment = "test 4", Disabled = false, GridID = 41, GridParentID = 4 });
			items.Add(new TestParItem() { PosIndex = 3, TestType = TestType.None, R = 8, Fi = 48, Min = 14, Max = 24, Comment = "test 4", Disabled = false, GridID = 42, GridParentID = 4 });
			items.Add(new TestParItem() { PosIndex = 3, TestType = TestType.None, R = 8, Fi = 48, Min = 14, Max = 24, Comment = "test 4", Disabled = false, GridID = 43, GridParentID = 4 });
			items.Add(new TestParItem() { PosIndex = 4, TestType = TestType.None, R = 8, Fi = 48, Min = 14, Max = 24, Comment = "test 5", Disabled = false, GridID = 51, GridParentID = 5 });
			items.Add(new TestParItem() { PosIndex = 4, TestType = TestType.None, R = 8, Fi = 48, Min = 14, Max = 24, Comment = "test 5", Disabled = false, GridID = 52, GridParentID = 5 });
			items.Add(new TestParItem() { PosIndex = 4, TestType = TestType.None, R = 8, Fi = 48, Min = 14, Max = 24, Comment = "test 5", Disabled = false, GridID = 53, GridParentID = 5 });

			radGridView1.DataSource = items;

			foreach (var row in radGridView1.Rows)
				row.IsExpanded = true;
		}

		private void radGridView1_DataError(object sender, GridViewDataErrorEventArgs e)
		{
			radGridView1.CancelEdit();
			MessageBox.Show("Invalid data format.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
		}

		private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
		{
			if (radGridView1.IsInEditMode)
				return;

			if ((e.ColumnIndex < 0) || (e.RowIndex < 0))
				return;

			var cell = sender as GridComboBoxCellElement;
			if (cell == null)
				return;

			radGridView1.BeginEdit();
			var editor = cell.Editor as RadDropDownListEditor;
			if (editor == null)
				return;

			var el = editor.EditorElement as RadDropDownListEditorElement;
			if (el == null)
				return;

			el.ShowPopup();
		}

		private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
		{
			if ((e.ColumnIndex < 0) || (e.RowIndex < 0))
				return;

			var item = e.Row.DataBoundItem as TestParItem;
			if (item == null)
				return;

			bool isSub = item.GridParentID != 0;
			bool hasSub = !isSub && (item.TestType == TestType.TestSub);

			switch (e.Column.Name)
			{
				case "colPosNr":
					if (isSub)
					{
						int index = item.GridID % 10;
						e.CellElement.Text = $"{item.PosNr}.{index}";
					}
					break;

				case "colR":
				case "colFi":
				case "colComment":
					if (isSub)
						e.CellElement.Text = "";
					break;

				case "colMin":
				case "colMax":
					if (hasSub)
					{
						e.CellElement.Text = "";
						//e.CellElement.DrawFill = true;
						//e.CellElement.BackColor = Color.Yellow;
					}
					break;

				case "colTestType":
					if (isSub)
					{
						int index = item.GridID % 10;
						switch (index)
						{
							case 1:
								e.CellElement.Text = "      Diameter";
								break;
							case 2:
								e.CellElement.Text = "      Height";
								break;
							case 3:
								e.CellElement.Text = "      Angle";
								break;
						}
					}
					break;
			}
		}

		private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
		{
			if ((e.ColumnIndex < 0) || (e.RowIndex < 0))
				return;

			var item = e.Row.DataBoundItem as TestParItem;
			if (item == null)
				return;

			bool isSub = item.GridParentID != 0;
			bool hasSub = !isSub && (item.TestType == TestType.TestSub);

			switch (e.Column.Name)
			{
				case "colR":
				case "colFi":
				case "colComment":
					if (isSub)
						e.Cancel = true;
					break;

				case "colMin":
				case "colMax":
					if (hasSub)
						e.Cancel = true;
					break;

				case "colTestType":
					if (isSub)
						e.Cancel = true;
					break;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			var exporter = new Telerik.WinControls.Export.GridViewSpreadExport(radGridView1);
			exporter.ExportFormat = Telerik.WinControls.Export.SpreadExportFormat.Xlsx;
			exporter.FileExportMode = Telerik.WinControls.Export.FileExportMode.CreateOrOverrideFile;
			exporter.ExportVisualSettings = true;
			exporter.RunExport(@"D:\test.xlsx", new Telerik.WinControls.Export.SpreadExportRenderer());
		}
	}
}
