package org.wandledi.spells;

import org.wandledi.AbstractSpell;
import org.wandledi.Spell;

/**A complex spell combines the effects of several other spells.
 *
 * For instance 'new ComplexSpell(new AttributeTransformation(...), new Duplication(n))'
 * will first perform an attribute transformation on the target element and then
 * the transformed element will be duplicated with the new attributes.
 *
 * Note that it is undefined how following spells will behave if you
 * change more than the contents of the target element first, which is the case
 * with Replacements, Inclusions and Duplications.
 *
 * That is if you have a Duplication first and then an Insertion, "the Insertion does not know"
 * where to apply. Therefore in such cases if you want the Insertion to happen multiple times,
 * first insert and duplicate last.
 *
 */
public class ComplexSpell extends AbstractSpell {

    Spell[] spells;

    /**Creates a complex spell with the combined effects of the given spells.
     * The effects apply in order of the given array.
     *
     * @param spells
     */
    public ComplexSpell(Spell... spells) {

        this.spells = spells;
        weave(spells);
    }

    @Override
    public void setParent(Spell parent) {

        spells[spells.length - 1].setParent(parent);
    }

    /**Weaves this spell together with the given spells,
     * so that the effects of all the spells are combined.
     *
     * @param spells Spells to weave in.
     * @return This spell (no new spell).
     */
    protected void weave(Spell... spells) {

        super.setParent(spells[0]);
        for (int i = 0; i < spells.length - 1; ++i) {
            spells[i].setParent(spells[i + 1]);
        }
    }

    @Override
    public Spell clone() {

        Spell[] copies = new Spell[spells.length];
        for (int i = 0; i < spells.length; ++i) {
            copies[i] = spells[i].clone();
        }
        return new ComplexSpell(copies);
    }
}

@@ -886,24 +886,34 @@
 pell %7B%0A%0A    
+protected 
 Spell%5B%5D spel
@@ -916,16 +916,16 @@
 spells;%0A
-
 %0A    /**
@@ -1120,33 +1120,32 @@
 ll... spells) %7B%0A
-%0A
         this.spe
@@ -1180,32 +1180,71 @@
 spells);%0A    %7D%0A%0A
+    protected ComplexSpell() %7B%0A%0A    %7D%0A%0A
     @Override%0A  
@@ -1279,25 +1279,24 @@
 l parent) %7B%0A
-%0A
         spel
@@ -1597,25 +1597,24 @@
 . spells) %7B%0A
-%0A
         supe
@@ -1762,32 +1762,32 @@
 %0A%0A    @Override%0A
+
     public Spell
@@ -1793,25 +1793,24 @@
 l clone() %7B%0A
-%0A
         Spel

make it easier to extend ComplextSpell