import {PanelChildControl} from "../Control/PanelChildControl";
import CpaEvent from "./CpaEvent";
import { PlayerAccount } from "../WXSDK/PlayerAccount";
import { Recommend } from "./RecommendView";
import CpaItem from "./CpaItem";
import {TimerService} from "../Time/TimerService";

export default class BaoKuanGameView extends PanelChildControl implements TimerControl
{
    c_state:fairygui.Controller;
    item_0:CpaItem;
    item_1:CpaItem;
    item_2:CpaItem;
    item_3:CpaItem;
    t0:fairygui.Transition;

    constructor() { super(); }

    protected onInit():void
    {
        this.initView();
        this.initEvent();
    }

    private initView():void
    {
        this.c_state = this.getController( "c_state" );
        this.t0 = this.getTransition( "t0" );
        this.c_state.selectedIndex = 0;
    }

    private initEvent():void
    {
        console.log("监听互推事件---BaokuanGame0");
        this.AddEventData( CpaEvent.YQHomeData,this,this.yqhomedataHandler );
    }

    private yqhomedataHandler():void
    {
        console.log("监听互推事件---BaokuanGame1");
        this.RefreshBKYX();
        TimerService.Instance.addUpdateTarget( this );
    }

    private _time:number = 0;
    public update( dt:number ):void
    {
        if( this._istart )
        {
            this._time += dt;
            if( this._time >= 5000 )
            {
                this._time = 0;
                this.RefreshBKYX( true );
            }
        }
    }

    key = 0;
    private _istart:boolean = false;
    RefreshBKYX( isanmo:boolean = false )  
    {
        if (!PlayerAccount.CurrentAccount.CurAccountData.recommend) return;
        console.log("监听互推事件---BaokuanGame2");
        this.c_state.selectedIndex = 1;
        var datas = Recommend.cainixihuanData;
        var item:CpaItem;
        for (var i = 0; i < 4; i++)  {
            if (this.key + i >= datas.length - 1)  {
                this.key = 0;
            }
            var curdata = datas[this.key + i];  
            item = this["item_"+i];
            item.showData( curdata,true );
            if (curdata == null) continue;
            
        }
        this.key += 4;
        if (this.key >= datas.length - 1)  {
            this.key = 0;
        }
        this._istart = true;
        if( isanmo )
        {
            this.t0.play( null,1 );
        }
    }
}