using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using C1.Win.Chart;
using System.Drawing.Drawing2D;
using C1.Chart;
using System.IO;

namespace FlexChartExplorer.Samples
{
    public partial class AxisLabels : UserControl
    {
        public AxisLabels()
        {
            InitializeComponent();
            InitilizeControls();

            SetupChart();
        }

        void SetupChart()
        {
            var data = GDPData.GetData();

            flexChart1.BeginUpdate();
            flexChart1.Series.Clear();
            flexChart1.DataSource = data;
            flexChart1.ChartType = ChartType.Column;
            flexChart1.Binding = "GDP";
            flexChart1.BindingX = "Country";


            var ser = new Series();
            flexChart1.Series.Add(ser);

            flexChart1.AxisX.StaggeredLines = 2;
            flexChart1.AxisY.Format = "#,,";
            flexChart1.AxisY.Title = "GDP, trillion($)";
            flexChart1.AxisY.AxisLine = false;
            flexChart1.AxisY.MajorGrid = true;
            flexChart1.AxisY.MajorTickMarks = TickMark.None;

            flexChart1.EndUpdate();
        }

        private void InitilizeControls()
        {
            flexChart1 = baseSample1.flexChart1;
            baseSample1.lblTitle.Text = Localizer.GetItem("axisLabels", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("axisLabels", "description").MakeRtf();

            baseSample1.pControls.Controls.Clear();

            var cbOver = new ComboBox()
            {
                Size = new Size(200, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };
            foreach (var item in Enum.GetValues(typeof(OverlappingLabels)))
                cbOver.Items.Add(new ComboBoxItem("Overlapping") { Text = ((OverlappingLabels)item).ToString(), Value = (int)item });
            cbOver.SelectedIndex = 0;
            cbOver.SelectedIndexChanged += (s,e) =>
                flexChart1.AxisX.OverlappingLabels = (OverlappingLabels)Enum.Parse(typeof(OverlappingLabels), (cbOver.SelectedItem as ComboBoxItem).Text);

            var lblMaxLevels = new Label()
            {
                Text = "Staggered Lines:",
                Size = new Size(100, 21),
                TextAlign = ContentAlignment.BottomRight
            };
            var numMaxLevels = new NumericUpDown()
            {
                Minimum = 1,
                Maximum = 4,
                Value = 2,
                DecimalPlaces = 0,
                Width = 30
            };
            numMaxLevels.ValueChanged += (s,e) => flexChart1.AxisX.StaggeredLines = (int)numMaxLevels.Value;

            baseSample1.pControls.Controls.Add(lblMaxLevels);
            baseSample1.pControls.Controls.Add(numMaxLevels);
            baseSample1.pControls.Controls.Add(cbOver);
        }
    }

    class GDPData
    {
        static string csv_data = @"United States,18624450
European Union,16408364
China,11232108
Japan,4936543
Germany,3479232
United Kingdom,2629188
France,2466472
India,2263792
Italy,1850735
Brazil,1798622
Canada,1529760
South Korea,1411042
Russia,1283162
Australia,1261645
Spain,1232597
Mexico,1046925
Indonesia,932448
Turkey,863390
Netherlands,777548
Switzerland,669038
Saudi Arabia,646438";

        static List<object> _data;

        static List<object> ParseData()
        {
            var list = new List<object>();
            var data = csv_data;
            var ss = data.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < ss.Length; i++)
            {
                var vals = ss[i].Split(',');
                list.Add(new { Country = vals[0], GDP = double.Parse(vals[1]) });
            }

            return list;
        }

        public static List<object> GetData()
        {
            if (_data == null)
                _data = ParseData();

            return _data;
        }
    }
}
