/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice 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/.
 *
 */

#include <ChartTools.hxx>
#include <docsh.hxx>
#include <drwlayer.hxx>

#include <com/sun/star/chart2/XChartDocument.hpp>
#include <com/sun/star/embed/XEmbeddedObject.hpp>
#include <chart2/AbstractPivotTableDataProvider.hxx>
#include <svx/svditer.hxx>
#include <svx/svdoole2.hxx>
#include <svx/svdpage.hxx>

using namespace css;

namespace sctools {

namespace {

chart2api::AbstractPivotTableDataProvider*
getPivotTableDataProvider(const SdrOle2Obj* pOleObject)
{
    chart2api::AbstractPivotTableDataProvider* pPivotTableDataProvider = nullptr;

    const uno::Reference<embed::XEmbeddedObject>& xObject = pOleObject->GetObjRef();
    if (xObject.is())
    {
        uno::Reference<chart2::XChartDocument> xChartDoc(xObject->getComponent(), uno::UNO_QUERY);
        if (xChartDoc.is())
        {
            pPivotTableDataProvider = dynamic_cast<chart2api::AbstractPivotTableDataProvider*>(
                                            xChartDoc->getDataProvider().get());
        }
    }
    return pPivotTableDataProvider;
}

OUString getAssociatedPivotTableName(const SdrOle2Obj* pOleObject)
{
    OUString aPivotTableName;
    chart2api::AbstractPivotTableDataProvider* pPivotTableDataProvider
        = getPivotTableDataProvider(pOleObject);
    if (pPivotTableDataProvider)
        aPivotTableName = pPivotTableDataProvider->getPivotTableName();
    return aPivotTableName;
}

} // end anonymous namespace

ChartIterator::ChartIterator(ScDocShell* pDocShell, SCTAB nTab, ChartSourceType eChartSourceType)
    : m_eChartSourceType(eChartSourceType)
{
    if (!pDocShell)
        return;
    ScDocument& rDoc = pDocShell->GetDocument();
    ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
    if (!pDrawLayer)
        return;
    SdrPage* pPage = pDrawLayer->GetPage(sal_uInt16(nTab));
    if (!pPage)
        return;
    m_oIterator.emplace(pPage, SdrIterMode::DeepNoGroups);
}

SdrOle2Obj* ChartIterator::next()
{
    if (!m_oIterator)
        return nullptr;

    SdrObject* pObject = m_oIterator->Next();
    while (pObject)
    {
        if (pObject->GetObjIdentifier() == SdrObjKind::OLE2 && ScDocument::IsChart(pObject))
        {
            SdrOle2Obj* pOleObject = static_cast<SdrOle2Obj*>(pObject);

            chart2api::AbstractPivotTableDataProvider* pPivotTableDataProvider
                = getPivotTableDataProvider(pOleObject);

            if (pPivotTableDataProvider && m_eChartSourceType == ChartSourceType::PIVOT_TABLE)
                return pOleObject;
            else if (!pPivotTableDataProvider && m_eChartSourceType == ChartSourceType::CELL_RANGE)
                return pOleObject;
        }
        pObject = m_oIterator->Next();
    }
    return nullptr;
}

SdrOle2Obj* findChartsByName(ScDocShell* pDocShell, SCTAB nTab, std::u16string_view rName, ChartSourceType eChartSourceType)
{
    if (!pDocShell)
        return nullptr;

    ChartIterator aIterator(pDocShell, nTab, eChartSourceType);

    SdrOle2Obj* pObject = aIterator.next();
    while (pObject)
    {
        uno::Reference<embed::XEmbeddedObject> xObject = pObject->GetObjRef();
        if (xObject.is())
        {
            OUString aObjectName = pDocShell->GetEmbeddedObjectContainer().GetEmbeddedObjectName(xObject);
            if (aObjectName == rName)
                return pObject;
        }
        pObject = aIterator.next();
    }
    return nullptr;
}

SdrOle2Obj* getChartByIndex(ScDocShell* pDocShell, SCTAB nTab, ::tools::Long nIndex, ChartSourceType eChartSourceType)
{
    if (!pDocShell)
        return nullptr;

    ChartIterator aIterator(pDocShell, nTab, eChartSourceType);

    SdrOle2Obj* pObject = aIterator.next();
    ::tools::Long i = 0;
    while (pObject)
    {
        if (i == nIndex)
        {
            return pObject;
        }

        i++;
        pObject = aIterator.next();
    }
    return nullptr;
}

std::vector<SdrOle2Obj*> getAllPivotChartsConnectedTo(std::u16string_view sPivotTableName, ScDocShell& rDocShell)
{
    std::vector<SdrOle2Obj*> aObjects;

    ScDocument& rDocument = rDocShell.GetDocument();
    ScDrawLayer* pModel = rDocument.GetDrawLayer();
    if (!pModel)
        return aObjects;

    sal_uInt16 nPageCount = pModel->GetPageCount();
    for (sal_uInt16 nPageNo = 0; nPageNo < nPageCount; nPageNo++)
    {
        SdrPage* pPage = pModel->GetPage(nPageNo);
        if (!pPage)
            continue;

        ChartIterator aIterator(&rDocShell, nPageNo, ChartSourceType::PIVOT_TABLE);
        SdrOle2Obj* pObject = aIterator.next();
        while (pObject)
        {
            if (sPivotTableName == getAssociatedPivotTableName(pObject))
            {
                aObjects.push_back(pObject);
            }
            pObject = aIterator.next();
        }
    }
    return aObjects;
}

} // end sctools

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