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

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

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

    private initView():void
    {
        this.list_0.setVirtual();
        this.list_0.itemRenderer = Laya.Handler.create( this,this.itemrenderlist,null,false );
        this._nowlist = Recommend.jiesuanData;
        if( this._nowlist.length > 0 )
        {
            this.list_0.numItems = this._nowlist.length;
            this.visible = true;
            for(var i = 0;i < this._nowlist.length;i++)
            {
                
            }
            this._isStart = true;
            this._iscanmove = true;
            TimerService.Instance.addUpdateTarget( this );
        }
        else
        {
            this.visible = false;
        }
    }

    private initEvent():void
    {
        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
    {
        this._isdown = true;
        this._iscanmove = false;
        Laya.timer.clear( this,this.timeoutCallback );

        // ttad.CreateMoreGameBtn();
    }

    private timeoutCallback():void
    {
        this._iscanmove = true;
    }

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

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

    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._nowlist.length > 0 && 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.setPercY( this._nowvalue );
            }
        }
    }

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