/*
 * SLD Editor - The Open Source Java SLD Editor
 *
 * Copyright (C) 2016, SCISYS UK Limited
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sldeditor.tool.batchupdatefont;

import com.sldeditor.common.NodeInterface;
import com.sldeditor.common.SLDDataInterface;
import com.sldeditor.common.SLDEditorInterface;
import com.sldeditor.common.localisation.Localisation;
import com.sldeditor.datasource.attribute.DataSourceAttributeList;
import com.sldeditor.datasource.attribute.DataSourceAttributeListInterface;
import com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode;
import com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNodeTypeEnum;
import com.sldeditor.datasource.extension.filesystem.node.geoserver.GeoServerStyleHeadingNode;
import com.sldeditor.datasource.extension.filesystem.node.geoserver.GeoServerStyleNode;
import com.sldeditor.datasource.extension.filesystem.node.geoserver.GeoServerWorkspaceNode;
import com.sldeditor.tool.ToolButton;
import com.sldeditor.tool.ToolInterface;
import com.sldeditor.tool.ToolPanel;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * Tool to contain scale related tools.
 *
 * @author Robert Ward (SCISYS)
 */
public class BatchUpdateFontTool implements ToolInterface {

    /** The Constant PANEL_WIDTH. */
    private static final int PANEL_WIDTH = 60;

    /** The BatchUpdate Font button. */
    protected JButton toolButton;

    /** The scale panel. */
    private JPanel panel = null;

    /** The sld data list. */
    private List<SLDDataInterface> sldDataList = null;

    /** The attribute data. */
    @SuppressWarnings("unused")
    private DataSourceAttributeListInterface attributeData = new DataSourceAttributeList();

    /** The application. */
    private SLDEditorInterface application = null;

    /**
     * Constructor.
     *
     * @param application the application
     */
    public BatchUpdateFontTool(SLDEditorInterface application) {
        super();
        this.application = application;

        createUI();
    }

    /** Creates the ui. */
    private void createUI() {
        panel = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel.getLayout();
        flowLayout.setVgap(0);
        flowLayout.setHgap(0);
        panel.setBorder(
                BorderFactory.createTitledBorder(
                        Localisation.getString(
                                BatchUpdateFontTool.class, "BatchUpdateFontTool.title")));

        toolButton =
                new ToolButton(
                        Localisation.getString(
                                BatchUpdateFontTool.class, "BatchUpdateFontTool.title"),
                        "tool/batchupdatefont.png");
        panel.add(toolButton);
        toolButton.setEnabled(false);
        toolButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        BatchUpdateFontPanel scalePanel = new BatchUpdateFontPanel(application);

                        scalePanel.populate(sldDataList);
                        scalePanel.setVisible(true);
                    }
                });
        panel.setPreferredSize(new Dimension(PANEL_WIDTH, ToolPanel.TOOL_PANEL_HEIGHT));
    }

    /**
     * Gets the panel.
     *
     * @return the panel
     */
    @Override
    public JPanel getPanel() {
        return panel;
    }

    /**
     * Sets the selected items.
     *
     * @param nodeTypeList the node type list
     * @param sldDataList the sld data list
     */
    /*
     * (non-Javadoc)
     *
     * @see com.sldeditor.tool.ToolInterface#setSelectedItems(java.util.List, java.util.List)
     */
    @Override
    public void setSelectedItems(
            List<NodeInterface> nodeTypeList, List<SLDDataInterface> sldDataList) {
        this.sldDataList = sldDataList;

        if (toolButton != null) {
            toolButton.setEnabled(!sldDataList.isEmpty());
        }
    }

    /**
     * Gets the tool name.
     *
     * @return the tool name
     */
    /*
     * (non-Javadoc)
     *
     * @see com.sldeditor.tool.ToolInterface#getToolName()
     */
    @Override
    public String getToolName() {
        return getClass().getName();
    }

    /**
     * Check if the supplied tree node/data file is supported by tool.
     *
     * @param nodeTypeList the node type list
     * @param sldDataList the sld data list
     * @return true, if successful
     */
    /*
     * (non-Javadoc)
     *
     * @see com.sldeditor.tool.ToolInterface#supports(java.util.List, java.util.List)
     */
    @Override
    public boolean supports(
            List<Class<?>> uniqueNodeTypeList,
            List<NodeInterface> nodeTypeList,
            List<SLDDataInterface> sldDataList) {
        boolean result = false;

        if (nodeTypeList != null) {
            for (NodeInterface node : nodeTypeList) {
                if (node instanceof FileTreeNode) {
                    FileTreeNode fileTreeNode = (FileTreeNode) node;

                    if (fileTreeNode.getFileCategory() == FileTreeNodeTypeEnum.SLD) {
                        result = true;
                    }
                } else if (node instanceof GeoServerStyleNode) {
                    result = true;
                } else if (node instanceof GeoServerStyleHeadingNode) {
                    result = true;
                } else if (node instanceof GeoServerWorkspaceNode) {
                    GeoServerWorkspaceNode workspaceNode = (GeoServerWorkspaceNode) node;
                    if (workspaceNode.isStyle()) {
                        result = true;
                    }
                }
            }
        }
        return result;
    }
}
