var fireDragonData = {
    fireBall : null,
    columns : null,
    fireState : -1, // -1 = rien, 0= orientation tete, 1= boule de feu, 2= explosion
    launched: -1,
    isFire : false,
}

function FireBall () {
    this.sprite = null;
    this.orientation = null;
    this.cible = new Point();
    this.speed = 6;
    this.maxSpeed = 6;
    this.hit = false;
}

function prepareFireDragon (columns) {
    fireDragonData.columns = new Array();
    mLog('prepare Dragon fire');
    mLog(columns.length + ' columns received');
    for (toKill = 0; toKill<3; toKill++) {
        possible = new Array();//recherche d'archer a tuer
        for (i=0; i<columns.length; i++) {
            if (columns[i].archer != null && columns[i].destroyed == false && columns[i].chosen == false) {
                if (columns[i].archer.lvl == ArcherToKill[gameData.round-1][toKill])
                    possible.push(columns[i]);
            }
        }
        if (possible.length != 0) {
            chosen = possible[Math.floor(Math.random() * (possible.length-1))];
            fireDragonData.columns.push(chosen);
            chosen.chosen = true;
        } else {
            possibleFree = new Array();//il n'y avait pas d'archer a tuer, recherche d'un pilone a détruire
            for (i=0; i<columns.length; i++) {
                if (columns[i].archer == null && columns[i].destroyed == false && columns[i].chosen == false) {
                    possibleFree.push(columns[i]);
                }
            }
            if (possibleFree.length != 0) {
                chosen = possibleFree[Math.floor(Math.random() * (possibleFree.length-1))];
                chosen.chosen = true;
                fireDragonData.columns.push(chosen);
            }
        }
    }
    fireDragonData.launched = -1;
    fireDragonData.isFire = true;
}


function createFireBall(column) {
    start = new Point();
    start.x = myCache.dragonHead.x;
    start.y = myCache.dragonHead.y;
    fireDragonData.fireBall = new FireBall();
    fireDragonData.fireBall.sprite = game.add.sprite(start.x,start.y, 'fireBall');
    myCache.dragonHead.bringToTop();
    fireDragonData.fireBall.sprite.scale.set(0.4);
    animFireBall = fireDragonData.fireBall.sprite.animations.add('fire');
    animFireBall.play(15, true);
    fireDragonData.fireBall.sprite.anchor.setTo(0.5, 0.5);
    fireDragonData.fireBall.cible = new Point();
    fireDragonData.fireBall.cible.x = column.x + 20;
    fireDragonData.fireBall.cible.y = column.y + 10;
    //game.add.sprite(arrow.cible.x, arrow.cible.y, 'debug_croix');
    oppose = fireDragonData.fireBall.cible.y - start.y;
    hypotenuse = Math.sqrt(Math.pow(fireDragonData.fireBall.cible.x - start.x,2) + Math.pow(fireDragonData.fireBall.cible.y - start.y, 2));
    fireDragonData.fireBall.sprite.rotation = Math.asin(oppose/hypotenuse);
    if (fireDragonData.fireBall.cible.x < start.x)
        fireDragonData.fireBall.sprite.rotation = Math.PI - fireDragonData.fireBall.sprite.rotation;
    fireDragonData.hit = false;
}

