import { AnimationComponent } from "cc";
import { SoundManager } from "../../../../engines/sounds/SoundManager";
import { GameController } from "../GameController";
import { WeaponBase } from "./WeaponBase";


export default class AutomaticFifleWeapon extends WeaponBase
{
    constructor(leftHand:AnimationComponent,rightHand:AnimationComponent){
        super(leftHand,rightHand);
    }
    
    Fire(bullet:boolean):void{
        super.Fire(bullet);
        if(this.fireKey==1){
            this.FireLeft();
            // SoundManager.single.StopSound("WeaponRight");
            if(bullet){
                this.bulletCount--;
            }
        }else if(this.fireKey==3){
            this.FireRight();
            // SoundManager.single.StopSound("WeaponLeft");
            if(bullet){
                this.bulletCount--;
            }
        }else{
            this.FireLeft();
            if(this.bulletCount>1){
                this.FireRight();
            }
            if(bullet){
                this.bulletCount-=2;
            }
        }
    }

    StopFire():void{
        super.StopFire();
        SoundManager.single.StopSound("WeaponLeft");
        SoundManager.single.StopSound("WeaponRight");
    }

    Reload():void{
        super.Reload();
        SoundManager.single.StopSound("WeaponLeft");
        SoundManager.single.StopSound("WeaponRight");
    }

    private FireLeft():void{
        this.isLeftFire=true;
        this.leftHand.on(AnimationComponent.EventType.FINISHED,this.FireCompleteHandler,this,true);
        this.leftHand.play(this.weaponConfig.handFireAnimation);
        this.rightHand.crossFade("Idle",0.5);

        this.leftGunFire.play(this.weaponConfig.gunFireAnimation);
        GameController.single.WeaponAttack();
        SoundManager.single.PlaySound("WeaponLeft",this.weaponConfig.fireSound,true);
    }

    private FireRight():void{
        this.isLeftFire=false;
        this.rightHand.on(AnimationComponent.EventType.FINISHED,this.FireCompleteHandler,this,true);
        this.rightHand.play(this.weaponConfig.handFireAnimation);
        this.leftHand.crossFade("Idle",0.5);
        
        this.rightGunFire.play(this.weaponConfig.gunFireAnimation);
        GameController.single.WeaponAttack();
        SoundManager.single.PlaySound("WeaponRight",this.weaponConfig.fireSound,true);
    }
    
    private FireCompleteHandler():void{
        this.isFire=false;
        if(this.isLeftFire){
            this.leftHand.crossFade("Idle",0.5);
        }else{
            this.rightHand.crossFade("Idle",0.5)
        }
    }
}