var EHDI = EHDI || Object.create(null);

EHDI.components = EHDI.components || Object.create(null);

EHDI.components.Character = function(bread, fish, bonus, number) {
    EHDI.aka.Container.call(this);
    
    this.number = number;
    this.armature = EHDI.GAME.dbFactory.buildArmature("person" + this.number);
    
    this.armature.animation.gotoAndPlay("p" + this.number + "_default", -1, -1, 0);
    dragonBones.WorldClock.clock.add(this.armature);
    this.sprite = this.armature.getDisplay();
    this.addChild(this.sprite);
    
    this.fish = fish;
    this.bread = bread;
    this.currentFish = fish;
    this.currentBread = bread;
    
    this.plate = new EHDI.aka.Container();
    this.addChild(this.plate);
    
    this.need = (fish > bread) ? "bread" : "fish";
    
    this.fishArmature = EHDI.GAME.dbFactory.buildArmature("fish");
    this.fishArmature.animation.gotoAndPlay("fish" + this.currentFish, -1, -1, 1);
    dragonBones.WorldClock.clock.add(this.fishArmature);
    this.fishSprite = this.fishArmature.getDisplay();
    this.plate.addChild(this.fishSprite);
    this.fishSprite.position.y = - EHDI.GAME.sceneManager.getStageHeight() * 0.1;
    
    this.fishSprite.position.x = - this.fishSprite.width * 0.05;
    
    this.breadArmature = EHDI.GAME.dbFactory.buildArmature("bread");
    this.breadArmature.animation.gotoAndPlay("bread" + this.currentBread, -1, -1, 1);
    dragonBones.WorldClock.clock.add(this.breadArmature);
    this.breadSprite = this.breadArmature.getDisplay();
    this.plate.addChild(this.breadSprite);
    this.breadSprite.position.y = - EHDI.GAME.sceneManager.getStageHeight() * 0.1;
    this.breadSprite.position.x = - this.breadSprite.width * 0.05;
    this.breadArmature.advanceTime(this.breadArmature.animation.animationDataList[bread].duration);
    this.fishArmature.advanceTime(this.fishArmature.animation.animationDataList[fish].duration);
    var handToUse = "handm_dark";
    
    if(this.number === 4 || this.number === 5)
        handToUse = "handm_light";
    else if(this.number === 6 || this.number === 9 || this.number === 10)
        handToUse = "handf_dark";
    else if(this.number === 7 || this.number === 8)
        handToUse = "handf_light";
    this.leftHand = new EHDI.aka.Sprite(EHDI.Assets.images[handToUse]);
    this.plate.addChild(this.leftHand);
    this.leftHand.position.set(-this.breadSprite.width * 0.51, -EHDI.GAME.sceneManager.getStageHeight() * 0.15);
    
    this.rightHand = new EHDI.aka.Sprite(EHDI.Assets.images[handToUse]);
    this.plate.addChild(this.rightHand);
    this.rightHand.position.set(this.breadSprite.width * 0.41, - EHDI.GAME.sceneManager.getStageHeight() * 0.15);
    this.rightHand.scale.x = -1;
    
    if(bonus) {
        this.hourglass = new EHDI.aka.Sprite(EHDI.Assets.images["fishbread_hourglass"]);
        this.hourglass.anchor.x = 0.5;
        this.hourglass.anchor.y = 0.5;
        this.hourglass.visible = false;
        this.hourglass.position.x = this.sprite.width * 0.45;
        this.hourglass.position.y = -this.sprite.height * 0.8;
        this.addChild(this.hourglass);
        this.bonus = bonus;
        
        var time = new EHDI.aka.PixiText("5", {fontFamily:'proximanova-black', fill: 0x02FC02, fontSize: 38, stroke : 0x000000, strokeThickness: 1});
        time.anchor.set(0.5, 0.5);
        time.position.x = - this.hourglass.width * 0.1;
        this.hourglass.addChild(time);
    }
};

EHDI.components.Character.prototype = Object.create(EHDI.aka.Container.prototype);

