/*
 * Copyright (C) 2020 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. AND ITS CONTRIBUTORS ``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 ITS 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.
 */

WI.AnimationCollectionContentView = class AnimationCollectionContentView extends WI.CollectionContentView
{
    constructor(representedObject)
    {
        console.assert(representedObject instanceof WI.AnimationCollection);

        let contentPlaceholder = document.createElement("div");

        let descriptionElement = contentPlaceholder.appendChild(document.createElement("div"));
        descriptionElement.className = "description";

        switch (representedObject.animationType) {
        case WI.Animation.Type.WebAnimation:
            descriptionElement.textContent = WI.UIString("Waiting for animations created by JavaScript.");
            break;

        case WI.Animation.Type.CSSAnimation:
            descriptionElement.textContent = WI.UIString("Waiting for animations created by CSS.");
            break;

        case WI.Animation.Type.CSSTransition:
            descriptionElement.textContent = WI.UIString("Waiting for transitions created by CSS.");
            break;
        }
        console.assert(descriptionElement.textContent);

        super(representedObject, WI.AnimationContentView, contentPlaceholder);

        this.selectionEnabled = true;

        this.element.classList.add("animation-collection");
    }

    // Public

    handleRefreshButtonClicked()
    {
        for (let subview of this.subviews) {
            if (subview instanceof WI.AnimationContentView)
                subview.handleRefreshButtonClicked();
        }
    }

    // Protected

    contentViewAdded(contentView)
    {
        contentView.element.addEventListener("mouseenter", this._handleContentViewMouseEnter);
        contentView.element.addEventListener("mouseleave", this._handleContentViewMouseLeave);
    }

    contentViewRemoved(contentView)
    {
        contentView.element.removeEventListener("mouseenter", this._handleContentViewMouseEnter);
        contentView.element.removeEventListener("mouseleave", this._handleContentViewMouseLeave);
    }

    detached()
    {
        WI.domManager.hideDOMNodeHighlight();

        super.detached();
    }

    // Private

    _handleContentViewMouseEnter(event)
    {
        let contentView = WI.View.fromElement(event.target);
        if (!(contentView instanceof WI.AnimationContentView))
            return;

        let animation = contentView.representedObject;
        animation.requestEffectTarget((node) => {
            if (!node || !node.ownerDocument)
                return;
            node.highlight();
        });
    }

    _handleContentViewMouseLeave(event)
    {
        WI.domManager.hideDOMNodeHighlight();
    }
};
