/*
 *
 * Paros and its related class files.
 * 
 * Paros is an HTTP/HTTPS proxy for assessing web application security.
 * Copyright (C) 2003-2004 Chinotec Technologies Company
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Clarified Artistic License
 * as published by the Free Software Foundation.
 * 
 * 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
 * Clarified Artistic License for more details.
 * 
 * You should have received a copy of the Clarified Artistic License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.parosproxy.paros.extension.history;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.parosproxy.paros.extension.AbstractDialog;
import org.parosproxy.paros.extension.Extension;
import org.parosproxy.paros.network.HttpMalformedHeaderException;
import org.parosproxy.paros.network.HttpMessage;
import org.parosproxy.paros.network.HttpSender;
import org.parosproxy.paros.view.HttpPanel;
/**
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ManualRequestEditorDialog extends AbstractDialog {

	private HttpPanel requestPanel = null;
	private JPanel panelCommand = null;
	private JButton btnSend = null;
	private JLabel jLabel = null;
	private JTabbedPane panelTab = null;
	private HttpPanel responsePanel = null;
	private Extension extension = null;
	private HttpSender httpSender = null;
	private boolean isSendEnabled = true;
	
    /**
     * @throws HeadlessException
     */
    public ManualRequestEditorDialog() throws HeadlessException {
        super();
		initialize();
        
    }

    /**
     * @param arg0
     * @param arg1
     * @throws HeadlessException
     */
    public ManualRequestEditorDialog(Frame parent, boolean modal, boolean isSendEnabled) throws HeadlessException {
        super(parent, modal);
        this.isSendEnabled = isSendEnabled;
		initialize();

    }

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
	    this.setContentPane(getPanelTab());
	    this.addWindowListener(new java.awt.event.WindowAdapter() { 
	    	public void windowClosing(java.awt.event.WindowEvent e) {
	    	    getHttpSender().shutdown();
	    	    getResponsePanel().setMessage("","", false);
	    	}
	    });
	    getRequestPanel().getPanelOption().add(getPanelCommand(), "");
	    
	}
	
	/**
	 * This method initializes requestPanel	
	 * 	
	 * @return org.parosproxy.paros.view.HttpPanel	
	 */    
	public HttpPanel getRequestPanel() {
		if (requestPanel == null) {
			requestPanel = new HttpPanel(true);
		}
		return requestPanel;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getPanelCommand() {
		if (panelCommand == null) {
			panelCommand = new JPanel();
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			jLabel = new JLabel();
			panelCommand.setLayout(new GridBagLayout());
			jLabel.setText("");
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.ipadx = 0;
			gridBagConstraints2.ipady = 0;
			gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST;
			gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.weightx = 1.0D;
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.anchor = java.awt.GridBagConstraints.NORTHEAST;
			gridBagConstraints3.insets = new java.awt.Insets(2,2,2,2);
			panelCommand.add(jLabel, gridBagConstraints2);
			panelCommand.add(getBtnSend(), gridBagConstraints3);
		}
		return panelCommand;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getBtnSend() {
		if (btnSend == null) {
			btnSend = new JButton();
			btnSend.setText("Send");
			btnSend.setEnabled(isSendEnabled);
			btnSend.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
				    try {
				        HttpMessage msg = new HttpMessage();
				        getRequestPanel().getMessage(msg, true);
				        msg.getRequestHeader().setContentLength(msg.getRequestBody().length());
				        getHttpSender().sendAndReceive(msg, false);
				        if (!msg.getResponseHeader().isEmpty()) {
				            getResponsePanel().setMessage(msg, false);
				        }
				        getPanelTab().setSelectedIndex(1);
				    } catch (NullPointerException npe) {
				        getExtention().getView().showWarningDialog("Malformed header error.");				        
				    } catch (HttpMalformedHeaderException mhe) {
				        getExtention().getView().showWarningDialog("Malformed header error.");				        
				    } catch (IOException ioe) {
				        getExtention().getView().showWarningDialog("IO error in sending request.");
				    }
				    
				}
			});
		}
		return btnSend;
	}
	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */    
	private JTabbedPane getPanelTab() {
		if (panelTab == null) {
			panelTab = new JTabbedPane();
			panelTab.addTab("Request", null, getRequestPanel(), null);
			panelTab.addTab("Response", null, getResponsePanel(), null);
		}
		return panelTab;
	}
	/**
	 * This method initializes httpPanel	
	 * 	
	 * @return org.parosproxy.paros.view.HttpPanel	
	 */    
	public HttpPanel getResponsePanel() {
		if (responsePanel == null) {
			responsePanel = new HttpPanel(false);
		}
		return responsePanel;
	}

    public void setExtension(Extension extension) {
        this.extension = extension;
    }
    
    private Extension getExtention() {
        return extension;
    }
    
    public void setVisible(boolean show) {
        if (show) {
            try {
                if (httpSender != null) {
                    httpSender.shutdown();
                    httpSender = null;
                }
            } catch (Exception e) {}
            getPanelTab().setSelectedIndex(0);
        }
        super.setVisible(show);
    }
	
    private HttpSender getHttpSender() {
        if (httpSender == null) {
            httpSender = new HttpSender(extension.getModel().getOptionsParam().getConnectionParam());
            
        }
        return httpSender;
    }
    
    public void setMessage(HttpMessage msg) {
        getPanelTab().setSelectedIndex(0);
        getRequestPanel().setMessage(msg, true);
        getResponsePanel().setMessage("", "", false);
        getBtnSend().setEnabled(true);
        
    }
    

     }
