/*
 * Copyright (C) 2010 Teleal GmbH, Switzerland
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.teleal.cling.support.contentdirectory.ui;

import org.teleal.cling.controlpoint.ActionCallback;
import org.teleal.cling.controlpoint.ControlPoint;
import org.teleal.cling.model.meta.DeviceService;
import org.teleal.cling.support.contentdirectory.descriptor.dc.Title;
import org.teleal.cling.support.contentdirectory.descriptor.didl.Container;

import javax.swing.JTree;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 */
public abstract class ContentTree extends JTree implements ContentBrowseActionCallbackCreator {

    final protected Container rootContainer;
    final protected DefaultMutableTreeNode rootNode;

    public ContentTree(ControlPoint controlPoint, DeviceService deviceService) {

        rootContainer = createRootContainer(deviceService);
        rootNode = new DefaultMutableTreeNode(rootContainer) {
            @Override
            public boolean isLeaf() {
                return false;
            }
        };

        DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
        setModel(treeModel);

        getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        addTreeWillExpandListener(createContainerTreeExpandListener(controlPoint, deviceService, treeModel));
        setCellRenderer(createContainerTreeCellRenderer());

        controlPoint.execute(createContentBrowseActionCallback(deviceService, treeModel, getRootNode()));
    }

    public Container getRootContainer() {
        return rootContainer;
    }

    public DefaultMutableTreeNode getRootNode() {
        return rootNode;
    }

    protected Container createRootContainer(DeviceService deviceService) {
        Container rootContainer = new Container();
        rootContainer.setId("0");
        rootContainer.setTitle(new Title("Content Directory on " + deviceService.getDevice().getDisplayString()));
        return rootContainer;
    }

    protected TreeWillExpandListener createContainerTreeExpandListener(ControlPoint controlPoint,
                                                                       DeviceService deviceService,
                                                                       DefaultTreeModel treeModel) {
        return new ContentTreeExpandListener(controlPoint, deviceService, treeModel, this);
    }

    protected DefaultTreeCellRenderer createContainerTreeCellRenderer() {
        return new ContentTreeCellRenderer();
    }

    public ActionCallback createContentBrowseActionCallback(DeviceService deviceService,
                                                              DefaultTreeModel treeModel,
                                                              DefaultMutableTreeNode treeNode) {

        return new ContentBrowseActionCallback(deviceService, treeModel, treeNode) {
            public void updateStatusUI(Status status, DefaultMutableTreeNode treeNode, DefaultTreeModel treeModel) {
                ContentTree.this.updateStatus(status, treeNode, treeModel);
            }
            public void failureUI(String failureMessage) {
                ContentTree.this.failure(failureMessage);
            }
        };
    }

    // Show the "Loading..." message _inside_ the tree as a special node
    public void updateStatus(ContentBrowseActionCallback.Status status, DefaultMutableTreeNode treeNode, DefaultTreeModel treeModel) {
        if (status.equals(ContentBrowseActionCallback.Status.LOADING)) {
            treeNode.removeAllChildren();
            int index = treeNode.getChildCount() <= 0 ? 0 : treeNode.getChildCount();
            treeModel.insertNodeInto(new DefaultMutableTreeNode(status.getDefaultMessage()), treeNode, index);
            treeModel.nodeStructureChanged(treeNode);
        }
    }

    public abstract void failure(String message);

}
