/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the Collabora Office project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_OOX_EXPORT_CHARTEXPORT_HXX
#define INCLUDED_OOX_EXPORT_CHARTEXPORT_HXX

#include <set>
#include <vector>

#include <com/sun/star/uno/Reference.hxx>
#include <cpo/uno/Sequence.hxx>
#include <com/sun/star/chart2/XFormattedString.hpp>
#include <oox/dllapi.h>
#include <oox/export/drawingml.hxx>
#include <oox/export/utils.hxx>
#include <oox/token/tokens.hxx>
#include <rtl/ustring.hxx>
#include <sal/types.h>
#include <sax/fshelper.hxx>

namespace com::sun::star {
    namespace beans {
        class XPropertySet;
    }
    namespace chart {
        class XDiagram;
        class XChartDocument;
    }
    namespace chart2 {
        struct RelativePosition;
        struct RelativeSize;
        class XDiagram;
        class XChartDocument;
        class XDataSeries;
        class XChartType;
        namespace data
        {
            class XDataSequence;
            class XLabeledDataSequence;
        }
    }
    namespace drawing {
        class XShape;
    }
    namespace frame {
        class XModel;
    }
}

namespace oox {
namespace core {
    class XmlFilterBase;
}}

namespace model {
    struct StyleRef;
    struct FontRef;
    struct StyleEntry;
}

namespace oox::drawingml {

struct LabelPlacementParam;

enum AxisType
{
    AXIS_PRIMARY_X,
    AXIS_PRIMARY_Y,
    AXIS_PRIMARY_Z,
    AXIS_SECONDARY_X,
    AXIS_SECONDARY_Y,
    AXIS_CATEGORY, // used for chartex
    AXIS_VALUE     // used for chartex
};

struct AxisIdPair{
    AxisType nAxisType;
    sal_Int32 nAxisId;
    sal_Int32 nCrossAx;

    AxisIdPair(AxisType nType, sal_Int32 nId, sal_Int32 nAx)
        : nAxisType(nType)
        , nAxisId(nId)
        , nCrossAx(nAx)
    {}
};

/**
 A helper container class to collect the chart data point labels and the address
 of the cell[range] from which the labels are sourced if that is the case. This
 is then used to write the label texts under the extension tag <c15:datalabelsRange>.
 */
class DataLabelsRange
{
public:

    /// type of the internal container that stores the indexed label text.
    typedef std::map<sal_Int32, OUString> LabelsRangeMap;

    /// Returns whether the container is empty or not.
    bool empty() const;
    /// Returns the count of labels stored.
    size_t count() const;
    /// Indicates whether the container has a label with index specified by nIndex.
    bool hasLabel(sal_Int32 nIndex) const;
    /// Returns the address of the cell[range] from which label contents are sourced.
    const OUString & getRange() const;

    /// Sets the address of the cell[range] from which label contents are sourced.
    void setRange(const OUString& rRange);
    /// Adds a new indexed label text.
    void setLabel(sal_Int32 nIndex, const OUString& rText);

    LabelsRangeMap::const_iterator begin() const;
    LabelsRangeMap::const_iterator end() const;

private:
    OUString         maRange;
    LabelsRangeMap   maLabels;
};


class ChartExport final : public DrawingML {

public:
    // first: data sequence for label, second: data sequence for values.
    typedef ::std::vector< AxisIdPair > AxisVector;
    bool mbLinkToExternalData = true;

private:
    struct ChartDimInfo
    {
        OUString sFormula;
        OUString sNFormula;
        OUString sDimTypeStr;   // "val", "cat", "size", etc.
        bool bIsNumeric = false;
        bool bHasInfo = false;  // true if chartex properties were found
    };


    sal_Int32           mnXmlNamespace;
    sal_Int32           mnSeriesCount;
    css::uno::Reference< css::frame::XModel > mxChartModel;
    css::uno::Reference< css::chart::XDiagram > mxDiagram;
    css::uno::Reference< css::chart2::XDiagram > mxNewDiagram;
    std::shared_ptr<URLTransformer> mpURLTransformer;

    // members filled by InitRangeSegmentationProperties (retrieved from DataProvider)
    bool mbHasCategoryLabels; //if the categories are only automatically generated this will be false

    //css::uno::Reference< css::drawing::XShapes > mxAdditionalShapes;
    css::uno::Reference< css::chart2::data::XDataSequence > mxCategoriesValues;

    AxisVector          maAxes;
    bool                mbHasZAxis;
    bool                mbIs3DChart;
    bool                mbStacked;
    bool                mbPercent;
    bool                mbHasDateCategories;

    std::set<sal_Int32> maExportedAxis;

private:
    sal_Int32 getChartType();

    cpo::uno::Sequence< cpo::uno::Sequence< rtl::OUString > > getSplitCategoriesList(const OUString& rRange);

