using ceTe.DynamicPDF;
using Consulenza.ReportWriter.Business.Entity;
using Consulenza.ReportWriter.Business.OBJ_PDF;
using Dundas.Charting.WebControl;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

namespace Consulenza.ReportWriter.Business.CHART_PDF
{
    public class CustomChartPDF : ObjectPDF
    {
        public CustomChartPDF()
        {
            ObjectType = ObjectTypePdf.CUSTOMCHART;
        }

        private Chart _chartbase = new Chart();
        private List<ObjectPDF> _elements = new List<ObjectPDF>();

        public List<ObjectPDF> Elements
        {
            get { return _elements; }
            set { _elements = value; }
        }

        private float deltaY;
        public float DeltaY
        {
            get { return deltaY; }
            set { deltaY = value; }
        }

        public Chart ChartBase
        {
            get { return _chartbase; }
            set { _chartbase = value; }
        }
        public float X
        {
            get { return _x; }
            set { _x = value; }
        }

        public float Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public float Width
        {
            get { return _width; }
            set { _width = value; }
        }

        /// <summary>
        /// Y
        /// </summary>
        public float Y
        {
            get { return _y; }
            set { _y = value; }
        }

        private float _x;
        private float _y;
        private float _width;
        private float _height;


        public Image GetImage()
        {
            using (var imgGrafico = new System.IO.MemoryStream())
            {
                _chartbase.ImageResolution = 300;
                _chartbase.Save(imgGrafico, ChartImageFormat.Png);
                return Bitmap.FromStream(imgGrafico);
            }
        }


        private Image customImage = null;
        public void SetCustomImage(Image image)
        {
            customImage = image;
        }

        protected ImagePDF ToImage(bool standarizeSize = false)
        {
            var imgGrafico = new System.IO.MemoryStream();
            if (customImage == null)
            {
                if (standarizeSize)
                {
                    ChartBase.ImageResolution = 96;
                    _chartbase.ImageResolution = 96;
                    ChartBase.Height = new System.Web.UI.WebControls.Unit(Height);
                    ChartBase.Width = new System.Web.UI.WebControls.Unit(Width);
                    ChartBase.ImageResolution = 300;
                    _chartbase.ImageResolution = 300;
                }
                else
                {
                    // default 96 DPI
                    _chartbase.ImageResolution = 300;
                }
            }
            else
            {
                customImage.Save(imgGrafico, ImageFormat.Png);
            }
            _chartbase.Save(imgGrafico, ChartImageFormat.Png);
            return new ImagePDF(_x, imgGrafico, (float)_chartbase.Height.Value / 3, (float)_chartbase.Width.Value / 3, _y)
            {
                DeltaY = this.DeltaY
            };
        }

        public override PageElement ToElement()
        {
            var image = ToImage();
            var listReturn = new List<ObjectPDF>();
            listReturn.Add(image);
            Elements.AddRange(listReturn);
            return this;
        }
    }
}
