using System.Diagnostics;
using Telerik.WinControls.RadControlSpy;
using Telerik.WinControls.UI;

namespace RadGanttViewIssue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTimeLine();
            InitializeData();
            InitializeDataForScrollBar();
            InitializeColumns();
            InitializeEvent();
        }

        /// <summary>
        /// Initialize the start and the end of the timeline
        /// </summary>
        private void InitializeTimeLine()
        {
            this.radGanttView1.GanttViewElement.GraphicalViewElement.TimelineStart = new DateTime(2010, 10, 9);
            this.radGanttView1.GanttViewElement.GraphicalViewElement.TimelineEnd = new DateTime(2010, 12, 10);
        }

        /// <summary>
        /// Initialize the data
        /// </summary>
        private void InitializeData()
        {
            CustomTask item1 = new CustomTask();
            item1.Start = new DateTime(2010, 10, 10);
            item1.End = new DateTime(2010, 10, 15);
            item1.Title = "Summary task.1. title";

            CustomTask subitem11 = new CustomTask();
            subitem11.Start = new DateTime(2010, 10, 10);
            subitem11.End = new DateTime(2010, 10, 12);
            subitem11.Title = "Sub-task.1.1 title";

            CustomTask subitem12 = new CustomTask();
            subitem12.Start = new DateTime(2010, 10, 12);
            subitem12.End = new DateTime(2010, 10, 15);
            subitem12.Title = "Sub-task.1.2 title";
            subitem12.HandleColor = true;

            item1.Items.Add(subitem11);
            item1.Items.Add(subitem12);

            this.radGanttView1.Items.Add(item1);

            CustomTask item2 = new CustomTask();
            item2.Start = new DateTime(2010, 10, 12);
            item2.End = new DateTime(2010, 10, 18);
            item2.Title = "Summary task.2. title";

            CustomTask subitem21 = new CustomTask();
            subitem21.Start = new DateTime(2010, 10, 12);
            subitem21.End = new DateTime(2010, 10, 13);
            subitem21.Title = "Sub-task.2.1 title";
            subitem21.HandleColor = true;

            CustomTask subitem22 = new CustomTask();
            subitem22.Start = new DateTime(2010, 10, 13);
            subitem22.End = new DateTime(2010, 10, 18);
            subitem22.Title = "Sub-task.2.2 title";
            subitem22.HandleColor = true;

            CustomTask subitem23 = new CustomTask();
            subitem23.Start = new DateTime(2010, 10, 18);
            subitem23.End = new DateTime(2010, 10, 18);
            subitem23.Title = "Sub-task.2.3 title";

            item2.Items.Add(subitem21);
            item2.Items.Add(subitem22);
            item2.Items.Add(subitem23);

            this.radGanttView1.Items.Add(item2);

            GanttViewLinkDataItem link1 = new GanttViewLinkDataItem();
            link1.StartItem = subitem11;
            link1.EndItem = subitem12;
            link1.LinkType = TasksLinkType.FinishToStart;
            this.radGanttView1.Links.Add(link1);

            GanttViewLinkDataItem link2 = new GanttViewLinkDataItem();
            link2.StartItem = subitem21;
            link2.EndItem = subitem22;
            link2.LinkType = TasksLinkType.StartToStart;
            this.radGanttView1.Links.Add(link2);

            GanttViewLinkDataItem link3 = new GanttViewLinkDataItem();
            link3.StartItem = subitem22;
            link3.EndItem = subitem23;
            link3.LinkType = TasksLinkType.FinishToStart;
            this.radGanttView1.Links.Add(link3);
        }

        /// <summary>
        /// Create items to have the vertical scrollbar
        /// </summary>
        private void InitializeDataForScrollBar()
        {
            for (int i = 1; i <= 30; i++)
            {
                CustomTask item = new CustomTask();
                item.Start = new DateTime(2010, 10, 12);
                item.End = new DateTime(2010, 10, 18);
                item.Title = $"Summary Task :: {i}";

                this.radGanttView1.Items.Add(item);
            }
        }

        /// <summary>
        /// Initialize the colums
        /// </summary>
        private void InitializeColumns()
        {
            GanttViewTextViewColumn titleColumn = new GanttViewTextViewColumn("Title");
            GanttViewTextViewColumn startColumn = new GanttViewTextViewColumn("Start");
            GanttViewTextViewColumn endColumn = new GanttViewTextViewColumn("End");

            this.radGanttView1.GanttViewElement.Columns.Add(titleColumn);
            this.radGanttView1.GanttViewElement.Columns.Add(startColumn);
            this.radGanttView1.GanttViewElement.Columns.Add(endColumn);
        }

        /// <summary>
        /// Handle events of the Gantt
        /// </summary>
        private void InitializeEvent()
        {
            this.radGanttView1.ItemElementCreating += RadGanttView1_ItemElementCreating;
            this.radGanttView1.GraphicalViewItemFormatting += RadGanttView1_GraphicalViewItemFormatting;
        }

        private void RadGanttView1_GraphicalViewItemFormatting(object sender, GanttViewGraphicalViewItemFormattingEventArgs e)
        {
            var item = e.Item as CustomTask;

            if (item == null)
                return;

            if (item.HandleColor)
            {
                //Let the color from RadGanttView1_ItemElementCreating
            }
            else
            {
                //Need to reset Value (not working)
                e.ItemElement.TaskElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            }
        }

        private void RadGanttView1_ItemElementCreating(object sender, GanttViewItemElementCreatingEventArgs e)
        {
            var item = (CustomTask)e.Item;
            if (e.Item.Items.Count == 0 && e.ViewElement is GanttViewGraphicalViewElement && item.HandleColor)
            {
                //Create custom Element for task with HandleColor
                e.ItemElement = new MyGanttViewTaskItemElement((GanttViewGraphicalViewElement)e.ViewElement, 1);
            }
        }
    }
}