    OUString parseFormula( const OUString& rRange );
    void InitPlotArea();

    void ExportContent_(bool bIsChartex);
    void exportChartSpace( const css::uno::Reference<
                           css::chart::XChartDocument >& rChartDoc,
                           bool bIncludeTable,
                           bool bIsChartex);
    void exportChart( const css::uno::Reference<
                          css::chart::XChartDocument >& rChartDoc,
                          bool bIsChartex);
    void exportData_chartex( const css::uno::Reference<
                              css::chart::XChartDocument >& rChartDoc);
    void exportExternalData(bool bIsChartex);
    void exportLegend( const css::uno::Reference<
                          css::chart::XChartDocument >& rChartDoc,
                          bool bIsChartex);
    void exportTitle( const css::uno::Reference< css::drawing::XShape >& xShape,
                        bool bIsChartex,
                          const cpo::uno::Sequence< css::uno::Reference< css::chart2::XFormattedString > >& xFormattedSubTitle =
                          cpo::uno::Sequence< css::uno::Reference< css::chart2::XFormattedString > >() );
    void exportPlotArea( const css::uno::Reference<
                             css::chart::XChartDocument >& rChartDoc,
                             bool bIsChartex);
    void exportAdditionalShapes( const css::uno::Reference<css::chart::XChartDocument >& rChartDoc );
    void exportFill( const css::uno::Reference< css::beans::XPropertySet >& xPropSet );
    void exportSolidFill(const css::uno::Reference<css::beans::XPropertySet>& xPropSet);
    void exportGradientFill( const css::uno::Reference< css::beans::XPropertySet >& xPropSet );
    void exportBitmapFill( const css::uno::Reference< css::beans::XPropertySet >& xPropSet );
    void exportHatch(const css::uno::Reference<css::beans::XPropertySet>& xPropSet);
    void exportDataTable( );
    ChartDimInfo getChartDimInfo( const css::uno::Reference<css::chart2::data::XDataSequence>& xSeq);

    void exportAreaChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportBarChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportBubbleChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportDoughnutChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    // Output a chartex chart, other than RegionMap
    void exportChartex( const css::uno::Reference< css::chart2::XChartType >& xChartType,
            const char *sTypeName );
    // Output a RegionMap chartex chart
    void exportRegionMapChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportLineChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportOfPieChart( const css::uno::Reference< css::chart2::XChartType >&
            xChartType, const char* s_subtype, double nSplitPos );
    void exportPieChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportRadarChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportScatterChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportScatterChartSeries( const css::uno::Reference< css::chart2::XChartType >& xChartType,
            const cpo::uno::Sequence<css::uno::Reference<css::chart2::XDataSeries>>* pSeries);
    void exportStockChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportSurfaceChart( const css::uno::Reference< css::chart2::XChartType >& xChartType );
    void exportHiLowLines();
    void exportUpDownBars(const css::uno::Reference< css::chart2::XChartType >& xChartType );

    void exportAllSeries(const css::uno::Reference<css::chart2::XChartType>& xChartType, bool& rPrimaryAxes);
    void exportSeries_chart(const css::uno::Reference< css::chart2::XChartType >& xChartType,
            const cpo::uno::Sequence<css::uno::Reference<css::chart2::XDataSeries> >& rSeriesSeq,
            bool& rPrimaryAxes);
    void exportSeries_chartex(const css::uno::Reference< css::chart2::XChartType >& xChartType,
            const cpo::uno::Sequence<css::uno::Reference<css::chart2::XDataSeries> >& rSeriesSeq,
            const char* sTypeName);

    void exportVaryColors(const css::uno::Reference<css::chart2::XChartType>& xChartType);
    void exportCandleStickSeries(
        const cpo::uno::Sequence<css::uno::Reference<css::chart2::XDataSeries>>& aSeriesSeq,
        bool& rPrimaryAxes);

    void exportSeriesText(
        const css::uno::Reference< css::chart2::data::XDataSequence >& xValueSeq, bool bIsChartex );
    void exportSeriesCategory(
        const css::uno::Reference< css::chart2::data::XDataSequence >& xValueSeq, sal_Int32 nValueType = XML_cat );
    void exportSeriesValues(
        const css::uno::Reference< css::chart2::data::XDataSequence >& xValueSeq, sal_Int32 nValueType = XML_val );
    void exportShapeProps( const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
            sal_Int32 nNS);
    void exportDataPoints(
        const css::uno::Reference< css::beans::XPropertySet >& xSeriesProperties,
        sal_Int32 nSeriesLength, sal_Int32 eChartType );
    void exportDataLabels( const css::uno::Reference<css::chart2::XDataSeries>& xSeries, sal_Int32 nSeriesLength,
        sal_Int32 eChartType, DataLabelsRange& rDLblsRange,
        bool bIsChartex);
    void exportGrouping( bool isBar = false );
    void exportTrendlines( const css::uno::Reference< css::chart2::XDataSeries >& xSeries );
    void exportMarker( const css::uno::Reference< css::beans::XPropertySet >& xPropSet );
    void exportSmooth();
    void exportFirstSliceAng();

