using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DashboardModel;
using C1.Win.C1FlexGrid;
using System.Globalization;

namespace DashboardWinForms
{
    public partial class Tasks : UserControl
    {

        public Tasks()
        {
            InitializeComponent();
            Text = Strings.Resource.TaskPage_Title;
            c1DockingTabPage1.Text = Strings.Resource.AllTasks_Header;
            c1DockingTabPage2.Text = Strings.Resource.InProgress_Header;
            c1DockingTabPage3.Text = Strings.Resource.Completed_Header;
            c1DockingTabPage4.Text = Strings.Resource.Deferred_Header;
            c1DockingTabPage5.Text = Strings.Resource.Urgent_Header;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            var selector = new ValRangeSelector() { Dock = DockStyle.Bottom };
            selector.DateRangeChanged += Selector_DateRangeChanged;
            Controls.Add(selector);
            c1DockingTab1_TabClick(null, EventArgs.Empty);
            _flex.Groups = "AssignedTo";
            _flex.Grid.DrawMode = DrawModeEnum.OwnerDraw;
            _flex.Grid.OwnerDrawCell += new OwnerDrawCellEventHandler(_flex_OwnerDrawCell);
            _flex.Grid.Resize += Grid_Resize;
        }

        private void Selector_DateRangeChanged(object sender, DateRangeChangedEventArgs e)
        {
            DataService.GetService().DateRange = e.NewValue;
            c1DockingTab1_TabClick(null, EventArgs.Empty);
        }

        void _flex_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
        {
            if (_flex.Grid.Cols[e.Col].UserData != null && e.Row >= _flex.Grid.Rows.Fixed)
            {
                double value;
                if (double.TryParse(_flex.Grid.GetDataDisplay(e.Row, e.Col), NumberStyles.Any, CultureInfo.CurrentCulture, out value))
                {
                    // calculate bar extent
                    Rectangle rc = e.Bounds;
                    double max = (double)_flex.Grid.Cols[e.Col].UserData;
                    rc.Width = (int)(_flex.Grid.Cols[e.Col].WidthDisplay * value / max);

                    // draw background
                    e.DrawCell(DrawCellFlags.Background | DrawCellFlags.Border);

                    // draw bar
                    rc.Inflate(-2, -2);
                    e.Graphics.FillRectangle(Brushes.Gold, rc);
                    rc.Inflate(-1, -1);
                    e.Graphics.FillRectangle(Brushes.LightGoldenrodYellow, rc);
                    e.Text = (value * 100).ToString() + "%";

                    // draw cell content
                    e.DrawCell(DrawCellFlags.Content);
                }
            }
        }

        private void c1DockingTab1_TabClick(object sender, EventArgs e)
        {
            int selectedIndex = c1DockingTab1.SelectedIndex;
            DataService.GetService().CampainTaskType = (CampainTaskType)selectedIndex;
            _flex.Parent.Tag = _flex.Groups;
            _flex.Groups = "";
            _flex.Grid.DataSource = DataService.GetService().CampainTaskCollction.Cast<CampainTaskItem>().ToList();
            int r1 = _flex.Grid.Rows.Fixed;
            int r2 = _flex.Grid.Rows.Count - 1;
            if (r2 > 1)
            {
                Column col = _flex.Grid.Cols["Complete"];
                double max = _flex.Grid.Aggregate(AggregateEnum.Max, r1, col.Index, r2, col.Index);
                col.UserData = max;
                col.TextAlign = TextAlignEnum.CenterCenter;
            }
            _flex.Parent = c1DockingTab1.SelectedTab;
            _flex.Groups = (string)_flex.Parent.Tag ?? "AssignedTo";
            Grid_Resize(null, EventArgs.Empty);
        }

        private void Grid_Resize(object sender, EventArgs e)
        {
            for (int i = 1; i < _flex.Grid.Cols.Count; i++)
                _flex.Grid.Cols[i].Width = (_flex.Grid.Width - 50) / 4;
        }

    }

}
