/**
 * 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.preference.chat;

import javax.swing.*;

import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.preference.Preference;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

/**
 * Handles the preferences for Chatting. This handles preferences used in chatting such as the nickname
 * to be used and showing dates and times of chat posts.
 */
public class ChatPreference implements Preference {
    private ChatPreferencePanel panel;
    private String errorMessage = "Error";

    /**
     * Define the Namespace used for this preference.
     */
    public static final String NAMESPACE = "http://www.jivesoftware.org/spark/chatwindow";

    @Override
	public String getTitle() {
        return Res.getString("title.general.chat.settings");
    }

    @Override
	public String getListName() {
        return Res.getString("title.chat");
    }

    @Override
	public String getTooltip() {
        return Res.getString("title.general.chat.settings");
    }

    @Override
	public Icon getIcon() {
        return SparkRes.getImageIcon(SparkRes.Icon.USER1_MESSAGE_24x24);
    }

    @Override
	public void load() {
    }

    @Override
	public void commit() {
        LocalPreferences pref = SettingsManager.getLocalPreferences();
        pref.setTimeDisplayedInChat(panel.getShowTime());
        if( panel.getShowTime() )
        {
      	  pref.setTimeFormat(panel.getFormatTime());
        }
        pref.setChatRoomNotifications(panel.isGroupChatNotificationsOn());
        pref.setChatHistoryEnabled(!panel.isChatHistoryHidden());
        pref.setPrevChatHistoryEnabled(!panel.isPrevChatHistoryHidden());
        pref.setChatLengthDefaultTimeout(panel.getChatTimeoutTime());
        pref.setTabsOnTop(panel.isTabsOnTop());
        pref.setTabsScroll(panel.isTabScroll());
        pref.setBuzzEnabled(panel.isBuzzEnabled());
        pref.setCloseUnreadMessageEnabled(panel.isCloseUnreadMessageEnabled());
        pref.setChatHistoryAscending(panel.isSortChatHistoryAscending());
    }

    @Override
	public String getErrorMessage() {
        return errorMessage;
    }

    @Override
	public boolean isDataValid() {
        boolean dataIsValid = true;
        return dataIsValid;
    }

    @Override
	public JComponent getGUI() {
        panel = new ChatPreferencePanel();
        LocalPreferences pref = SettingsManager.getLocalPreferences();
        panel.setShowTime(pref.isTimeDisplayedInChat());
        panel.setGroupChatNotificationsOn(pref.isChatRoomNotificationsOn());
        panel.setChatHistoryHidden(!pref.isChatHistoryEnabled());
        panel.setPrevChatHistoryHidden(!pref.isPrevChatHistoryEnabled());
        panel.setChatTimeoutTime(pref.getChatLengthDefaultTimeout());
        panel.setTabsOnTop(pref.isTabOnTop());
        panel.setTabsScrollBox(pref.isTabsScroll());
        panel.setBuzzEnabled(pref.isBuzzEnabled());
        panel.setCloseUnreadMessageEnabled(pref.isCloseUnreadMessage());
        panel.setSortChatHistoryAscending(pref.isChatHistoryAscending());
        return panel;
    }

    @Override
	public String getNamespace() {
        return NAMESPACE;
    }

}
