using System.Xml;
#if MSCHART
using FastReport.MSChart;
using FastReport.DataVisualization.Charting;
#endif

namespace FastReport.Import.JasperReports
{
    /// <summary>
    /// Represents the JasperReports import plugin.
    /// </summary>
    public partial class JasperReportsImport : ImportBase
    {
        partial void LoadSVGImage(ReportComponentBase parent, XmlNode xmlObject)
        {
            var SVGObject = ComponentsFactory.CreateSVGObject("", parent);
            LoadReportComponentBase(SVGObject, xmlObject);
            LoadPadding(SVGObject, xmlObject);

            if (xmlObject.Attributes["rotation"] != null)
            {
                SVGObject.Angle = UnitsConverter.ConvertRotation(xmlObject.Attributes["rotation"].Value);
            }

            if (xmlObject.Attributes["scaleImage"] != null)
            {
                SVGObject.SizeMode = UnitsConverter.ConvertImageSizeMode(xmlObject.Attributes["scaleImage"].Value);
            }

            if (xmlObject["imageExpression"] != null)
                SVGObject.ImageLocation = (xmlObject["imageExpression"].InnerText).Replace("\"", "");
        }

        partial void LoadRichText(IParent band, XmlNode xmlObject)
        {
            RichObject RichObject = ComponentsFactory.CreateRichObject("", (Base)band);
            LoadReportComponentBase(RichObject, xmlObject);
            LoadPadding(RichObject, xmlObject);

            if (xmlObject["text"] != null)
                RichObject.Text = xmlObject["text"].InnerText;

            if (xmlObject["textFieldExpression"] != null)
                RichObject.Text = xmlObject["textFieldExpression"].InnerText;

        }

        partial void LoadChart(IParent band, XmlNode xmlObject)
        {
#if MSCHART
            MSChartObject chart = ComponentsFactory.CreateMSChartObject("", (Base)band);
            XmlNode chartSettings = null;
            XmlNode dataSet = null;
            XmlNode plot = null;

            foreach (XmlNode node in xmlObject)
            {
                if (node.Name == "chart" || node.Name.Contains("chartSettings"))
                    chartSettings = node;
                if (node.Name == "categorySeries" || node.Name.Contains("Dataset"))
                    dataSet = node;
                if (node.Name.ToLower().Contains("plot"))
                    plot = node;
            }

            LoadReportComponentBase(chart, chartSettings);

            Title title = new Title();
            chart.Chart.Titles.Add(title);

            if (chartSettings["chartTitle"] != null)
            {
                if (chartSettings["chartTitle"]["titleExpression"] != null)
                    title.Text = (chartSettings["chartTitle"]["titleExpression"].InnerText);

                if (chartSettings["chartTitle"]["font"] != null)
                    title.Font = ParseFont(chartSettings["chartTitle"]["font"]);
            }

            ChartArea chartArea = new ChartArea("Default");
            chart.Chart.ChartAreas.Add(chartArea);
            chartArea.AxisX = new Axis();
            chartArea.AxisX.IsMarginVisible = false;
            chartArea.AxisY = new Axis();
            chartArea.AxisY.IsMarginVisible = false;
            chartArea.AxisX2 = new Axis();
            chartArea.AxisY2 = new Axis();
            chartArea.Area3DStyle.LightStyle = LightStyle.None;

            if (plot != null)
            {
                if (plot.Attributes["backcolor"] != null)
                    chartArea.BackColor = UnitsConverter.ConvertColor(plot.Attributes["backcolor"].Value);
            }

            Legend legend = new Legend();
            chart.Chart.Legends.Add(legend);
            legend.IsDockedInsideChartArea = true;
            legend.LegendStyle = LegendStyle.Column;
            if (chartSettings["chartLegend"] != null)
            {
                if (chartSettings.Attributes["isShowLegend"] != null)
                    legend.Enabled = UnitsConverter.ConvertBool(chartSettings.Attributes["isShowLegend"].Value);

                if (chartSettings["chartLegend"]["backgroundColor"] != null)
                    legend.BackColor = UnitsConverter.ConvertColor(chartSettings["chartLegend"]["backgroundColor"].InnerText);

                if (chartSettings["chartLegend"]["textColor"] != null)
                    legend.ForeColor = UnitsConverter.ConvertColor(chartSettings["chartLegend"]["textColor"].InnerText);

                if (chartSettings["chartLegend"]["font"] != null)
                    legend.Font = ParseFont(chartSettings["chartLegend"]["font"]);
            }

            int index = 0;
            foreach (XmlNode series in dataSet)
            {
                if (series.Name != "categorySeries")
                    continue;

                MSChartSeries msSeries = chart.AddSeries(UnitsConverter.ConvertChartType(xmlObject.Name));

                if (series["seriesExpression"] != null)
                    msSeries.Name = (series["seriesExpression"].InnerText);

                if (series["categoryExpression"] != null)
                    msSeries.XValue = SwapBarackets((series["categoryExpression"].InnerText));

                if (series["valueExpression"] != null)
                    msSeries.YValue1 = SwapBarackets((series["valueExpression"].InnerText));

                try
                {
                    msSeries.SeriesSettings.Color = UnitsConverter.ConvertColor(plot["plot"].ChildNodes[index].Attributes["color"].Value);
                }
                catch { }

                index++;
            }
#endif
        }

        partial void LoadMap(IParent band, XmlNode xmlObject)
        {
            Map.MapObject map = ComponentsFactory.CreateMapObject("", (Base)band);
            LoadReportComponentBase(map, xmlObject);
        }
    }
}