/*
 * Copyright (C) 2019 Apple Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include "ExceptionCode.h"
#include "SVGProperty.h"
#include <wtf/Optional.h>

namespace WebCore {

template<typename ItemType>
class SVGList : public SVGProperty {
public:
    unsigned length() const { return numberOfItems(); }

    unsigned numberOfItems() const
    {
        return m_items.size();
    }

    void clear(ExceptionCode& ec)
    {
        auto result = canAlterList(ec);
        if (ec)
            return;
        ASSERT(result.releaseReturnValue());

        clearItems();
        commitChange();
    }

    ItemType getItem(unsigned index, ExceptionCode& ec)
    {
        auto result = canGetItem(index, ec);
        if (ec)
            return ItemType();
        ASSERT(result.releaseReturnValue());

        return at(index);
    }

    ItemType getItem(unsigned index)
    {
    	ExceptionCode ec = 0;
        return getItem(index, ec);
    }

    ItemType initialize(const ItemType& newItem, ExceptionCode& ec)
    {
        auto result = canAlterList(ec);
        if (ec)
            return ItemType();

        // Spec: Clears all existing current items from the list.
        clearItems();

        auto item = append(newItem);
        commitChange();
        return item;
    }

    ItemType insertItemBefore(const ItemType& newItem, unsigned index, ExceptionCode& ec)
    {
        auto result = canAlterList(ec);
        if (ec)
            return ItemType();
        ASSERT(result.releaseReturnValue());

        // Spec: If the index is greater than or equal to numberOfItems,
        // then the new item is appended to the end of the list.
        if (index > numberOfItems())
            index = numberOfItems();

        auto item = insert(index, newItem);
        commitChange();
        return item;
    }

    ItemType replaceItem(const ItemType& newItem, unsigned index, ExceptionCode& ec)
    {
        auto result = canReplaceItem(index, ec);
        if (ec)
            return ItemType();
        ASSERT(result.releaseReturnValue());

        auto item = replace(index, newItem);
        commitChange();
        return item;
    }

    ItemType removeItem(unsigned index, ExceptionCode& ec)
    {
        auto result = canRemoveItem(index, ec);
        if (ec)
            return ItemType();
        ASSERT(result.releaseReturnValue());

        auto item = remove(index);
        commitChange();
        return item;
    }

    ItemType appendItem(const ItemType& newItem, ExceptionCode& ec)
    {
        auto result = canAlterList(ec);
        if (ec)
            return ItemType();
        ASSERT(result.releaseReturnValue());

        auto item = append(newItem);
        commitChange();
        return item;
    }

    void setItem(unsigned index, const ItemType& newItem, ExceptionCode& ec)
    {
        auto result = replaceItem(newItem, index, ec);
        if (ec)
            return;
        return;
    }

    // Parsers and animators need to have a direct access to the items.
    Vector<ItemType>& items() { return m_items; }
    const Vector<ItemType>& items() const { return m_items; }
    size_t size() const { return m_items.size(); }
    bool isEmpty() const { return m_items.isEmpty(); }

    void clearItems()
    {
        detachItems();
        m_items.clear();
    }

protected:
    using SVGProperty::SVGProperty;

    bool canAlterList(ExceptionCode& ec) const
    {
        if (isReadOnly()) {
        	ec = NO_MODIFICATION_ALLOWED_ERR;
            return false;
        }
        return true;
    }

    bool canGetItem(unsigned index, ExceptionCode& ec)
    {
        if (index >= m_items.size()) {
        	ec = INDEX_SIZE_ERR;
            return false;
        }
        return true;
    }

    bool canReplaceItem(unsigned index, ExceptionCode& ec)
    {
        auto result = canAlterList(ec);
        if (ec)
            return false;
        ASSERT(result.releaseReturnValue());

        if (index >= m_items.size()) {
        	ec = INDEX_SIZE_ERR;
            return false;
        }
        return true;
    }

    bool canRemoveItem(unsigned index, ExceptionCode& ec)
    {
        auto result = canAlterList(ec);
        if (ec)
            return false;
        ASSERT(result.releaseReturnValue());

        if (index >= m_items.size()) {
        	ec = INDEX_SIZE_ERR;
            return false;
        }
        return true;
    }

    virtual void detachItems() { }
    virtual ItemType at(unsigned index) const = 0;
    virtual ItemType insert(unsigned index, const ItemType&) = 0;
    virtual ItemType replace(unsigned index, const ItemType&) = 0;
    virtual ItemType remove(unsigned index) = 0;
    virtual ItemType append(const ItemType&) = 0;

    Vector<ItemType> m_items;
};

}
