using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.Charting;
using System.Windows.Browser;
using System.Globalization;
using Telerik.Windows.Controls;

namespace Integrating_SL4Chart_ASP.NET
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }


        [ScriptableMember]
        public bool Show3D
        {
            set
            {
                if (value)
                    this.Init3DChart();
                else
                    this.Init2DChart();
            }
        }

        [ScriptableMember]
        public string AxisXTitle
        {
            set
            {
                this.RadChart1.DefaultView.ChartArea.AxisX.Title = value;
            }
        }

        [ScriptableMember]
        public string AxisYTitle
        {
            set
            {
                this.RadChart1.DefaultView.ChartArea.AxisY.Title = value;
            }
        }

        [ScriptableMember]
        public bool ShowItemLabels
        {
            set
            {
                this.RadChart1.DefaultView.ChartArea.DataSeries[0].Definition.ShowItemLabels = value;
            }
        }

        [ScriptableMember]
        public bool ShowItemTooltips
        {
            set
            {
                this.RadChart1.DefaultView.ChartArea.DataSeries[0].Definition.ShowItemToolTips = value;
            }
        }

        [ScriptableMember]
        public bool HideElementsOnRotation
        {
            set
            {
                this.SetHideElementsOnRotation(value);
            }
        }

        [ScriptableMember]
        public bool ShowXAxis
        {
            set
            {
                BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();
                this.RadChart1.DefaultView.ChartArea.AxisX.Visibility = (Visibility)converter.Convert(value, typeof(Visibility), null, CultureInfo.CurrentCulture);
            }
        }

        [ScriptableMember]
        public bool ShowYAxis
        {
            set
            {
                BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();
                this.RadChart1.DefaultView.ChartArea.AxisY.Visibility = (Visibility)converter.Convert(value, typeof(Visibility), null, CultureInfo.CurrentCulture);
            }
        }

        [ScriptableMember]
        public bool ShowXAxisGridlines
        {
            set
            {
                BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();
                this.RadChart1.DefaultView.ChartArea.AxisX.MajorGridLinesVisibility = (Visibility)converter.Convert(value, typeof(Visibility), null, CultureInfo.CurrentCulture);
            }
        }

        [ScriptableMember]
        public bool ShowYAxisGridlines
        {
            set
            {
                BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();
                this.RadChart1.DefaultView.ChartArea.AxisY.MajorGridLinesVisibility = (Visibility)converter.Convert(value, typeof(Visibility), null, CultureInfo.CurrentCulture);
            }
        }

        [ScriptableMember]
        public bool ShowXAxisStriplines
        {
            set
            {
                BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();
                this.RadChart1.DefaultView.ChartArea.AxisX.StripLinesVisibility = (Visibility)converter.Convert(value, typeof(Visibility), null, CultureInfo.CurrentCulture);
            }
        }

        [ScriptableMember]
        public bool ShowYAxisStriplines
        {
            set
            {
                BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();
                this.RadChart1.DefaultView.ChartArea.AxisY.StripLinesVisibility = (Visibility)converter.Convert(value, typeof(Visibility), null, CultureInfo.CurrentCulture);
            }
        }

        [ScriptableMember]
        public string AxisXFormat
        {
            set
            {
                this.RadChart1.DefaultView.ChartArea.AxisX.DefaultLabelFormat = value;
            }
        }

        [ScriptableMember]
        public string AxisYFormat
        {
            set
            {
                this.RadChart1.DefaultView.ChartArea.AxisY.DefaultLabelFormat = value;
            }
        }

        [ScriptableMember]
        public string ItemsFormat
        {
            set
            {
                this.RadChart1.DefaultView.ChartArea.DataSeries[0].Definition.ItemLabelFormat = value;
            }
        }

        [ScriptableMember]
        public string SeriesColor
        {
            set
            {
                string c = value.Split('#')[1];
                Color col = Color.FromArgb(255, byte.Parse(c.Substring(0, 2), NumberStyles.HexNumber), byte.Parse(c.Substring(2, 2), NumberStyles.HexNumber), byte.Parse(c.Substring(4, 2), NumberStyles.HexNumber));
                this.RadChart1.DefaultView.ChartArea.DataSeries[0].Definition.Appearance.Stroke = new SolidColorBrush(col);
                this.RadChart1.DefaultView.ChartArea.DataSeries[0].Definition.Appearance.Fill = new SolidColorBrush(col);
            }
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            InitRadChart();
            HtmlPage.RegisterScriptableObject("slChartPage", this);
        }

        private void InitRadChart()
        {
            Init2DChart();
        }

        private void Init2DChart()
        {
            this.RadChart1.DefaultView.ChartArea.DataSeries.Clear();
            this.RadChart1.DefaultView.ChartArea.Extensions.Clear();
            InitChartArea<BarSeriesDefinition>();
        }

        private void Init3DChart()
        {
            this.RadChart1.DefaultView.ChartArea.DataSeries.Clear();
            InitChartArea<Bar3DSeriesDefinition>();
            CameraExtension camera = new CameraExtension() { ZoomEnabled = true };
            this.RadChart1.DefaultView.ChartArea.Extensions.Add(camera);
        }

        private void InitChartArea<TSeriesDefinition>() where TSeriesDefinition : ISeriesDefinition, new()
        {
            DataSeries ser = new DataSeries();
            ser.Definition = new TSeriesDefinition();
            FillWithSampleData(ser, 8);

            this.RadChart1.DefaultView.ChartArea.DataSeries.Add(ser);

            this.RadChart1.DefaultView.ChartArea.DataSeries[0].LegendLabel = "Revenue";
            this.RadChart1.DefaultView.ChartArea.AxisX.IsDateTime = true;
            this.RadChart1.DefaultView.ChartArea.AxisX.LabelRotationAngle = 30;
        }

        public static void FillWithSampleData(DataSeries series, int numberOfItems)
        {
            Random random = new Random((int)(series.GetHashCode() + DateTime.Now.Ticks));
            for (int i = 0; i < numberOfItems; i++)
            {
                int randomNumber = random.Next(30, 100);
                series.Add(new DataPoint { XCategory = DateTime.Today.AddDays(i).ToString(), YValue = randomNumber });
            }
        }

        private void SetHideElementsOnRotation(bool value)
        {
            if (RadChart1.DefaultView.ChartArea.Extensions.Count > 0)
                (RadChart1.DefaultView.ChartArea.Extensions[0] as CameraExtension).HidePlaneElementsDuringRotation = value;
        }
    }
}
