/*
    # 在函式 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 ;
    }
    private string FindMostCloestWeaponComponent( UnitData _unitData ,
												  UnitWeaponSystem _weaponSys ,
												  NamedObject _possibleUnit ,
												  List<string> _possibleWeaponList ,													
												  ref Dictionary<string , SelectInformation> _fireList )
    {
        string ret = "" ;

        Dictionary<string,float> angleMap = new Dictionary<string, float>() ;
        foreach( string weaponComponentName in _possibleWeaponList )
        {
            // Debug.Log( "weaponComponentName=" + weaponComponentName ) ;
            if( false == _weaponSys.m_WeaponDataMap.ContainsKey( weaponComponentName ) )
            {
                continue ;
            }

            // no trackor beam
            if( -1 != weaponComponentName.IndexOf( ConstName.UnitDataComponentWeaponTrackorBeamPrefix ) )
            {
                continue ;
            }
            if( true == _fireList.ContainsKey( weaponComponentName ) )
            {
                // 如果已經用掉了就跳過不作
                continue ;
            }

            if( 0 !=
                ( _weaponSys.FindAbleWeaponComponent( weaponComponentName , _possibleUnit.Obj ) ).Length
                )
            {

                WeaponDataSet weapenDataSet = _weaponSys.m_WeaponDataMap[ weaponComponentName ] ;
                Vector3 toTargetVec = _possibleUnit.Obj.transform.position - this.gameObject.transform.position ;
                Vector3 toComponentVec = weapenDataSet.Component3DObject.transform.position - this.gameObject.transform.position ;
                float angle = Vector3.Angle( toComponentVec , toTargetVec ) ;
                angleMap[ weaponComponentName ] = angle ;
                // Debug.Log( "toComponentVec=" + toComponentVec ) ;
                // Debug.Log( "toTargetVec=" + toTargetVec ) ;
                // Debug.Log( "angleMap[ weaponComponentName ]=" + angle ) ;
            }
        }

        // 尋找angle值最小的
        float angleMin = 361.0f ;
        Dictionary<string,float>.Enumerator e = angleMap.GetEnumerator() ;
        while( e.MoveNext() )
        {
            if( e.Current.Value < angleMin )
            {
                angleMin = e.Current.Value ;
                ret = e.Current.Key ;
            }
        }
        // Debug.Log( "FindMostCloestWeaponComponent() ret=" + ret ) ;
        return ret ;
    }
    // 發射武器
    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 virtual bool CheckWeaponAndMoveToTarget( UnitData _unitData , 
													   UnitWeaponSystem _weaponSys ,
													   Vector3 _TargetPosition )
    {
        string weaponComponent = "" ;
        Vector3 vecToTarget = Vector3.zero ;
        Vector3 norVecToTarget = Vector3.zero ;
        float angleOfTarget = 0.0f ;
        float dotOfUp = 0.0f ;
        string IMPLUSE_ENGINE_RATIO = ConstName.UnitDataComponentImpulseEngineRatio ;
        // Debug.Log( "CheckWeaponAndMoveToTarget()" ) ;

        // check fire phaser weapon is available
        weaponComponent = _weaponSys.FindAbleWeaponComponent( RandomAWeaponKeyword() ,
                                                              m_TargetNow.Name ) ;
        // Debug.Log( "CheckWeaponAndMoveToTarget() weaponComponent=" + weaponComponent ) ;
        if( 0 != weaponComponent.Length )
        {
            this.SetState( AI_Fire_State.StopAndFireToTarget ) ;
            return true ;
        }

        // move to target
        if( true == MathmaticFunc.FindTargetRelation( this.gameObject ,
                                                     _TargetPosition ,
                                                     ref vecToTarget ,
                                                     ref norVecToTarget ,
                                                     ref angleOfTarget ,
                                                     ref dotOfUp ) )
        {
            if( true == _unitData.standardParameters.ContainsKey( IMPLUSE_ENGINE_RATIO ) )
            {
                // speed to maximum
                float sqrtMaximumWeaponrange = m_MaximumWeaponRange * m_MaximumWeaponRange * 0.81f ;
                if( vecToTarget.sqrMagnitude > sqrtMaximumWeaponrange )
                    _unitData.standardParameters[ IMPLUSE_ENGINE_RATIO ].ToMax() ;
                else
                    _unitData.standardParameters[ IMPLUSE_ENGINE_RATIO ].Clear() ;
            }

            // angular speed
            _unitData.AngularRatioHeadTo( angleOfTarget ,
                                          dotOfUp ,
                                          0.1f ) ;
        }

        return false ;
    }
    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 _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 ) ;
        }
    }