//return true if there left no fireBall.
function updateFireDragon() {
    
    switch (fireDragonData.fireState) {
        case -1:
            //mLog('state -1');
            fireDragonData.launched++;
            if (fireDragonData.launched == 3) {
                fireDragonData.fireState = 3;
            } else 
                fireDragonData.fireState = 0;
            break;
        case 0:
            //mLog('state 0');
            destRotation = getHeadRotation();
            if (destRotation > myCache.dragonHead.rotation)
                vec = 0.01;
            else
                vec = -0.01;
            myCache.dragonHead.rotation = myCache.dragonHead.rotation + vec;
            if (vec == 0.01 && myCache.dragonHead.rotation > destRotation)
                myCache.dragonHead.rotation = destRotation;
            if (vec == -0.01 && myCache.dragonHead.rotation < destRotation)
                myCache.dragonHead.rotation = destRotation;
            if (destRotation == myCache.dragonHead.rotation) {
                fireDragonData.fireState = fireDragonData.fireState + 1;
                createFireBall(cible);
                sound_FireBall.play();
            }
            /*game.paused = true;
            game.input.onDown.add(unpause, self);*/
            break;
        case 1:
            //mLog('state 1');
            if (!updateFireBall())
                fireDragonData.fireState = fireDragonData.fireState + 1;
            break;
        case 2:
            //mLog('state 2');
            if (updateDestroy())
                fireDragonData.fireState = -1;
            break;
        case 3: // replace head
            destRotation = 0;
            if (destRotation > myCache.dragonHead.rotation)
                vec = 0.01;
            else
                vec = -0.01;
            myCache.dragonHead.rotation = myCache.dragonHead.rotation + vec;
            if (vec == 0.01 && myCache.dragonHead.rotation > destRotation)
                myCache.dragonHead.rotation = destRotation;
            if (vec == -0.01 && myCache.dragonHead.rotation < destRotation)
                myCache.dragonHead.rotation = destRotation;
            if (destRotation == myCache.dragonHead.rotation) {
                fireDragonData.fireState = -1;;
                fireDragonData.isFire = false;
                return true;
            }
            break;
        default:
            break;
    }
    return false;
}

function getHeadRotation() {
    cible = fireDragonData.columns[fireDragonData.launched];
    /*mLog(fireDragonData.launched);
    mLog(fireDragonData.columns);
    for (i=0; i<fireDragonData.columns.length;i++)
        mLog(fireDragonData.columns[i]);
    mLog(cible);*/
    oppose = cible.y - myCache.dragonHead.y;
    hypotenuse = Math.sqrt(Math.pow(cible.x - myCache.dragonHead.x,2) + Math.pow(cible.y - myCache.dragonHead.y, 2));
    rotation = Math.asin(oppose/hypotenuse);
    if (myCache.dragonHead.x > cible.x)
        rotation = rotation - Math.PI/2;
    else 
        rotation = Math.PI/2 - rotation;
    rotation = rotation + 0.01; //correction visuelle
    return -rotation;
}

function unpause(event){
    game.paused = false;
}

function updateFireBall () {
    mustContinue = false;
    if (fireDragonData.hit == true) 
        mustContinue = true;
    fireDragonData.fireBall.sprite.x = fireDragonData.fireBall.sprite.x + fireDragonData.fireBall.speed * Math.cos(fireDragonData.fireBall.sprite.rotation);
    fireDragonData.fireBall.sprite.y = fireDragonData.fireBall.sprite.y + fireDragonData.fireBall.speed * Math.sin(fireDragonData.fireBall.sprite.rotation);
    distance = Math.sqrt(Math.pow(fireDragonData.fireBall.cible.x - fireDragonData.fireBall.sprite.x,2) + Math.pow(fireDragonData.fireBall.cible.y - fireDragonData.fireBall.sprite.y, 2));
    if (distance < 75)
        fireDragonData.fireBall.speed = fireDragonData.fireBall.maxSpeed -1;
    if (distance < 50)
        fireDragonData.fireBall.speed = fireDragonData.fireBall.maxSpeed -2;
    if (distance < 10) {
        fireDragonData.fireBall.speed = 0;
        fireDragonData.hit = true;
    }
    return !mustContinue;
}

function updateDestroy () {
    cible = fireDragonData.columns[fireDragonData.launched];
    if (cible.destroyed == false) {
        sound_Explosion.play();
        if (cible.archer != null)
            sound_cri.play();
        fireDragonData.fireBall.sprite.destroy();
        if (cible.archer != null) {
            cible.archer.sprite.destroy();
            cible.archer.alive = false;
        }
        random = Math.floor((Math.random() * 3) + 1);
        if (random == 1)
            cible.sprite.loadTexture('piloneBrule1');
        else if (random == 2)
            cible.sprite.loadTexture('piloneBrule2');
        else if (random == 3)
            cible.sprite.loadTexture('piloneBrule3');
        cible.destroyed = true;
    } 
    if (explosion == null) {
        explosion = game.add.sprite(cible.x - 20,cible.y - 20, 'explosion');
        explosion.scale.set(0.70);
        animExplosion = explosion.animations.add('boum');
        animExplosion.play(26, false);
    } else {
        if (animExplosion.frame == 12) {
            explosion.destroy();
            explosion = null;
            return true;
        }
    }
    return false;
}