/*
 * 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/>.
 *
 * 
 * CreateAssociationTypePrompt.java
 *
 * Created on 15.6.2006, 10:27
 */

package org.wandora.application.gui;


import java.awt.GridBagConstraints;
import java.util.Collection;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.wandora.application.Wandora;
import org.wandora.application.gui.simple.SimpleButton;
import org.wandora.application.gui.simple.SimpleLabel;
import org.wandora.topicmap.Association;
import org.wandora.topicmap.SchemaBox;
import org.wandora.topicmap.Topic;
import org.wandora.topicmap.TopicMap;
import org.wandora.topicmap.TopicMapException;
import org.wandora.utils.Tuples.T2;
import org.wandora.utils.swing.GuiTools;

/**
 *
 * @author  olli
 */
public class CreateAssociationTypePrompt extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;
	
    private GetTopicButton typeButton;    
    private Wandora wandora;
    private Vector<T2<GetTopicButton,GetTopicButton>> roles;
    
    /** Creates new form CreateAssociationTypePrompt */
    public CreateAssociationTypePrompt(Wandora wandora) throws TopicMapException {
        super(wandora,true);
        this.wandora=wandora;
        initialize();        
        addPlayer();
        addPlayer();        
    }
    public CreateAssociationTypePrompt(Wandora wandora,Association a) throws TopicMapException {
        super(wandora,true);
        this.wandora=wandora;
        initialize();
        typeButton.setTopic(a.getType());
        for(Topic role : a.getRoles()){
            Topic player=a.getPlayer(role);
            Topic roleClass=null;
            if(!player.isOfType(role)){
                Collection<Topic> types=player.getTypes();
                if(!types.isEmpty()) roleClass=types.iterator().next();
            }
            addPlayer(role,roleClass);
        }
    }
    public CreateAssociationTypePrompt(Wandora wandora,Collection<Topic> roles) throws TopicMapException {
        super(wandora,true);
        this.wandora=wandora;
        initialize();
        for(Topic t : roles){
            addPlayer(t,null);
        }
    }
    
    private void initialize() throws TopicMapException {
        roles=new Vector<T2<GetTopicButton,GetTopicButton>>();
        typeButton=new GetTopicButton(wandora,this);
        initComponents();
        typeButton.updateText();
        GuiTools.centerWindow(this,wandora);        
    }
    
    /** 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new SimpleLabel();
        _typeButton = typeButton;
        addPlayerButton = new SimpleButton();
        jPanel1 = new javax.swing.JPanel();
        okButton = new SimpleButton();
        cancelButton = new SimpleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        playersPanel = new javax.swing.JPanel();

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

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabel1.setText("Association type:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jLabel1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(_typeButton, gridBagConstraints);

        addPlayerButton.setText("Add Player");
        addPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPlayerButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(addPlayerButton, gridBagConstraints);

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

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(okButton, gridBagConstraints);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(cancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jPanel1, gridBagConstraints);

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

        jScrollPane1.setViewportView(playersPanel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-429)/2, (screenSize.height-340)/2, 429, 340);
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        try{
            createAssociationType();
        }catch(TopicMapException tme){
            tme.printStackTrace();
        }
        this.setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void addPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPlayerButtonActionPerformed
        try{
            addPlayer();
        }catch(TopicMapException tme){
            tme.printStackTrace();
        }
    }//GEN-LAST:event_addPlayerButtonActionPerformed
    
    public void createAssociationType() throws TopicMapException {
        TopicMap tm=wandora.getTopicMap();
        boolean cont=true;
        if(typeButton.getTopic()==null) cont=false;
        for(T2<GetTopicButton,GetTopicButton> t : roles){
            if(t.e1.getTopic()==null) cont=false;
        }
        if(!cont){
            JOptionPane.showMessageDialog(this,"You must select topic for association type and all roles.");
            return;
        }        
        
        Topic selectedType=typeButton.getTopic();
        
        Topic associationType=tm.getTopic(SchemaBox.ASSOCIATIONTYPE_SI);
        Topic role=tm.getTopic(SchemaBox.ROLE_SI);
        Topic roleClass=tm.getTopic(SchemaBox.ROLECLASS_SI);
        Topic contentType=tm.getTopic(SchemaBox.CONTENTTYPE_SI);
        
        selectedType.addType(associationType);
        
        for(T2<GetTopicButton,GetTopicButton> t : roles){
            Association a=tm.createAssociation(role);
            a.addPlayer(selectedType,associationType);
            Topic selectedRole=t.e1.getTopic();
            selectedRole.addType(role);
            a.addPlayer(selectedRole,role);
            Topic selectedRoleClass=t.e2.getTopic();
            if(selectedRoleClass!=null){
                a=tm.createAssociation(roleClass);
                a.addPlayer(selectedRole,role);
                a.addPlayer(selectedRoleClass,roleClass);
                
                selectedRoleClass.addType(contentType);
                a=tm.createAssociation(associationType);
                a.addPlayer(selectedRoleClass,contentType);
                a.addPlayer(selectedType,associationType);
            }
            else{
                selectedRole.addType(contentType);
                a=tm.createAssociation(associationType);
                a.addPlayer(selectedRole,contentType);
                a.addPlayer(selectedType,associationType);
            }
        }
    }

    private void refreshPlayersPanel(){
        playersPanel.removeAll();
        int counter=0;
        GridBagConstraints gbc=new GridBagConstraints();
        gbc.insets=new java.awt.Insets(5,5,5,5);
        gbc.weightx=1.0;
        gbc.anchor=gbc.CENTER;
        gbc.gridx=1;
        gbc.gridy=1;
        playersPanel.add(new SimpleLabel("Role"),gbc);
        gbc.gridx=2;
        playersPanel.add(new SimpleLabel("Class"),gbc);
        for(T2<GetTopicButton,GetTopicButton> t : roles){            
            gbc.gridx=1;
            gbc.gridy=counter+2;
            gbc.fill=gbc.HORIZONTAL;
            playersPanel.add(t.e1,gbc);
            gbc.gridx=2;
            playersPanel.add(t.e2,gbc);
            SimpleButton deleteButton=new SimpleButton("Remove");
            final int fcounter=counter;
            deleteButton.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(java.awt.event.ActionEvent e){
                    roles.remove(fcounter);
                    refreshPlayersPanel();
                }
            });
            gbc.gridx=3;
            gbc.weightx=0.0;
            gbc.fill=gbc.NONE;
            playersPanel.add(deleteButton,gbc);
            counter++;
        }
        gbc.gridx=1;
        gbc.gridy=counter+2;
        gbc.weighty=1.0;
        playersPanel.add(new JPanel(),gbc);
        
        playersPanel.revalidate();
        playersPanel.repaint();
    }
    
    public void addPlayer() throws TopicMapException {
        addPlayer(null,null);
    }
    public void addPlayer(Topic role,Topic roleClass) throws TopicMapException {
        T2<GetTopicButton,GetTopicButton> t=new T2<>(new GetTopicButton(role,wandora,this),new GetTopicButton(roleClass,wandora,this,true));
        roles.add(t);
        refreshPlayersPanel();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _typeButton;
    private javax.swing.JButton addPlayerButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JPanel playersPanel;
    // End of variables declaration//GEN-END:variables
    
}
