/*
 * File    : ConfigPanel*.java
 * Created : 11 mar. 2004
 * By      : TuxPaper
 * 
 * Copyright (C) 2004, 2005, 2006 Aelitis SAS, All rights Reserved
 *
 * 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 2 of the License.
 *
 * 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 ( see the LICENSE file ).
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * AELITIS, SAS au capital de 46,603.30 euros,
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 */

package org.gudy.azureus2.ui.swt.views.configsections;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.ipfilter.*;
import org.gudy.azureus2.core3.ipfilter.impl.IpFilterAutoLoaderImpl;
import org.gudy.azureus2.core3.logging.LogAlert;
import org.gudy.azureus2.core3.logging.Logger;
import org.gudy.azureus2.core3.util.AERunnable;
import org.gudy.azureus2.core3.util.DisplayFormatters;
import org.gudy.azureus2.ui.swt.Messages;
import org.gudy.azureus2.ui.swt.Utils;
import org.gudy.azureus2.ui.swt.config.*;
import org.gudy.azureus2.ui.swt.plugins.UISWTConfigSection;

import com.aelitis.azureus.core.AzureusCore;
import com.aelitis.azureus.core.AzureusCoreFactory;
import com.aelitis.azureus.ui.swt.imageloader.ImageLoader;

import org.gudy.azureus2.plugins.ui.config.ConfigSection;

public class ConfigSectionIPFilter implements UISWTConfigSection {
  IpFilter filter;
  Table table;
  boolean noChange;
  
  FilterComparator comparator;
  
  private boolean bIsCachingDescriptions = false;
  
  class FilterComparator implements Comparator {
    
    boolean ascending = true;
    
    static final int FIELD_NAME = 0;
    static final int FIELD_START_IP = 1;
    static final int FIELD_END_IP = 2;
    
    int field = FIELD_START_IP;
   
    
    public int compare(Object arg0,Object arg1) {
      IpRange range0 = (IpRange) arg0;
      IpRange range1 = (IpRange) arg1;
      if(field == FIELD_NAME) {
        return (ascending ? 1 : -1) * ( range0.compareDescription( range1 ));
      }
      if(field == FIELD_START_IP) {
        return (ascending ? 1 : -1) * ( range0.compareStartIpTo( range1 ));
      }
      if(field == FIELD_END_IP) {
        return (ascending ? 1 : -1) * ( range0.compareEndIpTo( range1 ));
      }
      return 0;
    }
    
    public void setField(int newField) {      
      if(field == newField) ascending = ! ascending;
      field = newField;
    }
    
    
  }
  
  IpRange 	ipRanges[];
  Label		percentage_blocked;

	private IPFilterListener filterListener;
  
  public
  ConfigSectionIPFilter()
  {
  	comparator = new FilterComparator();
  }
  
  public String configSectionGetParentSection() {
    return ConfigSection.SECTION_ROOT;
  }

	public String configSectionGetName() {
		return "ipfilter";
	}

  public void configSectionSave() {
    try{
      if (filter != null)
      	filter.save();
    }catch( Exception e ){
    	Logger.log(new LogAlert(LogAlert.UNREPEATABLE,
					"Save of filter file fails", e));
    }
  }
  
	public int maxUserMode() {
		return 1;
	}


  public void configSectionDelete() {
  	if (bIsCachingDescriptions) {
	    IpFilterManager ipFilterManager = AzureusCoreFactory.getSingleton().getIpFilterManager();
	  	ipFilterManager.clearDescriptionCache();
	  	bIsCachingDescriptions = false;
  	}
  	
  	if (filter != null) {
  		filter.removeListener(filterListener);
  	}
		ImageLoader imageLoader = ImageLoader.getInstance();
		imageLoader.releaseImage("openFolderButton");
		imageLoader.releaseImage("subitem");
  }

