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 C1.Win.Chart;
using DashboardModel;
using C1.Win.C1Gauge;
using C1.Win.Layout;

namespace DashboardWinForms
{
    public partial class Dashboard : UserControl
    {
        const string Fmt = "#,##0,,.##M";

        public Dashboard()
        {
            InitializeComponent();
            Text = Strings.Resource.DashboardPage_Title;
        }

        private void Dashboard_Load(object sender, EventArgs e)
        {
            Visible = false;
            var selector = new ValRangeSelector() { Dock = DockStyle.Bottom };
            Series series;
            selector.DateRangeChanged += (s, ev) =>
            {
                DataService.GetService().DateRange = ev.NewValue;

                panGauges1.Controls.Clear();
                new Gauges(DataService.GetService().CategorySalesVsGoal) { Dock = DockStyle.Fill }.Parent = panGauges1;
                labGauges1.Text = Strings.Resource.CategorySales_Header;
                labUnits1.Text = Strings.Resource.Unit_Text;

                chartSaleTopProd.DataSource = DataService.GetService().GetTopSaleProductList(3);
                chartSaleTopProd.Header.Content = Strings.Resource.TopSaleProducts_Header;
                chartSaleTopProd.Header.Style.Font = new Font(Font.FontFamily, 14);
                chartSaleTopProd.Header.HorizontalAlignment = HorizontalAlignment.Left;
                chartSaleTopProd.AxisY.Format = Fmt;
                chartSaleTopProd.Series.Clear();
                series = new Series();
                series.Binding = "Sales";
                chartSaleTopProd.BindingX = "Name";
                chartSaleTopProd.Series.Add(series);
                chartSaleTopProd.ChartType = C1.Chart.ChartType.Column;

                labTopSaleCustomers.Text = Strings.Resource.TopSaleCustomer_Header;
                gridTopSaleCustomer.DataSource = DataService.GetService().GetTopSaleCustomerList(7);
                gridTopSaleCustomer.Cols.Count = 4;
                gridTopSaleCustomer.Cols["Sales"].Format = "C0";
                gridTopSaleCustomer.Cols["Profit"].Format = "C0";
                gridTopSaleCustomer.AutoSizeCols();
                gridTopSaleCustomer.Cols[0].WidthDisplay = 30;

                panGauges2.Controls.Clear();
                new Gauges(DataService.GetService().RegionSalesVsGoal) { Dock = DockStyle.Fill }.Parent = panGauges2;
                labGauges2.Text = Strings.Resource.RegionSales_Header;
                labUnits2.Text = Strings.Resource.Unit_Text;
            };
            Controls.Add(selector);
            c1SplitContainer1.Dock = DockStyle.Fill;

            chartCurrProfitVSPriorProfit.DataSource = DataService.GetService().BudgetItemList;
            chartCurrProfitVSPriorProfit.Header.Content = Strings.Resource.BudgetVSProfit_Header;
            chartCurrProfitVSPriorProfit.Header.Style.Font = new Font(Font.FontFamily, 14);
            chartCurrProfitVSPriorProfit.Header.HorizontalAlignment = HorizontalAlignment.Left;
            chartCurrProfitVSPriorProfit.AxisY.Format = Fmt;
            series = new Series();
            series.Binding = "Profit";
            series.Name = Strings.Resource.CurrentYear_Series;
            chartCurrProfitVSPriorProfit.Series.Add(series);
            series = new Series();
            series.Binding = "ProirProfit";
            series.Name = Strings.Resource.PriorYear_Series;
            chartCurrProfitVSPriorProfit.Series.Add(series);
            chartCurrProfitVSPriorProfit.BindingX = "Date";
            
            chartCurrProfitVSPriorProfit.ChartType = C1.Chart.ChartType.LineSymbols;
            chartCurrProfitVSPriorProfit.Legend.Position = C1.Chart.Position.Top;

            chartSalesVsProfit.DataSource = DataService.GetService().BudgetItemList;
            chartSalesVsProfit.Header.Content = Strings.Resource.SalesVsProfit_Header;
            chartSalesVsProfit.Header.Style.Font = new Font(Font.FontFamily, 14);
            chartSalesVsProfit.Header.HorizontalAlignment = HorizontalAlignment.Left;
            series = new Series();
            series.Binding = "Sales";
            series.Name = Strings.Resource.Sale_Series;
            series.AxisY = new Axis() { Position = C1.Chart.Position.Right, Title = Strings.Resource.Sale_Series, Format = Fmt };
            chartSalesVsProfit.Series.Add(series);
            series = new Series();
            series.Binding = "Profit";
            series.Name = Strings.Resource.Profit_Series;
            series.ChartType = C1.Chart.ChartType.Line;
            series.AxisY = new Axis() { Position = C1.Chart.Position.Left, Title = Strings.Resource.Profit_Series, Format = Fmt };
            chartSalesVsProfit.Series.Add(series);
            chartSalesVsProfit.BindingX = "Date";
            gridTopSaleCustomer.Visible = true;
            var dashboardLayout = new C1DashboardLayout();
            dashboardLayout.Options.ToolIconAppearance = ToolIconAppearance.UpperRight;
            dashboardLayout.AttachToLayout(new SplitLayout(c1SplitContainer1));
            Visible = true;
        }

    }
}
