import {PanelBaseControl} from "../Control/PanelBaseControl";
import CpaItem from "./CpaItem";
import { RecommendItem, Recommend } from "./RecommendView";
import { ViewManager } from "../View/ViewManager";
import {TimerService} from "../Time/TimerService";
import { DispatcherManager } from "../utils/DispatcherManager";
import { GameState } from "../utils/GameState";
import { CompletePanel } from "../View/CompletePanel";
import { RoleData } from "../Data/RoleData";
import { FYHD_Sdk } from "../WXSDK/FYHD_Sdk";

export default class ChoutiPanView extends PanelBaseControl implements TimerControl
{
    static key:string = "ChoutiPanView";
    btn_return:fairygui.GButton;
    list_0:fairygui.GList;
    list_1:fairygui.GList;
    btn_jixugame:fairygui.GButton;
    private _nowList0:RecommendItem[];
    private _nowList1:RecommendItem[];
    private _isStart:boolean;
    private _iscanmove:boolean = false;
    private _isdown:boolean;
    

    private isend:number = 0;

    constructor(end?) 
    { 
        super( "main","choutipanView" ); 
        this.isend = end;
    }

    protected initView():void
    {
        this.list_0.setVirtual();
        this.list_0.itemRenderer = Laya.Handler.create( this,this.itemrenderlist,null,false );
        this.list_1.setVirtual();
        this.list_1.itemRenderer = Laya.Handler.create( this,this.itemrenderlist1,null,false );

        this._nowList0 = Recommend.gdyxData;
        this._nowList1 = Recommend.gdyxData;
        this.list_0.numItems = this._nowList0.length;
        this.list_1.numItems = this._nowList1.length;
        this._isStart = true;
        this._iscanmove = true;
        TimerService.Instance.addUpdateTarget( this );
    }

    protected initEvent():void
    {
        this.AddEventByType( this.btn_return,Laya.Event.CLICK,this,this.onclosefun );
        this.AddEventByType( this.btn_jixugame,Laya.Event.CLICK,this,this.onclosefun );
        this.AddEventByType( this.list_1,Laya.Event.MOUSE_DOWN,this,this.downclick );
        this.AddEventByType( this.list_1,Laya.Event.MOUSE_UP,this,this.upclick );
        this.AddEventByType( this.list_1,Laya.Event.MOUSE_OUT,this,this.upclick );
    }

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

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

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

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

    private upclick():void
    {
        if( this._isdown )
        {
            this._isdown = false;
            this._nowvalue = this.list_1.scrollPane.percY;
            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_1.scrollPane && this._nowList1 && 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_1.scrollPane.setPercY( this._nowvalue );
            }
        }
    }

    private onclosefun():void
    {
        if( this.isend == 1){
            ViewManager.Instance.close( ChoutiPanView );
            DispatcherManager.dispatchEvent( GameState.lose );
        }
        else if( this.isend == 2){
           let gold =  RoleData.Instance.levelcfg.winGold;
           ViewManager.Instance.close(ChoutiPanView);
           ViewManager.Instance.add(CompletePanel,gold);
        }
        else{
            ViewManager.Instance.close( ChoutiPanView );
        }
    }

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