EHDI.components.Character.prototype.processCustomer = function() {
    if(!EHDI.GAME.customerSpawner.interaction || EHDI.GAME.pauseButton.isPaused || EHDI.GAME.pauseButton.isEndGame)
        return;
    EHDI.GAME.customerSpawner.interaction = false;
    if(this.currentFish === this.currentBread) {
        this.lookAtFood(this.correct.bind(this));
    } else {
        this.lookAtFood(this.error.bind(this));
    }
};

EHDI.components.Character.prototype.fishToggle = function() {
    if(!EHDI.GAME.customerSpawner.interaction || EHDI.GAME.pauseButton.isPaused)
        return;
    if(this.need === "fish" && this.currentFish + 1 <= this.currentBread) {
        EHDI.GAME.soundManager.playSFX("give_fish");
        var glow = new EHDI.components.Glow(EHDI.GAME.gameScene.fishGlowTexture);
        EHDI.GAME.gameScene.particleLayer.addChild(glow);
        glow.position.set(EHDI.GAME.sceneManager.getStageWidth() * EHDI.NumberUtil.randomRange(0.5, 0.55), EHDI.GAME.sceneManager.getStageHeight() * 0.6);
        this.currentFish++;
        this.fishArmature.animation.gotoAndPlay("fish" + this.currentFish, -1, -1, 1);
        this.handleAnimationComplete();
    } else {
        this.error();
    }
};

EHDI.components.Character.prototype.breadToggle = function() {
    if(!EHDI.GAME.customerSpawner.interaction || EHDI.GAME.pauseButton.isPaused)
        return;
    if(this.need === "bread" && this.currentBread + 1 <= this.currentFish) {
        EHDI.GAME.soundManager.playSFX("give_bread");
        var glow = new EHDI.components.Glow(EHDI.GAME.gameScene.breadGlowTexture);
        EHDI.GAME.gameScene.particleLayer.addChild(glow);
        glow.position.set(EHDI.GAME.sceneManager.getStageWidth() * EHDI.NumberUtil.randomRange(0.4, 0.5), EHDI.GAME.sceneManager.getStageHeight() * 0.6);
        this.currentBread++;
        this.breadArmature.animation.gotoAndPlay("bread" + this.currentBread, -1, -1, 1);
        this.handleAnimationComplete();
    } else {
        this.error();
    }
};

EHDI.components.Character.prototype.lookAtFood = function(functionOnComplete) {
    if(this.reactionTimeline) {
        this.reactionTimeline.kill();
    }
    if(this.hint) {
        this.hint.visible = false;
    }
    this.armature.animation.gotoAndPlay("p" + this.number + "_down", -1, -1, 0);
    EHDI.GAME.gameScene.bowlShadow.visible = false;
    EHDI.GAME.customerSpawner.interaction = false;
    this.plate.y = 0;
    this.reactionTimeline = new TimelineMax();
    this.reactionTimeline.to(this.plate.scale, 0.25, {x : 1, y : 1, ease : Back.easeOut});
    this.reactionTimeline.add(functionOnComplete, "+=0.25");
}

EHDI.components.Character.prototype.correct = function() {
    if(this.reactionTimeline) {
        this.reactionTimeline.kill();
    };
    this.smile();
    this.reactionTimeline = new TimelineMax({onComplete : EHDI.GAME.customerSpawner.moveCustomer.bind(EHDI.GAME.customerSpawner)});
    this.reactionTimeline.to(this.sprite, 0.125, {y : - EHDI.GAME.sceneManager.getStageHeight() * 0.05});
    this.reactionTimeline.to(this.plate, 0.125, {y : - EHDI.GAME.sceneManager.getStageHeight() * 0.05}, 0.05);
    this.reactionTimeline.to(this.sprite, 0.125, {y : 0});
    this.reactionTimeline.to(this.plate, 0.125, {y : 0, ease : Power0.easeNone}, 0.175);
    if(this.bonus)
        this.reactionTimeline.to(this.hourglass, 0.15, {y : -this.sprite.height, alpha : 0});
    this.reactionTimeline.add(this.addScore.bind(this), "+=0.1");
    if(this.bonus) {
        this.hourglass.visible = true;
        EHDI.GAME.timer.addTime(5);
    }
    
};

