using System.Windows.Media;
using System.Collections.Generic;

namespace DrillDown
{
    public class DrillDownViewModel
    {
        private DataLayer _layer;
        private SunburstDataLayer _sunburstLayer;
        private TreemapDataLayer _treemapLayer;
        private string _groupBy = "Country,City";

        public List<string> ChartTypes
        {
            get
            {
                return new List<string>() { "Column", "Pie" };
            }
        }

        public Dictionary<string, string> Groups
        {
            get
            {
                var groups = new Dictionary<string, string>();
                groups.Add("Country and City", "Country,City");
                groups.Add("Country and Year", "Country,Year");
                groups.Add("Year and Country", "Year,Country");
                groups.Add("Country, City and Year", "Country,City,Year");
                groups.Add("Year, Country and City", "Year,Country,City");

                return groups;
            }
        }

        public Dictionary<string, AggregateType> Aggregates
        {
            get
            {
                var aggTypes = new Dictionary<string, AggregateType>();
                aggTypes.Add("Sum", AggregateType.Sum);
                aggTypes.Add("Count of Non-Null", AggregateType.Cnt);
                aggTypes.Add("Average", AggregateType.Avg);
                aggTypes.Add("Maximum", AggregateType.Max);
                aggTypes.Add("Minimum", AggregateType.Min);
                aggTypes.Add("Deviation", AggregateType.Rng);
                aggTypes.Add("Standard Deviation", AggregateType.Std);
                aggTypes.Add("Variance", AggregateType.Var);
                aggTypes.Add("Population Standard Deviation", AggregateType.StdPop);
                aggTypes.Add("Population variance", AggregateType.VarPop);

                return aggTypes;
            }
        }

        public DataLayer DataLayer
        {
            get
            {
                if (_layer == null)
                {
                    _layer = new DataLayer(DataService.Instance.GetData(500))
                    {
                        Binding = "Value",
                        GroupBy = GroupBy
                    };
                }
                return _layer;
            }
        }

        public string GroupBy
        {
            get { return _groupBy; }
            set
            {
                _groupBy = value;
                DataLayer.GroupBy = _groupBy;
            }
        }

        public SunburstDataLayer SunburstDataLayer
        {
            get
            {
                if (_sunburstLayer == null)
                {
                    _sunburstLayer = new SunburstDataLayer(DataService.Instance.GetSunburstData())
                    {
                        Binding = "Value",
                        BindingX = "Year,Quarter,Month",
                        CustomPalette = new List<Brush>()
                        {
                            GetBrush("#ff88bde6"),
                            GetBrush("#fffbb258"),
                            GetBrush("#ff90cd97"),
                            GetBrush("#fff6aac9"),
                            GetBrush("#ffbfa554"),
                            GetBrush("#ffbc99c7"),
                            GetBrush("#ffeddd46"),
                        }
                    };
                }
                return _sunburstLayer;
            }
        }

        public TreemapDataLayer TreemapDataLayer
        {
            get
            {
                if (_treemapLayer == null)
                {
                    _treemapLayer = new TreemapDataLayer(DataService.Instance.GetTreemapData());
                }
                return _treemapLayer;
            }
        }

        private Brush GetBrush(string clr)
        {
            var color = (Color)ColorConverter.ConvertFromString(clr);
            return new SolidColorBrush() { Color = color, Opacity = 0.7 };
        }
    }
}
