import { _decorator, Component, Node, AnimationComponent } from 'cc';
import { SoundManager } from '../../../../engines/sounds/SoundManager';
import { GameController } from '../GameController';
import { WeaponBase } from './WeaponBase';
const { ccclass, property } = _decorator;

export class SingleShotWeapon extends WeaponBase {

    constructor(leftHand:AnimationComponent,rightHand:AnimationComponent){
        super(leftHand,rightHand);
    }

    Fire(bullet:boolean):void{
        super.Fire(bullet);
        if(this.fireKey==1){
            this.FireLeft();
        }else if(this.fireKey==3){
            this.FireRight();
        }else{
            this.isLeftFire=!this.isLeftFire;
            if(this.isLeftFire){
                this.FireLeft();
            }else{
                this.FireRight();
            }
        }
        if(bullet){
            this.bulletCount--;
        }
    }

    private FireLeft():void{
        GameController.single.WeaponAttack();
        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);

        SoundManager.single.PlaySound("WeaponLeft",this.weaponConfig.fireSound,false,true);
    }

    private FireRight():void{
        GameController.single.WeaponAttack();
        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);
        SoundManager.single.PlaySound("WeaponRight",this.weaponConfig.fireSound,false,true);
    }
    
    private FireCompleteHandler():void{
        this.isFire=false;
        if(this.isLeftFire){
            this.leftHand.crossFade("Idle",0.5);
        }else{
            this.rightHand.crossFade("Idle",0.5)
        }
    }
}