    void exportErrorBar(const css::uno::Reference< css::beans::XPropertySet >& xErrorBarProps,
            bool bYError);

    void exportManualLayout(const css::chart2::RelativePosition& rPos, const css::chart2::RelativeSize& rSize, const bool bIsExcludingDiagramPositioning);

    void exportAxes_chart();
    void exportAxes_chartex();
    void exportAxis(const AxisIdPair& rAxisIdPair,
            bool bIsChartex);
    void exportOneAxis_chart(
        const css::uno::Reference< css::beans::XPropertySet >& xAxisProp,
        const css::uno::Reference< css::drawing::XShape >& xAxisTitle,
        const css::uno::Reference< css::beans::XPropertySet >& xMajorGrid,
        const css::uno::Reference< css::beans::XPropertySet >& xMinorGrid,
        sal_Int32 nAxisType,
        const char* sAxisPos,
        const AxisIdPair& rAxisIdPair);
    void exportOneAxis_chartex(
        const css::uno::Reference< css::beans::XPropertySet >& xAxisProp,
        const css::uno::Reference< css::drawing::XShape >& xAxisTitle,
        const css::uno::Reference< css::beans::XPropertySet >& xMajorGrid,
        const css::uno::Reference< css::beans::XPropertySet >& xMinorGrid,
        sal_Int32 nAxisType,
        const AxisIdPair& rAxisIdPair);
    void createAxes_chart(bool bPrimaryAxes, bool bCheckCombinedAxes);
    void createAxes_chartex(
        const css::uno::Reference<css::chart2::XDataSeries>& xSeries);
    void exportView3D();
    bool isDeep3dChart();

    // Namespace abbreviations used in chartex charts
    enum class NamespaceAbbrev
    {
        NONE,   // not chartex
        CX1,    // cx1 = "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
        CX2,    // cx2 = "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"
        CX4     // cx4 = "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"
    };

    // Determine if (at least one) chart exported is from the 2014 chartex
    // schema. If not, all are from the 2006 chart schema.
    bool isChartexNotChartNS(NamespaceAbbrev *peNS) const;

    void exportMissingValueTreatment(const css::uno::Reference<css::beans::XPropertySet>& xPropSet);

    OUString getNumberFormatCode(sal_Int32 nKey) const;

    // Functions for style file output
    void outputStyleEntry(::sax_fastparser::FSHelperPtr pFS,
            sal_Int32 nElTokenId, model::StyleEntry& aEntry);
    void outputStyleRef(::sax_fastparser::FSHelperPtr pFS,
            sal_Int32 nElTokenId, const model::StyleRef& aColor);
    void outputFontRef(::sax_fastparser::FSHelperPtr pFS,
            sal_Int32 nElTokenId, const model::FontRef& aColor);

    void writeLabelProperties(
        const css::uno::Reference<css::beans::XPropertySet>& xPropSet, const LabelPlacementParam& rLabelParam,
        sal_Int32 nLabelIndex, DataLabelsRange& rDLblsRange,
        bool bIsChartex);

    static void writeChartDim(const ::sax_fastparser::FSHelperPtr& pFS, const ChartDimInfo& rInfo);
public:

    OOX_DLLPUBLIC ChartExport( sal_Int32 nXmlNamespace, ::sax_fastparser::FSHelperPtr pFS, css::uno::Reference< css::frame::XModel > const & xModel,
                 ::oox::core::XmlFilterBase* pFB, DocumentType eDocumentType );

    OOX_DLLPUBLIC void SetURLTranslator(const std::shared_ptr<URLTransformer>& pTransformer);

    const css::uno::Reference< css::frame::XModel >& getModel() const { return mxChartModel; }

    OOX_DLLPUBLIC void WriteChartObj( const css::uno::Reference< css::drawing::XShape >& xShape, sal_Int32 nID, sal_Int32 nChartCount );
    void exportTextProps(const css::uno::Reference< css::beans::XPropertySet >& xPropSet, bool bIsChartex);

    OOX_DLLPUBLIC void ExportContent();
    void InitRangeSegmentationProperties(
        const css::uno::Reference<
            css::chart2::XChartDocument > & xChartDoc );

    OOX_DLLPUBLIC OUString GetExternalDataPath() const;
};

}

#endif // INCLUDED_OOX_EXPORT_CHARTEXPORT_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