  public Composite configSectionCreate(final Composite parent) {

  	if (!AzureusCoreFactory.isCoreRunning()) {
      Composite cSection = new Composite(parent, SWT.NULL);
    	cSection.setLayout(new FillLayout());
    	Label lblNotAvail = new Label(cSection, SWT.WRAP);
    	Messages.setLanguageText(lblNotAvail, "core.not.available");
    	return cSection;
    }

		ImageLoader imageLoader = ImageLoader.getInstance();
		Image imgOpenFolder = imageLoader.getImage("openFolderButton");			

		String sCurConfigID;

    GridData gridData;

    int userMode = COConfigurationManager.getIntParameter("User Mode");

    final IpFilterManager ipFilterManager = AzureusCoreFactory.getSingleton().getIpFilterManager();
    filter = ipFilterManager.getIPFilter();

    Composite gFilter = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
    gFilter.setLayout(layout);
    gridData = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
    gFilter.setLayoutData(gridData);
    
    
    // start controls

    	// row: enable filter + allow/deny
    
	gridData = new GridData();

    BooleanParameter enabled = new BooleanParameter(gFilter, "Ip Filter Enabled");
	enabled.setLayoutData( gridData ); 
    Messages.setLanguageText(enabled.getControl(), "ConfigView.section.ipfilter.enable");

	gridData = new GridData();

    BooleanParameter deny = new BooleanParameter(gFilter, "Ip Filter Allow");
	deny.setLayoutData( gridData ); 
    Messages.setLanguageText(deny.getControl(), "ConfigView.section.ipfilter.allow");
  
    deny.addChangeListener(
    	new ParameterChangeAdapter()
		{
    		public void
    		parameterChanged(
    			Parameter	p,
    			boolean		caused_internally )
			{
    			setPercentageBlocked();
			}
		});
    
    	// row persist banning
    
	gridData = new GridData();

  BooleanParameter persist_bad_data_banning = new BooleanParameter(gFilter, "Ip Filter Banning Persistent");
  persist_bad_data_banning.setLayoutData( gridData );
  Messages.setLanguageText(persist_bad_data_banning.getControl(), "ConfigView.section.ipfilter.persistblocking");

    Group gBlockBanning = new Group(gFilter, SWT.NULL);
    Messages.setLanguageText(gBlockBanning, "ConfigView.section.ipfilter.peerblocking.group");
    layout = new GridLayout();
    layout.numColumns = 2;
    gBlockBanning.setLayout(layout);


  	// row block bad + group ban

		BooleanParameter enable_bad_data_banning = new BooleanParameter(
				gBlockBanning, "Ip Filter Enable Banning",
				"ConfigView.section.ipfilter.enablebanning");
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		enable_bad_data_banning.setLayoutData(gridData);

    Label discard_label = new Label(gBlockBanning, SWT.NULL);
    Messages.setLanguageText(discard_label,
				"ConfigView.section.ipfilter.discardbanning");

    FloatParameter discard_ratio = new FloatParameter(gBlockBanning, "Ip Filter Ban Discard Ratio");
    gridData = new GridData();
    discard_ratio.setLayoutData(gridData);


    Composite cIndent = new Composite(gBlockBanning, SWT.NONE);
    gridData = new GridData(GridData.FILL_BOTH);
    gridData.horizontalSpan = 2;
    gridData.horizontalIndent = 15;
    cIndent.setLayoutData(gridData);
    layout = new GridLayout(3, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    cIndent.setLayout(layout);
    
		Image img = imageLoader.getImage("subitem");
		Label label = new Label(cIndent, SWT.NULL);
		gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
		label.setLayoutData(gridData);
		label.setImage(img);

    
		Label discard_min_label = new Label(cIndent, SWT.NULL);
    Messages.setLanguageText(discard_min_label,
    "ConfigView.section.ipfilter.discardminkb", new String[]{ DisplayFormatters.getUnit( DisplayFormatters.UNIT_KB)});

    IntParameter discard_min = new IntParameter(cIndent, "Ip Filter Ban Discard Min KB");
    gridData = new GridData();
    discard_min.setLayoutData(gridData);
    
   	// block banning

    Label block_label = new Label(gBlockBanning, SWT.NULL);
    Messages.setLanguageText(block_label,
    "ConfigView.section.ipfilter.blockbanning");

    IntParameter block_banning = new IntParameter(gBlockBanning,
    "Ip Filter Ban Block Limit", 0, 256);
    gridData = new GridData();
    block_banning.setLayoutData(gridData);

    // triggers
    
    enable_bad_data_banning.setAdditionalActionPerformer(new ChangeSelectionActionPerformer(
    		new Control[] { 
    				block_banning.getControl(), block_label, 
    				discard_ratio.getControl(), discard_label,
    				discard_min.getControl(), discard_min_label }));


    Group gAutoLoad = new Group(gFilter, SWT.NONE);
    Messages.setLanguageText(gAutoLoad, "ConfigView.section.ipfilter.autoload.group");
    layout = new GridLayout();
    layout.numColumns = 4;
    gAutoLoad.setLayout(layout);
    gAutoLoad.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    
    // Load from file
    sCurConfigID = "Ip Filter Autoload File";
    //allConfigIDs.add(sCurConfigID);
    Label lblDefaultDir = new Label(gAutoLoad, SWT.NONE);
    Messages.setLanguageText(lblDefaultDir, "ConfigView.section.ipfilter.autoload.file");
    lblDefaultDir.setLayoutData(new GridData());
    
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.minimumWidth = 50;
    final StringParameter pathParameter = new StringParameter(gAutoLoad, sCurConfigID);
    pathParameter.setLayoutData(gridData);

    Button browse = new Button(gAutoLoad, SWT.PUSH);
    browse.setImage(imgOpenFolder);
    imgOpenFolder.setBackground(browse.getBackground());

    browse.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        FileDialog dialog = new FileDialog(parent.getShell(), SWT.APPLICATION_MODAL);
        dialog.setFilterPath(pathParameter.getValue());
        dialog.setText(MessageText.getString("ConfigView.section.ipfilter.autoload.file"));
        dialog.setFilterExtensions(new String[] {
					"*.dat" + File.pathSeparator + "*.p2p" + File.pathSeparator + "*.p2b"
							+ File.pathSeparator + "*.txt",
					"*.*"
				});
        dialog.setFileName("ipfilter.dat");
        String file = dialog.open();
        if (file != null) {
          pathParameter.setValue(file);
        }
      }
    });
    browse.setLayoutData(new GridData());
    
    final Button btnLoadNow = new Button(gAutoLoad, SWT.PUSH);
    Messages.setLanguageText(btnLoadNow, "ConfigView.section.ipfilter.autoload.loadnow");
    btnLoadNow.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				try {
					btnLoadNow.getShell().setCursor(btnLoadNow.getDisplay().getSystemCursor(
							SWT.CURSOR_WAIT));
					COConfigurationManager.setParameter(IpFilterAutoLoaderImpl.CFG_AUTOLOAD_LAST, 0);
					filter.reload();
					btnLoadNow.getShell().setCursor(btnLoadNow.getDisplay().getSystemCursor(
							SWT.CURSOR_ARROW));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
    btnLoadNow.setLayoutData(new GridData());
    
    Label lblAutoLoadInfo = new Label(gAutoLoad, SWT.WRAP);
    Messages.setLanguageText(lblAutoLoadInfo, "ConfigView.section.ipfilter.autoload.info");
    lblAutoLoadInfo.setLayoutData(Utils.getWrappableLabelGridData(4, 0));

    
    
    // description scratch file
    if (userMode > 0) {
    	gridData = new GridData();
    	BooleanParameter enableDesc = new BooleanParameter(gFilter,
    	"Ip Filter Enable Description Cache");
    	enableDesc.setLayoutData(gridData);
    	Messages.setLanguageText(enableDesc.getControl(),
    	"ConfigView.section.ipfilter.enable.descriptionCache");
    }
      
 
		// table
	
    table = new Table(gFilter, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION | SWT.VIRTUAL);
    String[] headers = { "ConfigView.section.ipfilter.description", "ConfigView.section.ipfilter.start", "ConfigView.section.ipfilter.end" };
    int[] sizes = { 110, 110, 110 };
    int[] aligns = { SWT.LEFT, SWT.CENTER, SWT.CENTER };
    for (int i = 0; i < headers.length; i++) {
      TableColumn tc = new TableColumn(table, aligns[i]);
      tc.setText(headers[i]);
      tc.setWidth(sizes[i]);
      Messages.setLanguageText(tc, headers[i]); //$NON-NLS-1$
    }
    
    
    
    TableColumn[] columns = table.getColumns();
    columns[0].setData(new Integer(FilterComparator.FIELD_NAME));
    columns[1].setData(new Integer(FilterComparator.FIELD_START_IP));
    columns[2].setData(new Integer(FilterComparator.FIELD_END_IP));
    
    Listener listener = new Listener() {
      public void handleEvent(Event e) {
        TableColumn tc = (TableColumn) e.widget;
        int field = ((Integer) tc.getData()).intValue();
        comparator.setField(field);
        
        if (field == FilterComparator.FIELD_NAME && !bIsCachingDescriptions) {
        	ipFilterManager.cacheAllDescriptions();
        	bIsCachingDescriptions = true;
        }
        ipRanges = getSortedRanges(filter.getRanges());
        table.setItemCount(ipRanges.length);
        table.clearAll();
    	// bug 69398 on Windows
    	table.redraw();
      }
    };
    
    columns[0].addListener(SWT.Selection,listener);
    columns[1].addListener(SWT.Selection,listener);
    columns[2].addListener(SWT.Selection,listener);

    table.setHeaderVisible(true);

    gridData = new GridData(GridData.FILL_BOTH);
    gridData.heightHint = table.getHeaderHeight() * 3;
		gridData.widthHint = 200;
    table.setLayoutData(gridData);

    Composite cArea = new Composite(gFilter, SWT.NULL);
    layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.numColumns = 4;
    cArea.setLayout(layout);
  	gridData = new GridData(GridData.FILL_HORIZONTAL);
    cArea.setLayoutData(gridData);

    Button add = new Button(cArea, SWT.PUSH);
    gridData = new GridData(GridData.CENTER);
    gridData.widthHint = 100;
    add.setLayoutData(gridData);
    Messages.setLanguageText(add, "ConfigView.section.ipfilter.add");
    add.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event arg0) {
        addRange();
      }
    });

    Button remove = new Button(cArea, SWT.PUSH);
    gridData = new GridData(GridData.CENTER);
    gridData.widthHint = 100;
    remove.setLayoutData(gridData);
    Messages.setLanguageText(remove, "ConfigView.section.ipfilter.remove");
    remove.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event arg0) {
        TableItem[] selection = table.getSelection();
        if (selection.length == 0)
          return;
        removeRange((IpRange) selection[0].getData());
        ipRanges = getSortedRanges(filter.getRanges());
        table.setItemCount(ipRanges.length);
        table.clearAll();
        table.redraw();
      }
    });

    Button edit = new Button(cArea, SWT.PUSH);
    gridData = new GridData(GridData.CENTER);
    gridData.widthHint = 100;
    edit.setLayoutData(gridData);
    Messages.setLanguageText(edit, "ConfigView.section.ipfilter.edit");
    edit.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event arg0) {
        TableItem[] selection = table.getSelection();
        if (selection.length == 0)
          return;
        editRange((IpRange) selection[0].getData());
      }
    });

    percentage_blocked  = new Label(cArea, SWT.WRAP | SWT.RIGHT);
    percentage_blocked.setLayoutData(Utils.getWrappableLabelGridData(1, 0));
    setPercentageBlocked();
    

    
    table.addMouseListener(new MouseAdapter() {
      public void mouseDoubleClick(MouseEvent arg0) {
        TableItem[] selection = table.getSelection();
        if (selection.length == 0)
          return;
        editRange((IpRange) selection[0].getData());
      }
    });

    Control[] controls = new Control[3];
    controls[0] = add;
    controls[1] = remove;
    controls[2] = edit;
    IAdditionalActionPerformer enabler = new ChangeSelectionActionPerformer(controls);
    enabled.setAdditionalActionPerformer(enabler);

    ipRanges = getSortedRanges(filter.getRanges());

    table.addListener(SWT.SetData, new Listener() {
			public void handleEvent(Event event) {
				TableItem item = (TableItem) event.item;
				int index = table.indexOf(item);

				// seems we can get -1 here (see bug 1219314 )

				if (index < 0 || index >= ipRanges.length) {
					return;
				}
				IpRange range = ipRanges[index];
				item.setText(0, range.getDescription());
				item.setText(1, range.getStartIp());
				item.setText(2, range.getEndIp());
				item.setData(range);
			}
		});
    
    table.setItemCount(ipRanges.length);
    table.clearAll();
	// bug 69398 on Windows
	table.redraw();
    
		table.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event e) {
        resizeTable();
			}
		});

		gFilter.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event e) {
        resizeTable();
			}
		});
    
    
		filterListener = new IPFilterListener() {
			public boolean canIPBeBanned(String ip) {
				return true;
			}
		
			public void IPBanned(BannedIp ip) {
			}

			public void IPBlockedListChanged(final IpFilter filter) {
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						if (table.isDisposed()) {
					  	filter.removeListener(filterListener);
					  	return;
						}
		        ipRanges = getSortedRanges(filter.getRanges());
		        table.setItemCount(ipRanges.length);
		        table.clearAll();
		        table.redraw();
					}
				});
				
			}
			public boolean
			canIPBeBlocked(
				String	ip,
				byte[]	torrent_hash )
			{
				return true;
			}
		};
    filter.addListener(filterListener);
    
    return gFilter;
  }
  
  private void resizeTable() {
	  int iNewWidth = table.getClientArea().width - 
                    table.getColumn(1).getWidth() - 
                    table.getColumn(2).getWidth() - 20;
    if (iNewWidth > 50)
      table.getColumn(0).setWidth(iNewWidth);          
  }

 

  public void removeRange(IpRange range) {
  	filter.removeRange( range );
    //noChange = false;
    //refresh();
  }

  public void editRange(IpRange range) {
    new IpFilterEditor(AzureusCoreFactory.getSingleton(),table.getShell(), range);
    noChange = false;
    //refresh();
  }

  public void addRange() {
    new IpFilterEditor(AzureusCoreFactory.getSingleton(),table.getShell(), null);
    //noChange = false;
    //refresh();
  }

  public void refresh() {
    if (table == null || table.isDisposed() || noChange)
      return;
    noChange = true;
    TableItem[] items = table.getItems();
    for (int i = 0; i < items.length; i++) {      
      if (items[i] == null || items[i].isDisposed())
        continue;
      String tmp = items[i].getText(0);
      IpRange range = (IpRange) items[i].getData();
      
      String	desc = range.getDescription();

      if (desc != null && !desc.equals(tmp))
        items[i].setText(0, desc);

      tmp = items[i].getText(1);
      if (range.getStartIp() != null && !range.getStartIp().equals(tmp))
        items[i].setText(1, range.getStartIp());

      tmp = items[i].getText(2);
      if (range.getEndIp() != null && !range.getEndIp().equals(tmp))
        items[i].setText(2, range.getEndIp());

    }
  }
  
  protected IpRange[]
  getSortedRanges(
  		IpRange[]	ranges )
  {
  	Arrays.sort(
  		ranges,
		comparator);
  	
  	return( ranges );
	
  }
  
  protected void
  setPercentageBlocked()
  {
    long nbIPsBlocked = filter.getTotalAddressesInRange();
    
    if ( COConfigurationManager.getBooleanParameter( "Ip Filter Allow" )){
    	
    	nbIPsBlocked = 0x100000000L - nbIPsBlocked;
    }
    
    int percentIPsBlocked =  (int) (nbIPsBlocked * 1000L / (256L * 256L * 256L * 256L));
    
    String nbIps = "" + nbIPsBlocked;
    String percentIps = DisplayFormatters.formatPercentFromThousands(percentIPsBlocked);

    Messages.setLanguageText(percentage_blocked,"ConfigView.section.ipfilter.totalIPs",new String[]{nbIps,percentIps});

  }
}
