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

export default class CpaDownsView extends PanelChildControl implements TimerControl
{
    c_state:fairygui.Controller;
    list_0:fairygui.GList;
    private _nowList:RecommendItem[];
    private _isStart:boolean;
    private _iscanmove:boolean = false;
    private _isdown:boolean;
    constructor() { super(); }

    protected onInit():void
    {

        this.c_state = this.getController( "c_state" );
        this.c_state.selectedIndex = 0;
        this.initView();
        this.initEvent();


    }

    private initView():void
    {
        this.list_0.setVirtual();
        this.list_0.itemRenderer = Laya.Handler.create( this,this.itemrenderlist,null,false );
        this._isStart = false;
    }
    
    private initEvent():void
    {
        this.AddEventData( CpaEvent.YQHomeData,this,this.yqhomedataHandler );
        this.AddEventByType( this.list_0,Laya.Event.MOUSE_DOWN,this,this.downclick );
        this.AddEventByType( this.list_0,Laya.Event.MOUSE_UP,this,this.upclick );
        this.AddEventByType( this.list_0,Laya.Event.MOUSE_OUT,this,this.upclick );
    }

    private downclick():void
    {
        if(SceneControl.IsShowSign)return;
        this._isdown = true;
        this._iscanmove = false;
        Laya.timer.clear( this,this.timeoutCallback );

        // ttad.CreateMoreGameBtn();
    }

    private timeoutCallback():void
    {
        if(SceneControl.IsShowSign)return;
        this._iscanmove = true;
    }

    private upclick():void
    {
        if(SceneControl.IsShowSign)return;
        if( this._isdown )
        {
            this._isdown = false;
            this._nowvalue = this.list_0.scrollPane.percX;
            Laya.timer.once( 2000,this,this.timeoutCallback );
        }
    }

    private _movetime:number = 0;
    private _flushtime:number = 0;
    private dir:boolean = true;
    private _nowvalue:number = 0;
    public update( dt:number ):void
    {
        if( this._isStart )
        {
            if( this.list_0.scrollPane && this._nowList && this._iscanmove )
            {
                if( this.dir )
                {
                    this._nowvalue += 0.001;
                    if( this._nowvalue >= 1 )
                    {
                        this._nowvalue = 1;
                        this.dir = false;
                    }
                }
                else
                {
                    this._nowvalue -= 0.001;
                    if( this._nowvalue <= 0 )
                    {
                        this._nowvalue = 0;
                        this.dir = true;
                    }
                }
                this.list_0.scrollPane.setPercX( this._nowvalue );
            }
        }
    }

    private itemrenderlist( index:number,item:CpaItem ):void
    {
        if( index < this._nowList.length )
        {
            item.showData( this._nowList[index],true );
        }
    }

    private yqhomedataHandler():void
    {
        this.c_state.selectedIndex = 1;
        this._nowList = Recommend.cainixihuanData;
        this.list_0.numItems = this._nowList.length;
        for(var i = 0;i < this._nowList.length;i++)
        {
            
        }
        this._isStart = true;
        this._iscanmove = true;
        TimerService.Instance.addUpdateTarget( this );
    }

    public dispose():void
    {
        TimerService.Instance.removeUpdateTarget(this);
        super.dispose();
    }

}