/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */

package com.codename1.designer;

import com.codename1.ui.EditorFont;
import com.codename1.ui.util.EditableResources;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.JOptionPane;

/**
 * Generic dialog shown before adding a new resource to the resource editor
 *
 * @author  Shai Almog
 */
public class AddResourceDialog extends javax.swing.JPanel {
    public static final int IMAGE = 0;
    public static final int ANIMATION = 1;
    public static final int FONT = 2;
    public static final int THEME = 3;
    public static final int DATA = 4;
    public static final int LOCALIZATION = 5;
    public static final int TIMELINE = 6;
    public static final int UI = 7;
    public static final int INDEXED_IMAGE = 8;
    public static final int MULTI_IMAGE = 9;
    
    /**
     * Indicates the name selected for the property by the software,
     * if no change to this name was made then the name will keep changing
     * with combo box selections.
     */
    private String softwareSelectedName;
    private EditableResources res;
    
    public AddResourceDialog(EditableResources r, int resourceType) {
        this(r, resourceType, true);
    }
    
    public AddResourceDialog(EditableResources r, int resourceType, boolean disableType) {
        this(r);
        type.setSelectedIndex(resourceType);
        type.setEnabled(!disableType);
    }
    
    /** Creates new form AddResourceDialog */
    public AddResourceDialog(EditableResources r) {
        initComponents();
        res = r;
        softwareSelectedName = nextAvailableName();
        name.setText(softwareSelectedName);
        type.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(softwareSelectedName != null && name.getText().equals(softwareSelectedName)) {
                    softwareSelectedName = nextAvailableName();
                    name.setText(softwareSelectedName);
                } else {
                    softwareSelectedName = null;
                }
            }
        });
    }
    
    public static String nextAvailableName(EditableResources res, String type) {
        String resourceName = type + " 1";
        int counter = 2;
        while(checkName(res, resourceName)) {
            resourceName = type + " " + counter;
            counter++;
        }
        return resourceName;
    }
    
    private String nextAvailableName() {
        return nextAvailableName(res, (String)type.getSelectedItem());
    }

    public boolean checkName(EditableResources r) {
        return checkName(r, getName());
    }
    
    /**
     * Returns true if the name already exists in the resources
     */
    private static boolean checkName(EditableResources r, String name) {
        for(String n : r.getResourceNames()) {
            if(n.equals(name)) {
                return true;
            }
        }
        return false;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        type = new javax.swing.JComboBox();

        setName("Form"); // NOI18N

        jLabel1.setText("Name");
        jLabel1.setName("jLabel1"); // NOI18N

        name.setName("name"); // NOI18N

        jLabel2.setText("Type");
        jLabel2.setName("jLabel2"); // NOI18N

        type.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Image", "Animation", "Font", "Theme", "Data", "Localization (L10N)", "Timeline", "GUI", "Indexed Image", "Multi Image" }));
        type.setName("type"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(type, 0, 338, Short.MAX_VALUE)
                    .add(name, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(type, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    public String getResourceName() {
        return name.getText();
    }    

    public String addResource(EditableResources res, ResourceEditorView view) {
        // "Image", "Animation", "Font", "Theme", "Data", "Localization (L10N)"
        String newName = name.getText();
        for(String r : res.getResourceNames()) {
            if(r.equalsIgnoreCase(newName)) {
                JOptionPane.showMessageDialog(this, "A resource called: " + newName + " already exists\nYou must delete the resource first.", "Add Resource", JOptionPane.ERROR_MESSAGE);
                return null;
            }
        }

        switch(type.getSelectedIndex()) {
            case IMAGE: // image
                ImageRGBEditor imageEditor = new ImageRGBEditor(res, name.getText(), view);
                imageEditor.selectFile();
                view.setSelectedResource(name.getText());
                break;
            case MULTI_IMAGE:
                ImageMultiEditor multiImageEditor = new ImageMultiEditor(res, name.getText(), view);
                view.setSelectedResource(name.getText());
                break;
            case TIMELINE:
                new TimelineEditor(res, name.getText(), view);
                view.setSelectedResource(name.getText());
                break;
            case ANIMATION: // animation
                TimelineEditor.selectFile(view, res, name.getText());
                /*ImageEditor animationEditor = new ImageEditor(res, name.getText());
                animationEditor.setAnimation(true);
                animationEditor.selectFile(view);*/
                break;
            case FONT: // font
                new FontEditor(res, 
                        new EditorFont(com.codename1.ui.Font.createSystemFont(com.codename1.ui.Font.FACE_SYSTEM, com.codename1.ui.Font.STYLE_PLAIN, com.codename1.ui.Font.SIZE_MEDIUM),
                            null, "Arial-plain-12", true, RenderingHints.VALUE_TEXT_ANTIALIAS_ON,
                            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,;:!@/\\*()[]{}|#$%^&<>?'\"+- "), 
                        name.getText()).createFont();
                view.setSelectedResource(name.getText());
                break;
            case THEME: // theme
                res.setTheme(name.getText(), new Hashtable());
                view.setSelectedResource(name.getText());
                //ResourceEditorView.expandAndSelect(tree, name.getText());
                break;
            case DATA: // data
                DataEditor dataEditor = new DataEditor(res, name.getText());
                dataEditor.selectDataFile(view);
                view.setSelectedResource(name.getText());
                break;
            case LOCALIZATION: // localization
                Hashtable h = new Hashtable();
                Hashtable local = new Hashtable();
                local.put("cancel", "Cancel");
                local.put("ok", "OK");
                local.put("menu", "Menu");
                local.put("select", "Select");
                local.put("edit", "Edit");
                h.put("en", local);
                res.setL10N(name.getText(), h);
                view.setSelectedResource(name.getText());
                //ResourceEditorView.expandAndSelect(tree, name.getText());
                break;
            case UI:
                UserInterfaceEditor uiEditor = new UserInterfaceEditor(name.getText(), 
                        res, view.getProjectGeneratorSettings(), view);
                view.setSelectedResource(name.getText());
                break;
        } 
        
        return name.getText();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField name;
    private javax.swing.JComboBox type;
    // End of variables declaration//GEN-END:variables
    
}
