import { WordTipsPanel } from "../View/GlobalTipPanel";
import { LayoutControl } from "../Control/LayoutControl";
import { LayOUT } from "../utils/LayOUT";
import { DispatcherManager } from "../utils/DispatcherManager";
import {GameState} from "../utils/GameState";

/*
* name;
*/
export class GlobalTip
{
    static wordList:Array<WordTipsPanel> = [];
    static showWordList:Array<WordTipsPanel> = [];
    static maxNum:number = 10;
    static isEvent:boolean = false;
    

    private static ins:GlobalTip;
    constructor(){
        GlobalTip.ins = this;
    }

    public static get Instance():GlobalTip
    {
        if( GlobalTip.ins == null )
        {
            return new GlobalTip();
        }
        return GlobalTip.ins;
    }

    public initEvent():void
    {
        GlobalTip.isEvent = true;
        DispatcherManager.addEventListener( GameState.removeselect,this,this.addwordList );
    }

    public showWordTips( word:string ):void
    {
        if( !GlobalTip.isEvent )
        {
            GlobalTip.Instance.initEvent();
        }
        if( GlobalTip.showWordList.length > 0 )
        {
            var lastword:string = GlobalTip.showWordList[GlobalTip.showWordList.length-1].nowword;
            // if( lastword == word )
            // {
            //     return;
            // }
        }
        if( GlobalTip.wordList.length > 0 )
        {
            var item:WordTipsPanel = GlobalTip.wordList.shift();
            LayoutControl.Instance.AddLayerByName( item,LayOUT.TIP );
            GlobalTip.showWordList.push(item);
            item.showWord( word );
        }
        else
        {
            if( GlobalTip.showWordList.length < 10 )
            {
                var item1:WordTipsPanel = new WordTipsPanel();
                LayoutControl.Instance.AddLayerByName( item1,LayOUT.TIP );
                GlobalTip.showWordList.push(item1);
                item1.showWord( word );
            }
            else
            {
                var item1:WordTipsPanel = GlobalTip.showWordList.shift();
                item1.showWord( word );
                GlobalTip.showWordList.push(item1);
            }
        }
    }
    public addwordList( item:WordTipsPanel ):void
    {
        if( item )
        {
            item.removeFromParent();
            var index:number = GlobalTip.showWordList.indexOf( item );
            if( index != -1 )
            {
                GlobalTip.showWordList.splice( index,1 );
            }
            LayoutControl.Instance.ClearLayerByName(LayOUT.TIP );
            GlobalTip.wordList.push( item );
        }
    }
}

