package org.cocos2d.actions.tile;

import java.util.Random;

import org.cocos2d.nodes.CCNode;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccQuad3;

/** CCTurnOffTiles action.
 Turn off the files in random order
 */
public class CCTurnOffTiles extends CCTiledGrid3DAction {
	Random rand;
	int	seed;
	int tilesCount;
	int tilesOrder[];


    /** creates the action with a random seed, the grid size and the duration */
    public static CCTurnOffTiles action(int s, CGSize gridSize, float d) {
    	return new CCTurnOffTiles(s, gridSize, d);
    }

    /** initializes the action with a random seed, the grid size and the duration */
    protected CCTurnOffTiles(int s, CGSize gridSize, float d) {
        super(gridSize, d);
        seed = s;
        rand = new Random();
        tilesOrder = null;
    }

    @Override
    public CCTurnOffTiles copy() {
        return new CCTurnOffTiles(seed, gridSize, duration);
    }

    public void shuffle(int []array, int len) {
        for(int i = len - 1; i>=0; i-- ) {
            int j = (int)(rand.nextFloat() * (i+1));
            int v = array[i];
            array[i] = array[j];
            array[j] = v;
        }
    }

    public void turnOnTile(CGPoint pos) {
        setTile(pos, originalTile(pos));
    }

    public void turnOffTile(CGPoint pos) {
        ccQuad3	coords = new ccQuad3();
        setTile(pos, coords);
    }

    public void start(CCNode aTarget) {
        super.start(aTarget);

        if ( seed != -1 ) {
        	rand.setSeed(seed);
        }

        tilesCount = (int) (gridSize.width * gridSize.height);
        tilesOrder = new int[tilesCount];

        for(int i = 0; i < tilesCount; i++ )
            tilesOrder[i] = i;

        shuffle(tilesOrder, tilesCount);
    }

    @Override
    public void update(float time) {
        int i, l, t;

        l = (int)(time * (float)tilesCount);

        for( i = 0; i < tilesCount; i++ ) {
            t = tilesOrder[i];
            CGPoint tilePos = CGPoint.ccp( (int)(t / gridSize.height), (int)(t % gridSize.height) );

            if ( i < l )
                turnOffTile(tilePos);
            else
                turnOnTile(tilePos);
        }
    }

}

