/* -*- 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/.
 */

#pragma once

#include <string>
#include <utility>
#include <vector>
#include <map>

#include <tools/ref.hxx>
#include "rtfvalue.hxx"

namespace writerfilter::rtftok
{
using RTFSprmsImplBase = std::vector<std::pair<Id, RTFValue::Pointer_t>>;

/// The payload of RTFSprms which is only copied on write.
class RTFSprmsImpl : public RTFSprmsImplBase, public virtual SvRefBase
{
};

enum class RTFConflictPolicy
{
    Overwrite, ///< If an existing key is found, overwrite it in place.
    Append, ///< Always append the value to the end of the list.
    Ignore, ///< If the key is already in the list, then ignore, otherwise append.
    ReplaceAtStart, ///< Always prepend the value to the start of the list and remove existing entries.
};

/// A list of RTFSprm with a copy constructor that performs a deep copy.
class RTFSprms : public virtual SvRefBase
{
public:
    using Pointer_t = tools::SvRef<RTFSprms>;
    using Entry_t = std::pair<Id, RTFValue::Pointer_t>;
    using Iterator_t = std::vector<Entry_t>::iterator;
    using ReverseIterator_t = std::vector<Entry_t>::reverse_iterator;
    RTFSprms();
    ~RTFSprms() override;

    enum class CopyForWrite;
    RTFSprms(RTFSprms const& source, CopyForWrite);

    RTFSprms(RTFSprms const&) = default;
    RTFSprms(RTFSprms&&) = default;
    RTFSprms& operator=(RTFSprms const&) = default;
    RTFSprms& operator=(RTFSprms&&) = default;

    RTFValue::Pointer_t find(Id nKeyword, bool bFirst = true, bool bForWrite = false);
    /// Does the same as ->push_back(), except that it can overwrite or ignore existing entries.
    void set(Id nKeyword, const RTFValue::Pointer_t& pValue,
             RTFConflictPolicy ePolicy = RTFConflictPolicy::Overwrite);
    bool erase(Id nKeyword);
    void eraseLast(Id nKeyword);
    /// Removes elements which are already in the reference set.
    /// Also insert default values to override attributes of style
    /// (yes, really; that's what Word does).
    /// @param bImplicitPPr implicit dereference of top-level pPr SPRM
    /// @param pDirect pointer to the root of the direct formatting SPRM tree, if any
    RTFSprms cloneAndDeduplicate(RTFSprms& rReference, Id nStyleType, bool bImplicitPPr = false,
                                 RTFSprms* pDirect = nullptr) const;
    /// Inserts default values to override attributes of pAbstract.
    void duplicateList(const RTFValue::Pointer_t& pAbstract);
    /// Removes duplicated values based on in-list properties.
    void deduplicateList(const std::map<int, int>& rInvalidListLevelFirstIndents);
    std::size_t size() const { return m_pSprms->size(); }
    bool empty() const { return m_pSprms->empty(); }
    Entry_t& back() { return m_pSprms->back(); }
    Iterator_t begin() { return m_pSprms->begin(); }
    Iterator_t end() { return m_pSprms->end(); }
    void clear();
    bool equals(const RTFSprms& rOther) const;

private:
    void ensureCopyBeforeWrite();
    tools::SvRef<RTFSprmsImpl> m_pSprms;
};

/// RTF keyword with a parameter
class RTFSprm : public Sprm
{
public:
    RTFSprm(Id nKeyword, RTFValue::Pointer_t& pValue);
    sal_uInt32 getId() const override;
    virtual const RTFValue* getValue() const override { return m_pValue.get(); }
    writerfilter::Reference<Properties>::Pointer_t getProps() override;
#ifdef DBG_UTIL
    std::string getName() const override;
    std::string toString() const override;
#endif
private:
    Id m_nKeyword;
    RTFValue::Pointer_t& m_pValue;
};
} // namespace writerfilter::rtftok

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