/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.spark.component.panes;

import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.log.Log;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * CollapsiblePane provides a component which can collapse or expand its content area
 * with animation and fade in/fade out effects. It also acts as a standard container for
 * other Swing components.
 *
 * @author Derek DeMoro
 */
public class CollapsiblePane extends JPanel {
	private BaseCollapsibleTitlePane titlePane;
    private final JPanel mainPanel;

    private final CopyOnWriteArrayList<CollapsiblePaneListener> listeners = new CopyOnWriteArrayList<>();

    private boolean subPane;

    public CollapsiblePane() {
        setLayout(new BorderLayout());

        titlePane = new CollapsibleTitlePane();
        mainPanel = new JPanel();


        add(titlePane, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);

        mainPanel.setLayout(new BorderLayout());

        titlePane.addMouseListener(new MouseAdapter() {
            @Override
			public void mouseClicked(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    return;
                }
                boolean isCollapsed = titlePane.isCollapsed();
                setCollapsed(!isCollapsed);
            }
        });
    }

    /**
     * Creates a CollapsiblePane.
     * @param title the title to use.
     */
    public CollapsiblePane(String title) {
        this();
        setTitle(title);
    }

    /**
     * Set the title of the Collapsible Pane.
     * @param title the collapsible pane title.
     */
    public void setTitle(String title) {
        titlePane.setTitle(title);
    }

    public void setIcon(Icon icon) {
        titlePane.setIcon(icon);
    }

    public void setCollapsed(boolean collapsed) {
        titlePane.setCollapsed(collapsed);
        mainPanel.setVisible(!collapsed);

        if (collapsed) {
            firePaneCollapsed();
        }
        else {
            firePaneExpanded();
        }
    }

    public void setContentPane(Component comp) {
        mainPanel.add(comp);
    }

    public void addCollapsiblePaneListener(CollapsiblePaneListener listener) {
        listeners.addIfAbsent(listener);
    }

    public void removeCollapsiblePaneListener(CollapsiblePaneListener listener) {
        listeners.remove(listener);
    }

    private void firePaneExpanded()
    {
        final Iterator<CollapsiblePaneListener> iter = ModelUtil.reverseListIterator( listeners.listIterator() );
        while ( iter.hasNext() )
        {
            final CollapsiblePaneListener listener = iter.next();
            try
            {
                listener.paneExpanded();
            }
            catch ( Exception e )
            {
                Log.error( "A CollapsiblePaneListener(" + listener + ") threw an exception while processing an 'expanded' event.", e );
            }
        }
    }

    private void firePaneCollapsed()
    {
        final Iterator<CollapsiblePaneListener> iter = ModelUtil.reverseListIterator( listeners.listIterator() );
        while ( iter.hasNext() )
        {
            final CollapsiblePaneListener listener = iter.next();
            try
            {
                listener.paneCollapsed();
            }
            catch ( Exception e )
            {
                Log.error( "A CollapsiblePaneListener (" + listener + ") threw an exception while processing a 'collapsed' event.", e );
            }
        }
    }


    public BaseCollapsibleTitlePane getTitlePane() {
        return titlePane;
    }

    protected void setTitlePane(BaseCollapsibleTitlePane titlePane) {
        this.titlePane = titlePane;
    }

    public boolean isCollapsed() {
        return titlePane.isCollapsed();
    }

    public boolean isSubPane() {
        return subPane;
    }

    public void setSubPane(boolean subPane) {
        this.subPane = subPane;

        titlePane.setSubPane(subPane);
    }
}
