using C1.FlexMap;
using C1.Win.Chart;
using C1.Win.Map;
using DashboardModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Forms;

namespace DashboardWinForms
{
    public partial class Analysis : UserControl
    {
        private C1.Win.Map.VectorLayer _vectorLayer;

        public Analysis()
        {
            InitializeComponent();
            Text = Strings.Resource.AnalysisPage_Title;
            labProdWise.Text = Strings.Resource.ProductWise_Header;
            labRegionWise.Text = Strings.Resource.RegionWise_Header;
        }

        private void Analysis_Load(object sender, EventArgs e)
        {
            _vectorLayer = new C1.Win.Map.VectorLayer() { LabelVisibility = LabelVisibility.Visible };
            _vectorLayer.LabelStyle.Font = new Font(Font.FontFamily, 7);

            Map.TileLayer.TileSource = new VirtualEarthRoadSource();
            Map.Layers.Add(_vectorLayer);

            var engine = OlapPanel.FlexPivotEngine;
            engine.DataSource = DataService.GetService().ProductWiseSaleCollection;
            OlapGrid.DataSource = OlapPanel;
            engine.RowFields.Add("Category");
            engine.RowFields.Add("Product");
            engine.Fields["Category"].Width = 70;
            engine.Fields["Product"].Width = 120;
            engine.ColumnFields.Add("Region");
            engine.ValueFields.Add("Sale");
            engine.ValueField.Format = "C";

            var selector = new ValRangeSelector() { Dock = DockStyle.Bottom };
            selector.DateRangeChanged += (s, ev) =>
            {
                DataService.GetService().DateRange = ev.NewValue;
                UpdataMapMark();

                chartOpportunities.DataSource = DataService.GetService().OpportunityItemList;
                chartOpportunities.Header.Content = Strings.Resource.Opportunities_Header;
                chartOpportunities.Header.Style.Font = new Font(Font.FontFamily, 14);
                chartOpportunities.Header.HorizontalAlignment = HorizontalAlignment.Left;
                chartOpportunities.Series.Clear();
                var series = new Series();
                series.Binding = series.Name = "Sales";
                chartOpportunities.Series.Add(series);
                chartOpportunities.BindingX = "Level";
                chartOpportunities.DataLabel.Position = C1.Chart.LabelPosition.Center;
                chartOpportunities.DataLabel.Content = "{}{y:C0}";
                chartOpportunities.ChartType = C1.Chart.ChartType.Funnel;
            };
            Controls.Add(selector);
            c1SplitContainer1.Dock = DockStyle.Fill;
        }

        void UpdataMapMark()
        {
            _vectorLayer.Items.Clear();
            var regionSalesCollection = new ListCollectionView(DataService.GetService().GetRegionWiseSales());
            if (regionSalesCollection.Count == 0)
                return;
            regionSalesCollection.SortDescriptions.Add(new SortDescription("Sales", ListSortDirection.Descending));
            double maxValue = (regionSalesCollection.GetItemAt(0) as RegionSaleItem).Sales;
            foreach (RegionSaleItem sales in regionSalesCollection)
            {
                C1.Win.Map.VectorPlacemark mark = new C1.Win.Map.VectorPlacemark();
                mark.Geometry = new GeoPoint(sales.Locat.X, sales.Locat.Y);
                mark.Marker.Size = CreateMarkBySale(sales.Sales, maxValue);
                mark.Marker.Shape = MarkerShape.Circle;
                mark.Marker.Caption = string.Format("{0:C}", sales.Sales);
                mark.Marker.LabelPosition = LabelPosition.Center;
                mark.Style.Stroke.Color = sales.Profit > 0 ? Color.Orange : Color.RoyalBlue;
                mark.Style.BackColor = mark.Style.Stroke.Color;
                _vectorLayer.Items.Add(mark);
            }
        }

        SizeF CreateMarkBySale(double sales, double maxValue)
        {
            float delta = (float)(30 * sales / maxValue);
            return new SizeF(delta, delta);
        }

    }
}
