using System.Collections.Generic;
using ceTe.DynamicPDF;
using Dundas.Charting.WebControl;
using Consulenza.ReportCommon;
using Consulenza.ReportWriter.Business.Entity;
using System.Drawing;
using Color = System.Drawing.Color;
using Font = System.Drawing.Font;

namespace Consulenza.ReportWriter.Business.CHART_PDF
{
    public class PiePDF : ChartPDF
    {
        #region Field
        public float FontLabelSize { get; set; }
        /// <summary>
        /// Imposta o recupera l'angolo di separazione della torta.
        /// Di default = 0 (Orizzontale). 90(Verticale)
        /// </summary>
        public int StartAngle { get; set; }

        /// <summary>
        /// Imposta o recupera un bool che indica se disegnare il grafico in 3D.
        /// Di default = false.
        /// </summary>
        public bool Style3DEnabled { get; set; }

        public bool UseCustomPlotting { get; set; }

        public float CustomPlottingWidth { get; set; }

        public float CustomPlottingHeight { get; set; }

        public float CustomPlottingX { get; set; }

        public float CustomPlottingY { get; set; }

        /// <summary>
        /// Ottiene o imposta l'angolo di rotazione intorno all'asse X.
        /// Aumentando si ottiene l'effetto 3D più accentuato.
        /// Di default = 5. Massimo valore consentito = 90.
        /// </summary>
        public int Style3DXAngle { get; set; }

        #endregion

        #region Costruttori

        /// <summary>
        /// Costruttore
        /// </summary>
        public PiePDF()
        {
            ObjectType = ObjectTypePdf.CHART;
            ChartType = ChartTypePdf.PIE;
            ChartBase.ChartAreas.Add("Default");
            FontLabelSize = 6;
            StartAngle = 0;
            Style3DEnabled = false;
            Style3DXAngle = 5;
        }

        public PiePDF(float x)
            : this()
        {
            X = x;
        }

        public PiePDF(float x, float scale)
            : this(x)
        {
            Scale = scale;
        }

        public PiePDF(float x, float scale, float y)
            : this(x, scale)
        {
            Y = y;
        }

        public int LabelDecimalDigitsCount = 2;
        #endregion

        #region Metodi

        /// <summary>
        /// Ritorna l'oggetto che sarà stampato sulla pagina del documento.
        /// </summary>
        /// <returns></returns>
        public override PageElement ToElement()
        {
            var listReturn = new List<ObjectPDF>();

            #region Creo il grafico

            CreateChart();

            #endregion

            #region Ritorno l'immagine contenente il grafico

            listReturn.Add(ToImage());
            TotalHeight += Height;

            #endregion

            Elements.AddRange(listReturn);

            return this;
        }

        /// <summary>
        /// Crea il grafico di tipo Pie
        /// </summary>
        private void CreateChart()
        {
            // Imposto tutte le serie aggiunte al chart come Pie.
            base.SeriesCollection.ForEach(o => o.Type = SeriesChartType.Pie);

            ChartBase.AntiAliasing = AntiAliasing.All; // AntiAliasing.Graphics and AntiAliasing.Text            
            ChartBase.TextAntiAliasingQuality = TextAntiAliasingQuality.High;

            ChartBase.ChartAreas[0].BackColor = Color.White;
            ChartBase.ChartAreas[0].BorderColor = Color.Empty;
            ChartBase.ChartAreas[0].BorderWidth = 0;

            ChartBase.ChartAreas[0].Position.Auto = false;
            ChartBase.ChartAreas[0].Position.X = 0;
            ChartBase.ChartAreas[0].Position.Y = 0;
            ChartBase.ChartAreas[0].Position.Width = 100;
            ChartBase.ChartAreas[0].Position.Height = 100;

                      
          

            // Imposto il 3D
            ChartBase.ChartAreas[0].Area3DStyle.Enable3D = Style3DEnabled;
            ChartBase.ChartAreas[0].Area3DStyle.XAngle = Style3DXAngle;

            foreach (var item in SeriesCollection)
            {
                ChartBase.Series.Add(item.Name);

                // Imposto il tipo di grafico
                ChartBase.Series[item.Name].Type = item.Type;

                // Imposto lo stile delle etichette delle barre (utilizzati se _showlabelpoints = true)
                ChartBase.Series[item.Name]["PieLabelStyle"] = "Outside";
                ChartBase.Series[item.Name]["PieStartAngle"] = StartAngle.ToString();
                // Imposto il tipo di linea dei Points. Visibile solo se ShowLabelPoints = true e PieLabelStyle="Outside"
                ChartBase.Series[item.Name]["PieLineColor"] = "Black";
                ChartBase.Series[item.Name]["PieLineArrowType"] = "None";
                ChartBase.Series[item.Name]["PieLineArrowSize"] = "1";

                // Points
                ChartBase.Series[item.Name].YValueType = ChartValueTypes.Double;
                ChartBase.Series[item.Name].Font = new Font("Verdana", FontLabelSize, FontStyle.Bold);

                var i = 0;
                foreach (var point in item.Points)
                {
                    ChartBase.Series[item.Name].Points.AddY(point.Value);

                    if (point.Value != 0.00 && ShowLabelPoints)
                        ChartBase.Series[item.Name].Points[i].Label = Helper.FormatPercentage(point.Value, LabelDecimalDigitsCount);
                    
                    if (point.Value < 1.00 && ShowLabelPoints)
                        ChartBase.Series[item.Name].Points[i]["PieLabelStyle"] = "Ellipse";
                    else
                        ChartBase.Series[item.Name].Points[i]["PieLabelStyle"] = "Outside";

                    var r = point.Color.Red;
                    var g = point.Color.Green;
                    var b = point.Color.Blue;

                    ChartBase.Series[item.Name].Points[i].Color = Color.FromArgb(r, g, b);

                    i++;
                }
            }

            // Legenda disabilitata
            ChartBase.Legend.Enabled = false;

            if (UseCustomPlotting)
            {
                ChartBase.ChartAreas[0].InnerPlotPosition.Auto = false;
                ChartBase.ChartAreas[0].InnerPlotPosition.Height = CustomPlottingHeight;
                ChartBase.ChartAreas[0].InnerPlotPosition.Width = CustomPlottingWidth;
                ChartBase.ChartAreas[0].InnerPlotPosition.X = CustomPlottingX;
                ChartBase.ChartAreas[0].InnerPlotPosition.Y = CustomPlottingY;
            }
            else
            {
                // Set the plotting area position. Coordinates of a plotting 
                // area are relative to a chart area position.
                ChartBase.ChartAreas[0].InnerPlotPosition.Auto = true;
            }
        }

        #endregion
    }
}
