package usda.weru.mcrew.timeline;

import java.util.ArrayList;
import java.util.List;
import usda.weru.mcrew.JulianCalendar;
import usda.weru.mcrew.OperationObject;
import usda.weru.mcrew.RowInfo;
import usda.weru.mcrew.XMLConstants;

/**
 * This class handles the overflow of an icon when multiple operations register on
 * the same day.
 * @author jonathanhornbaker
 */
public class MultiOperationIcon extends OperationIcon implements Cloneable
{
    public static final long serialVersionUID = 8888888L;
    List<RowInfo> additional;

    /**
     * Basic constructor for an icon.  It takes in a row, which is the data that
     * lies under the image, the data object for the timeline to coordinate changes
     * and the timeline itself for refreshing purposes.
     * @param row
     * @param panelData
     * @param pan 
     */
    public MultiOperationIcon(RowInfo row, TimelinePanelData panelData, TimelinePanel pan)
    {
        super(row, panelData, pan);
    }

    public void add(RowInfo row)
    {
        if(additional == null) additional = new ArrayList<RowInfo>();
        additional.add(row);
    }
    
    /**
     * Changes the date of this operation to be the day passed in.
     * @param day 
     */
    @Override
    public void setDate(JulianCalendar day) 
    { 
        this.getRow().setDate(day); 
        for(RowInfo temp : additional)
        {
            temp.setDate(day);
        }
    }
    
    /**
     * This resets the location to be at the new pixel location fed in as x and y.
     * @param x
     * @param y 
     */
    @Override
    public void specifyLocation(int x, int y)
    { 
        origX = x;
        origY = y;
        String date = time.pixelsToDate(x).getDate();
        OperationObject opOb = (OperationObject) this.getRow().getDataObject(XMLConstants.soperation);
        String name = opOb.mOperationName;
        String toolTip = "<html>" + date + "<br>" + name;
        for(RowInfo temp : additional)
        {
            OperationObject tempOpOb = (OperationObject) temp.getDataObject(XMLConstants.soperation);
            String tempName = tempOpOb.mOperationName;
            toolTip += "<br>" + tempName;
        }
        toolTip += "</html>";
        this.setToolTipText(toolTip);
    }
    
    public RowInfo[] getAllRows()
    {
        RowInfo[] rows = new RowInfo[additional.size() + 1];
        rows[0] = this.getRow();
        for(int index = 1; index <= additional.size(); index ++) rows[index] = additional.get(index - 1);
        return rows;
    }
    
    /**
     * Provides a carbon copy of the existing rowinfo object for data manipulation.
     * @return The new copy of the rowinfo object.
     * @throws java.lang.CloneNotSupportedException
     */
    @Override
    public Object clone() throws CloneNotSupportedException
    {
        MultiOperationIcon clone = new MultiOperationIcon((RowInfo) getRow().clone(), data, time);
        clone.additional = new ArrayList<RowInfo>();
        for(RowInfo row : additional) clone.add((RowInfo) row.clone());
        return clone;
    }
}
