import { PlatformManager } from "../../../platforms/PlatformManager";
import UI_Export001 from "../../../ui/WXSExport/UI_Export001";
import UI_Export002 from "../../../ui/WXSExport/UI_Export002";
import UI_Export004 from "../../../ui/WXSExport/UI_Export004";



export default class ExportList
{
    private list:fgui.GList;
    private scrollDirection:number=0;
    private count:number=20;
    private itemRenderFunction:Function;
    private itemClick:Function;
    private datas:any[];

    private isForward:boolean;


    /**
     * 导出列表
     * @param list 
     * @param scrollDirection 0横向滚动 1纵向滚动
     * @itemRenderFunction function(data,index,item);
     * @itemClick          function(item);
     */
    constructor(list:fgui.GList,scrollDirection:number=0,itemRenderFunction:Function=null,itemClick:Function=null){
        this.list=list;
        this.list.itemRenderer=this.ItemRender.bind(this);
        this.itemClick=itemClick
        this.itemRenderFunction=itemRenderFunction;
        this.scrollDirection=scrollDirection;
        this.list.on(fgui.Event.CLICK_ITEM,this.ItemClickHandler,this);
    }

    Refresh(count:number=20):void{
        this.count=count;
        this.datas=PlatformManager.getRandomPageAd(count);
        this.list.numItems=this.datas.length;
        this.isForward=true;
    }

    private ItemRender(index:number,obj:fgui.GObject):void{
        let data:any=this.datas[index];
        if(this.itemRenderFunction!=null){
            this.itemRenderFunction(data,index,obj);
        }else{
            let icon:fgui.GLoader;
            if(obj instanceof UI_Export001){
                let ui:UI_Export001=obj as UI_Export001;
                ui.m_txtName.text=data.name;
                icon=ui.m_groupIcon;
            }else if(obj instanceof UI_Export002){
                let ui:UI_Export002=obj as UI_Export002;
                ui.m_txtName.text=data.name;
                icon=ui.m_groupIcon;
            }else if(obj instanceof UI_Export004){
                let ui:UI_Export004=obj as UI_Export004;
                icon=ui.m_groupIcon;
            }
            obj.data=data;
            icon.url=data.icon;
        }
    }

    private ItemClickHandler(item:fgui.GObject):void{
        if(this.itemClick!=null){
            this.itemClick(item);
        }else{
            PlatformManager.navigate2Mini(item.data,()=>{
                cc.log("跳转成功！");
            },()=>{
                cc.log("跳转失败！");
            })
        }
    }

    private lastIsDragged:boolean;
    private isTouch:boolean;
    private touchTime:number;
    Tick(dt:number):void{
        if(this.lastIsDragged!=this.list.scrollPane.isDragged){
            //用户拖拽结束
            if(this.lastIsDragged){
                this.touchTime=cc.sys.now();
            }else{
                this.isTouch=true;
            }
        }
        this.lastIsDragged=this.list.scrollPane.isDragged;
        if(this.isTouch==true){
            if(this.lastIsDragged==false){
                let curTime:number=cc.sys.now();
                if(curTime-this.touchTime>1000){
                    this.isTouch=false;
                }
            }
            return;
        }
        //横向
        if(this.scrollDirection==0){
            if(this.isForward){
                if(this.list.scrollPane.isRightMost){
                    this.isForward=false;
                }
                this.list.scrollPane.scrollRight(0.1);
            }else{
                if(this.list.scrollPane.posX==0){
                    this.isForward=true;
                }
                this.list.scrollPane.scrollLeft(0.1);
            }
        }else{//纵向
            if(this.isForward){
                if(this.list.scrollPane.isBottomMost){
                    this.isForward=false;
                }
                this.list.scrollPane.scrollDown(0.1);
            }else{
                if(this.list.scrollPane.posY==0){
                    this.isForward=true;
                }
                this.list.scrollPane.scrollUp(0.1);
            }
        }
    }
}