EHDI.components.Character.prototype.smile = function() {
    this.armature.animation.gotoAndPlay("p" + this.number + "_correct", -1, -1, 0);
};

EHDI.components.Character.prototype.addScore = function() {
    EHDI.GAME.scoreManager.addScore(10);
    EHDI.GAME.soundManager.playSFX("correct");
}

EHDI.components.Character.prototype.error = function() {
    if(this.reactionTimeline) {
        this.reactionTimeline.kill();
    }
    
    if(this.hint)
        this.hint.visible = false;
    
    EHDI.GAME.soundManager.playSFX("wrong");
    this.plate.y = 0;
    this.armature.animation.gotoAndPlay("p" + this.number + "_wrong", -1, -1, 0);
    EHDI.GAME.customerSpawner.interaction = false;
    this.reactionTimeline = new TimelineMax();
    this.reactionTimeline.to(this, 0.035, {x : this.x + EHDI.GAME.sceneManager.getStageWidth() * 0.01});
    this.reactionTimeline.to(EHDI.GAME.gameScene.bowlShadow, 0.035, {x : EHDI.GAME.gameScene.bowlShadow.x + EHDI.GAME.sceneManager.getStageWidth() * 0.01}, 0);
    this.reactionTimeline.to(this, 0.07, {x : this.x - EHDI.GAME.sceneManager.getStageWidth() * 0.02});
    this.reactionTimeline.to(EHDI.GAME.gameScene.bowlShadow, 0.07, {x : EHDI.GAME.gameScene.bowlShadow.x - EHDI.GAME.sceneManager.getStageWidth() * 0.02}, 0.035);
    this.reactionTimeline.to(this, 0.035, {x : this.x});
    this.reactionTimeline.to(EHDI.GAME.gameScene.bowlShadow, 0.035, {x : EHDI.GAME.gameScene.bowlShadow.x}, 0.105);
    this.reactionTimeline.to(this, 0.035, {x : this.x + EHDI.GAME.sceneManager.getStageWidth() * 0.01});
    this.reactionTimeline.to(EHDI.GAME.gameScene.bowlShadow, 0.035, {x : EHDI.GAME.gameScene.bowlShadow.x + EHDI.GAME.sceneManager.getStageWidth() * 0.01}, 0.14);
    this.reactionTimeline.to(this, 0.07, {x : this.x - EHDI.GAME.sceneManager.getStageWidth() * 0.02});
    this.reactionTimeline.to(EHDI.GAME.gameScene.bowlShadow, 0.07, {x : EHDI.GAME.gameScene.bowlShadow.x - EHDI.GAME.sceneManager.getStageWidth() * 0.02}, 0.175);
    this.reactionTimeline.to(this, 0.035, {x : this.x});
    this.reactionTimeline.to(EHDI.GAME.gameScene.bowlShadow, 0.035, {x : EHDI.GAME.gameScene.bowlShadow.x}, 0.245);
    this.reactionTimeline.add(this.resetState.bind(this), 0.85);
    this.reactionTimeline.to(this, 0.05, {x : this.x});
    this.reactionTimeline.to(this.plate.scale, 0.25, {x : 1.1, y : 1.1, ease : Back.easeOut});
};

EHDI.components.Character.prototype.resetState = function() {
    if(this.need === "fish") {
        this.currentFish = this.fish;
        this.fishArmature.animation.gotoAndPlay("fish" + this.currentFish, -1, -1, 1);
        this.fishArmature.advanceTime(this.fishArmature.animation.animationDataList[this.fish].duration);
    } else {
        this.currentBread = this.bread;
        this.breadArmature.animation.gotoAndPlay("bread" + this.currentBread, -1, -1, 1);
        this.breadArmature.advanceTime(this.breadArmature.animation.animationDataList[this.bread].duration);
   
    }
    this.armature.animation.gotoAndPlay("p" + this.number + "_default", -1, -1, 0);
    
    if(EHDI.GAME.customerSpawner.customerCount - 2 < 3 && this.currentFish !== this.currentBread) {
        this.reactionTimeline.add(this.showHint.bind(this), "+=0.05");
    } else if(EHDI.GAME.customerSpawner.customerCount - 2 < 3 && this.currentFish === this.currentBread) {
        if(this.hint) {
            this.hint.visible = true;
        } else {
            this.hint = new EHDI.aka.Sprite(EHDI.Assets.images["glow"]);
            this.hint.anchor.set(0.5, 0.5);

            if(this.number >= 6)
                this.hint.position.x = this.sprite.width * 0.0075;
            this.hint.y = - this.sprite.height * 0.55;
            this.addChildAt(this.hint, 0);
            
        }
    }
    
    EHDI.GAME.gameScene.bowlShadow.visible = true;
    EHDI.GAME.customerSpawner.interaction = true;
};

