/*
    # 在函式 TryFireWeapon() 中,呼叫武器系統 依照關鍵字 取得 可以發射的武器部件
    # 在函式 TryFireWeapon() 中,呼叫武器系統 傳入 武器部件名稱 目標船艦名稱 發射武器
    # 失敗時發出警告聲
    # 失敗時顯示武器範圍
    */
    private bool TryFireWeapon( UnitWeaponSystem _WeaponSys , 
								string _WeaponKeyword ,
								SelectInformation _SelectionInfo )
    {
        // find correct weapon
        string TargetComponentObjectName = ConstName.CreateComponent3DObjectName( _SelectionInfo.TargetUnitName ,
                                                                                  _SelectionInfo.m_TargetComponentName ) ;
        string mainCause = "" ;
        string weaponComponentName = _WeaponSys.FindAbleWeaponComponent( _WeaponKeyword ,
                                                                         TargetComponentObjectName ,
                                                                         ref mainCause ) ;
        bool ret = false ;
        // Debug.Log( "ActiveTrackorBeam3() weaponComponentName=" + weaponComponentName ) ;
        if( false == ( ret = _WeaponSys.ActiveWeapon( weaponComponentName ,
                                       		  		  _SelectionInfo.GetTargetUnit() ,
                                                      _SelectionInfo.m_TargetComponentName ) ) )
        {
            // fire weapon fail : beep
            if( null != m_Audio_WeaponFailBeep )
            {
                // show weapon range
                ShowWeaponRange( _WeaponKeyword ) ;

                this.gameObject.audio.PlayOneShot( m_Audio_WeaponFailBeep ) ;

                MessageQueueManager manager = GlobalSingleton.GetMessageQueueManager() ;
                if( null != manager )
                    manager.AddMessage( mainCause ) ;
            }
        }
        return ret ;
    }
    protected override void FireWeapon( UnitWeaponSystem _weaponSys ,
							  			NamedObject _TargetUnit )
    {
        if( true == GlobalSingleton.GetGlobalSingletonObj().GetComponent<AutoPlayMachine>().m_Active )
            return ;

        string weaponComponent = "" ;
        // check phaser
        weaponComponent = _weaponSys.FindAbleWeaponComponent( RandomAWeaponKeyword() ,
                                                             _TargetUnit.Name ) ;

        if( 0 == weaponComponent.Length )
        {
            // Debug.Log( "FireWeapon() no available weapon" ) ;
            this.SetState( AI_Fire_State.MoveToTarget ) ;
            return ;
        }

        // fire
        if( true == _weaponSys.ActiveWeapon( weaponComponent ,
                                             _TargetUnit ,
                                             ConstName.UnitDataComponentUnitIntagraty ) )
        {
            this.SetState( AI_Fire_State.WaitForReload ) ;
        }
    }
    // 發射武器
    protected override void FireWeapon( UnitWeaponSystem _weaponSys ,
							   		   NamedObject _TargetObject )
    {
        string weaponComponent = "" ;
        // check phaser
        weaponComponent = _weaponSys.FindAbleWeaponComponent( RandomAWeaponKeyword() ,
                                                              _TargetObject.Name ) ;
        if( 0 == weaponComponent.Length )
        {
            // no available weapon
            this.SetState( AI_Fire_State.MoveToTarget ) ;
            return ;
        }

        // fire
        if( true == _weaponSys.ActiveWeapon( weaponComponent ,
                                             _TargetObject ,
                                             ConstName.UnitDataComponentUnitIntagraty ) )
        {
            this.SetState( AI_Fire_State.WaitForReload ) ;
        }
    }
    protected override void FireWeapon( UnitWeaponSystem _weaponSys ,
							  			NamedObject _TargetUnit )
    {
        if( true == GlobalSingleton.GetGlobalSingletonObj().GetComponent<AutoPlayMachine>().m_Active )
            return ;

        string weaponComponent = "" ;
        string specifiedWeapon = RandomAWeaponKeyword() ;
        // 距離太遠
        string weaponInRange = _weaponSys.FindAbleToShootTargetWeaponComponent( specifiedWeapon , _TargetUnit.Obj ) ;
        // Debug.Log( "weaponInRange=" + weaponInRange ) ;
        if( 0 == weaponInRange.Length )
        {
            // no available weapon
            this.SetState( AI_Fire_State.MoveToTarget ) ;
            return ;
        }

        string weaponReady = _weaponSys.FindAbleToFireWeaponComponent( specifiedWeapon ) ;
        #if DEBUG
        Debug.Log( "FireWeapon() weaponReady=" + weaponReady ) ;
        #endif
        if( 0 == weaponReady.Length )
        {
            // no available weapon
            this.SetState( AI_Fire_State.WaitForReload ) ;
            return ;
        }

        // 檢查正在reload
        weaponComponent = _weaponSys.FindAbleWeaponComponent( specifiedWeapon , _TargetUnit.Obj ) ;
        // Debug.Log( "weaponReady=" + weaponComponent ) ;
        if( 0 == weaponComponent.Length )
        {
            // no available weapon
            this.SetState( AI_Fire_State.WaitForReload ) ;
            return ;
        }

        // Debug.Log( "_weaponSys.ActiveWeapon" ) ;
        // fire
        if( true == _weaponSys.ActiveWeapon( weaponComponent ,
                                            _TargetUnit ,
                                            ConstName.UnitDataComponentUnitIntagraty ) )
        {
            this.SetState( AI_Fire_State.WaitForReload ) ;
        }
    }