/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * 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 org.wandora.application.tools.mediawikiapi;

import java.awt.event.KeyEvent;

import javax.swing.JDialog;

import org.wandora.application.Wandora;
import org.wandora.application.WandoraTool;
import org.wandora.application.gui.simple.SimpleButton;
import org.wandora.application.gui.simple.SimpleField;
import org.wandora.application.gui.simple.SimpleLabel;

/**
 *
 * @author Eero
 */


class MediaWikiAPIConfigUI extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;

	private JDialog dialog = null;
    private boolean accepted;
    
    
    /**
     * Creates new form MediaWikiAPIConfigUI
     */
    public MediaWikiAPIConfigUI() {
        initComponents();
    }
    
    protected void open(Wandora wandora, WandoraTool t) {
        dialog = new JDialog(wandora, true);
        dialog.add(this);
        dialog.setTitle("Mediawiki API uploader options");
        dialog.setSize(500, 220);
        accepted = false;
        if(wandora != null) wandora.centerWindow(dialog);
        dialog.setVisible(true);
    }
    
    protected MediaWikiAPIConfig getConfig() throws Exception{
        MediaWikiAPIConfig conf = new MediaWikiAPIConfig();
        
        String url = wikiUrlTextField.getText();
        if(url.length() == 0) throw new Exception("Missing URL");
        String uname = usernameTextField.getText();
        if(url.length() == 0) throw new Exception("Missing username");
        String password = passwordTextField.getText();
        if(url.length() == 0) throw new Exception("Missing password");
        
        conf.setURL(url);
        conf.setUName(uname);
        conf.setPassword(password);
        
        
        return conf;
        
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        wikiUrlLabel = new SimpleLabel();
        wikiUrlTextField = new SimpleField();
        usernameLabel = new SimpleLabel();
        usernameTextField = new SimpleField();
        passwordLabel = new SimpleLabel();
        passwordTextField = new SimpleField();
        buttonPanel = new javax.swing.JPanel();
        FillerjPanel = new javax.swing.JPanel();
        okButton = new SimpleButton();
        cancelButton = new SimpleButton();
        description = new SimpleLabel();

        setLayout(new java.awt.GridBagLayout());

        wikiUrlLabel.setText("Mediawiki URL");
        wikiUrlLabel.setToolTipText("Mediawiki URL is a base address of the Mediawiki installation where resource will be uploaded.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 2, 4);
        add(wikiUrlLabel, gridBagConstraints);

        wikiUrlTextField.setMaximumSize(new java.awt.Dimension(290, 20000));
        wikiUrlTextField.setMinimumSize(new java.awt.Dimension(290, 20));
        wikiUrlTextField.setPreferredSize(new java.awt.Dimension(290, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 8);
        add(wikiUrlTextField, gridBagConstraints);

        usernameLabel.setText("Username");
        usernameLabel.setToolTipText("Username is used to login the Mediawiki. Uploading resource to the Mediawiki requires a valid username.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 4);
        add(usernameLabel, gridBagConstraints);

        usernameTextField.setMaximumSize(new java.awt.Dimension(90, 20));
        usernameTextField.setMinimumSize(new java.awt.Dimension(90, 20));
        usernameTextField.setPreferredSize(new java.awt.Dimension(90, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 8);
        add(usernameTextField, gridBagConstraints);

        passwordLabel.setText("Password");
        passwordLabel.setToolTipText("Password is used to login the Mediawiki.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 4);
        add(passwordLabel, gridBagConstraints);

        passwordTextField.setMaximumSize(new java.awt.Dimension(90, 20));
        passwordTextField.setMinimumSize(new java.awt.Dimension(90, 20));
        passwordTextField.setPreferredSize(new java.awt.Dimension(90, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 8);
        add(passwordTextField, gridBagConstraints);

        buttonPanel.setMaximumSize(new java.awt.Dimension(116, 25));
        buttonPanel.setMinimumSize(new java.awt.Dimension(116, 25));
        buttonPanel.setPreferredSize(new java.awt.Dimension(116, 25));
        buttonPanel.setLayout(new java.awt.GridBagLayout());

        FillerjPanel.setMaximumSize(new java.awt.Dimension(0, 0));

        javax.swing.GroupLayout FillerjPanelLayout = new javax.swing.GroupLayout(FillerjPanel);
        FillerjPanel.setLayout(FillerjPanelLayout);
        FillerjPanelLayout.setHorizontalGroup(
            FillerjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        FillerjPanelLayout.setVerticalGroup(
            FillerjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        buttonPanel.add(FillerjPanel, gridBagConstraints);

        okButton.setText("OK");
        okButton.setMaximumSize(new java.awt.Dimension(70, 23));
        okButton.setMinimumSize(new java.awt.Dimension(70, 23));
        okButton.setPreferredSize(new java.awt.Dimension(70, 23));
        okButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                okButtonMouseReleased(evt);
            }
        });
        okButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                okButtonKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        buttonPanel.add(okButton, gridBagConstraints);

        cancelButton.setText("Cancel");
        cancelButton.setMaximumSize(new java.awt.Dimension(70, 23));
        cancelButton.setMinimumSize(new java.awt.Dimension(70, 23));
        cancelButton.setPreferredSize(new java.awt.Dimension(70, 23));
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cancelButtonMouseReleased(evt);
            }
        });
        buttonPanel.add(cancelButton, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 8);
        add(buttonPanel, gridBagConstraints);

        description.setText("<html><head></head><body>The details for the targeted MediaWiki instance. The URL should be the one where index.php and api.php are reached. (http://en.wikipedia.org/w/ for the English Wikipedia) The user should have write access to the wiki.</body>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
        add(description, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okButtonMouseReleased
        accepted = true;
        if(dialog != null) dialog.setVisible(false);
    }//GEN-LAST:event_okButtonMouseReleased

    private void okButtonKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_okButtonKeyReleased
        accepted = true;
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if(dialog != null) {
                dialog.setVisible(false);
            }
        }
    }//GEN-LAST:event_okButtonKeyReleased

    private void cancelButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseReleased
        accepted = false;
        if(dialog != null) dialog.setVisible(false);
    }//GEN-LAST:event_cancelButtonMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel FillerjPanel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel description;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField passwordTextField;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameTextField;
    private javax.swing.JLabel wikiUrlLabel;
    private javax.swing.JTextField wikiUrlTextField;
    // End of variables declaration//GEN-END:variables
}
