/**
 * 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.sparkimpl.plugin.bookmarks;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smackx.bookmarks.BookmarkedConference;
import org.jivesoftware.smackx.bookmarks.BookmarkedURL;
import org.jivesoftware.spark.ui.conferences.ConferenceUtils;
import org.jivesoftware.spark.util.BrowserLauncher;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.log.Log;

/**
 */
public class BookmarkItem extends JPanel {
	private final JLabel imageLabel;
    private final JLabel nameLabel;
    private final JLabel descriptionLabel;

    public Action action;

    public BookmarkItem() {
        setLayout(new GridBagLayout());

        imageLabel = new JLabel();
        nameLabel = new JLabel();
        descriptionLabel = new JLabel();


        descriptionLabel.setFont(new Font("Dialog", Font.PLAIN, 11));
        descriptionLabel.setForeground((Color)UIManager.get("ContactItemDescription.foreground"));
        descriptionLabel.setHorizontalTextPosition(JLabel.LEFT);
        descriptionLabel.setHorizontalAlignment(JLabel.LEFT);


        this.setOpaque(true);

        add(imageLabel, new GridBagConstraints(0, 0, 1, 2, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 0), 0, 0));
        add(nameLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
        add(descriptionLabel, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 2, 0), 0, 0));
    }

    public void addURL(final BookmarkedURL bookmark) {
        imageLabel.setIcon(SparkRes.getImageIcon(SparkRes.Icon.LINK_16x16));
        nameLabel.setText(bookmark.getName());
        String bookmarkURL = bookmark.getURL();
        descriptionLabel.setText(bookmarkURL);
        action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BrowserLauncher.openURL(bookmarkURL);
            }
        };
    }

    public void addConferenceRoom(final BookmarkedConference bookmark) {
        imageLabel.setIcon(SparkRes.getImageIcon(SparkRes.Icon.CONFERENCE_IMAGE_16x16));
        nameLabel.setText(bookmark.getName() != null && !bookmark.getName().isEmpty() ? bookmark.getName() : bookmark.getJid().getLocalpart().asUnescapedString());
        descriptionLabel.setText(bookmark.getJid().toString());
        action = new AbstractAction() {
            @Override
			public void actionPerformed(ActionEvent e) {
                SwingWorker worker = new SwingWorker() {
                    @Override
                    public Object construct() {
                        try {
                            Thread.sleep(10);
                        }
                        catch (InterruptedException e1) {
                            Log.error(e1);
                        }
                        return "ok";
                    }

                    @Override
                    public void finished() {
                        ConferenceUtils.joinConferenceOnSeparateThread(bookmark.getName(), bookmark.getJid(), bookmark.getNickname(), bookmark.getPassword());
                    }
                };
                worker.start();
            }
        };
    }

    public void invokeAction() {
        action.actionPerformed(null);
    }

}
