/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2006 Phex Development Group
 *
 *  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, 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  Created on 21.12.2005
 *  --- CVS Information ---
 *  $Id: SearchFilterRulesEditModel.java 3362 2006-03-30 22:27:26Z gregork $
 */
package phex.gui.dialogs.filter;

import java.util.*;

import javax.swing.AbstractListModel;
import javax.swing.ListModel;
import javax.swing.event.EventListenerList;

import phex.query.QueryManager;
import phex.rules.Rule;
import phex.rules.SearchFilterRules;

public class SearchFilterRulesEditModel extends AbstractListModel implements ListModel
{
    protected EventListenerList listenerList = new EventListenerList();
    
    private List allRules;
    
    public SearchFilterRulesEditModel()
    {
        SearchFilterRules rules = QueryManager.getInstance().getSearchFilterRules();
        allRules = new ArrayList();
        allRules.addAll( rules.getAsList() );
    }
    
    public void applyChangesToQueryManager()
    {
        ArrayList newRulesList = new ArrayList();
        Iterator iterator = allRules.iterator();
        while( iterator.hasNext() )
        {
            Object obj = iterator.next();
            if ( obj instanceof Rule )
            {
                newRulesList.add( obj );
            }
            else
            {
                RuleEditWrapper wrapper = (RuleEditWrapper)obj;
                Rule modifiedRule = wrapper.getModifiedRule();
                if ( modifiedRule != null )
                {
                    newRulesList.add( modifiedRule );
                }
            }
        }
        QueryManager.getInstance().getSearchFilterRules().setRuleList(
            newRulesList );
    }
    
    public void addNewRule( Rule rule )
    {
        RuleEditWrapper wrapper = new RuleEditWrapper( rule );
        
        int pos = allRules.size();
        allRules.add( pos, wrapper );
        fireIntervalAdded(this, pos, pos);
    }
    
    public int moveRule( int idx, boolean moveUp )
    {
        Object rule = allRules.remove( idx );
        int newIdx = moveUp ? Math.max( 0, idx-1 ) : Math.min( idx+1, allRules.size() );
        allRules.add( newIdx, rule );
        int start = Math.min(idx, newIdx);
        int end = Math.max(idx, newIdx);
        fireContentsChanged( this, start, end );
        return newIdx;
    }
    
    public void removeRule( int idx )
    {
        RuleEditWrapper wrapper = getRuleEditWrapperAt(idx);        
        wrapper.setModifiedRule( null );
        
        allRules.remove(idx);
        fireIntervalRemoved(this, idx, idx);
    }
    
    public void updateRule( int idx, Rule rule )
    {
        RuleEditWrapper wrapper = (RuleEditWrapper) allRules.get( idx );
        wrapper.setModifiedRule( rule );
        fireContentsChanged(this, idx, idx);
    }
    
    /**
     * Returns the edit rule for the index position. In case the original rule
     * was modified this is the modified rule, otherwise the original rule is
     * returned.
     * @param idx
     * @return
     */
    public Rule getDisplayRuleAt( int idx )
    {
        RuleEditWrapper wrapper;
        Object obj = allRules.get( idx );
        if ( obj instanceof Rule )
        {
            wrapper = createRuleEditWrapper( (Rule)obj );
            allRules.remove( idx );
            allRules.add( idx, wrapper );
        }
        else 
        {
            wrapper = (RuleEditWrapper) obj;
        }

        return wrapper.getModifiedRule();
    }
    
    private RuleEditWrapper getRuleEditWrapperAt( int idx )
    {
        RuleEditWrapper wrapper;
        Object obj = allRules.get( idx );
        if ( obj instanceof Rule )
        {
            wrapper = createRuleEditWrapper( (Rule)obj );
            allRules.remove( idx );
            allRules.add( idx, wrapper );
        }
        else 
        {
            wrapper = (RuleEditWrapper) obj;
        }

        return wrapper;
    }
    
    private RuleEditWrapper createRuleEditWrapper( Rule orgRule )
    {
        Rule modifiableRule = (Rule) orgRule.clone();
        RuleEditWrapper wrapper = new RuleEditWrapper( modifiableRule );
        return wrapper;
    }
    
    public int getSize()
    {
        return allRules.size();
    }

    public Object getElementAt( int index )
    {
        return allRules.get(index);
    }
}
