import {AgencyBaseControl} from "./AgencyBaseControl";
import { ViewManager } from "../View/ViewManager";
import {LotteryPanel} from "../View/LotteryPanel";
import LotteryCFG from "../CFG/LotteryCFG";
import { GameUtils } from "../utils/GameUtils";
import { GlobalTip } from "../utils/GlobalTip";
import RoleCFG from "../CFG/RoleCFG";
import {AgencySkin} from "./AgencySkin";

export  class AgencyLottery extends AgencyBaseControl 
{
    static key:string = "AgencyLottery";
    
    constructor() { super(); }
    
    public init():void
    {

    }

    public openView():void
    {
        ViewManager.Instance.add( LotteryPanel );
    }

    /**************************** */
    private createAnimation(): void {
        var ani:Laya.Animation = new Laya.Animation();
        // ani.loadAtlas(this.AniConfPath); // 加载图集动画
        ani.interval = 30; // 设置播放间隔（单位：毫秒）
        ani.index = 1; // 当前播放索引
        ani.play(); // 播放图集动画

        // 获取动画的边界信息
        var bounds:Laya.Rectangle = ani.getGraphicBounds();
        ani.pivot(bounds.width / 2, bounds.height / 2);

        ani.pos(Laya.stage.width / 2, Laya.stage.height / 2);

        Laya.stage.addChild(ani);
    }

    private mAniPath:string;
    private mStartX:number = 400;
    private mStartY:number = 500;
    private mFactory:Laya.Templet;
    private mActionIndex:number = 0;
    private mCurrIndex:number = 0;
    private mArmature:Laya.Skeleton;
    private mCurrSkinIndex:number = 0;
    /************************* */


    public getreward( cfg:LotteryCFG ):void
    {
        if( cfg )
        {
            switch( cfg.type )
            {
                case 1:
                    this.RoleData.Money += Number(cfg.value);
                    GlobalTip.Instance.showWordTips( "恭喜获得" + cfg.value + "金币！" );
                break;
                case 2:
                    var rolecfg:RoleCFG = RoleCFG.dict[ cfg.value ];
                    ( this.getSingleTon( AgencySkin ) as AgencySkin ).setRoleState( rolecfg,1 );
                    GlobalTip.Instance.showWordTips( "恭喜获得角色" + rolecfg.rolename );
                break;
            }
        }
    }


    public GetReward():number
    {
        var index:number = 0;
        var dicts:Array<LotteryCFG> = LotteryCFG.vec;
        var maxNum:number = 0;
        var cfg:LotteryCFG;
        var i:number = 0;
        for( i=0;i<dicts.length;i++ )
        {
            cfg = dicts[i];
            maxNum += cfg.weight;
        }
        var sordvalue:number = GameUtils.randRangeint( 1,maxNum );
        var totalnum:number = 0;
        for( i = 0;i < dicts.length;i++ )
        {
            cfg = dicts[i];
            totalnum += cfg.weight;
            if( totalnum >= sordvalue )
            {
                index = i;
                break;
            }
        }
        return index;
    }


    /************************************************ */

    public startFun():void
    {
        this.mAniPath = "res/spine/spineRes1/dragon.sk";
        // this.mFactory = new Templet();
        // this.mFactory.on(Event.COMPLETE, this, this.parseComplete);
        // this.mFactory.on(Event.ERROR, this, this.onError);
        // this.mFactory.loadAni(this.mAniPath);
    }
    private onError():void
    {
        console.log("error");
    }
    private parseComplete():void {
        //创建模式为1，可以启用换装
        this.mArmature =this.mFactory.buildArmature(1);
        this.mArmature.x = this.mStartX;
        this.mArmature.y = this.mStartY;
        this.mArmature.scale(0.5, 0.5);
        Laya.stage.addChild(this.mArmature);
        // this.mArmature.on(Event.STOPPED, this, this.completeHandler);
        this.play();
    }
    
    private completeHandler():void
    {
        this.play();
    }
    
    private play():void
    {
        this.mCurrIndex++;
        if (this.mCurrIndex >= this.mArmature.getAnimNum())
        {
            this.mCurrIndex = 0;
        }
        this.mArmature.play(this.mCurrIndex,false);
    }
    /************************************** */
}