EHDI.components.Character.prototype.handleAnimationComplete = function() {
    if(this.reactionTimeline) {
        this.reactionTimeline.kill();
    }
    
    this.reactionTimeline = new TimelineMax();
    this.reactionTimeline.to(this.plate, 0.1, {y : EHDI.GAME.sceneManager.getStageHeight() * 0.015, ease : Back.easeOut}, 0.1);
    this.reactionTimeline.to(this.plate, 0.1, {y : 0, ease : Power0.easeNone});
    if(EHDI.GAME.customerSpawner.customerCount - 2 < 3 && this.currentFish !== this.currentBread) {
        this.reactionTimeline.add(this.showHint.bind(this), "+=0.05");
    } else if(EHDI.GAME.customerSpawner.customerCount - 2 < 3 && this.currentFish === this.currentBread) {
        if(this.hint) {
            this.hint.visible = true;
        } else {
            this.hint = new EHDI.aka.Sprite(EHDI.Assets.images["glow"]);
            this.hint.anchor.set(0.5, 0.5);
            
            if(this.number >= 6)
                this.hint.position.x = this.sprite.width * 0.0075;
            this.hint.y = - this.sprite.height * 0.55;
            this.addChildAt(this.hint, 0);
            
        }
    }
};

EHDI.components.Character.prototype.pause = function() {
    if(this.reactionTimeline)
        this.reactionTimeline.pause();
};

EHDI.components.Character.prototype.resume = function() {
    if(this.reactionTimeline)
        this.reactionTimeline.play();
}

EHDI.components.Character.prototype.mousedown = function() {
    this.isPressed = true;
    
    if(!EHDI.GAME.customerSpawner.interaction || EHDI.GAME.pauseButton.isPaused || EHDI.GAME.pauseButton.isEndGame)
        return;
    this.armature.animation.gotoAndPlay("p" + this.number + "_down", -1, -1, 0);
};

EHDI.components.Character.prototype.touchstart = function () {
    this.isPressed = true;
    
    if(!EHDI.GAME.customerSpawner.interaction || EHDI.GAME.pauseButton.isPaused || EHDI.GAME.pauseButton.isEndGame)
        return;
    this.armature.animation.gotoAndPlay("p" + this.number + "_down", -1, -1, 0);
};

EHDI.components.Character.prototype.mouseup = function() {
    
    if(!this.isPressed)
        return;
    this.isPressed = false;
    this.armature.animation.gotoAndPlay("p" + this.number + "_default", -1, -1, 0);
    this.processCustomer();    
}

EHDI.components.Character.prototype.touchend = function() {
    if(!this.isPressed)
        return;
    this.isPressed = false;
    this.armature.animation.gotoAndPlay("p" + this.number + "_default", -1, -1, 0);
    this.processCustomer();    
}

EHDI.components.Character.prototype.touchendoutside = function() {
    if(!this.isPressed)
        return;
    this.isPressed = false;
    
    this.armature.animation.gotoAndPlay("p" + this.number + "_default", -1, -1, 0);
}

EHDI.components.Character.prototype.mouseupoutside = function() {
    if(!this.isPressed)
        return;
    this.isPressed = false;
    this.armature.animation.gotoAndPlay("p" + this.number + "_default", -1, -1, 0);
}

EHDI.components.Character.prototype.showHint = function() {
    if(this.hint)
        this.hint.visible = false;
    if(this.need === "fish") 
        EHDI.GAME.gameScene.fishToggle.glow.scale.set(1, 1);
    else 
        EHDI.GAME.gameScene.breadToggle.glow.scale.set(1, 1);
}