import { ui } from "../ui/layaMaxUI";

import { Main } from "../Main";
import { IWindow } from "./Component/IWindow";
import { GameUtils } from "../GameUtils/GameUtils";
import WxApi from "../WXSDK/wxsdk";
import HttpRequest = Laya.HttpRequest;
import { WindowManager, WindowType } from "./WindowManager";
import ADPlayer from "../WXSDK/ADPlayer";
import { PlayerAccount, ReData } from "../Scripts/Network/PlayerAccount";
import { GameManager, GameState } from "../Scripts/Game/GameManager";


export class RecommendWindow extends ui.Window.RecommendWindowUI implements IWindow
{
    private static instance:RecommendWindow;
    constructor()
    {
        super();
        RecommendWindow.instance = this;
    }

    public static get Instance()
    {
        return RecommendWindow.instance;
    }

    cheat = false;

    OnOpen(_data = null)
    {
        this.choutiview.visible = false;
        this.height = Laya.stage.height;
    }

    public ShowRecommendWindow(_cainixihuan:boolean,fuhuotuijian:boolean,_chouti:boolean,_siwangtuijian:boolean,_baokuanyouxi:boolean)
    {
        Laya.timer.clearAll(this);
        if(_cainixihuan || (!_cainixihuan && !fuhuotuijian))
        {
            this.ShowCaiNiXiHuanView(_cainixihuan);
        }
        if(fuhuotuijian || (!_cainixihuan && !fuhuotuijian))
        {
            this.ShowCaiNiXiHuanView(fuhuotuijian);
        }
        this.ShowSiWangTuiJianView(_siwangtuijian);
        this.ShowChouTi(_chouti);
        this.ShowBaoKuanYouXiView(_baokuanyouxi);
    }

    ShowBaoKuanYouXiView(_open:boolean)
    {
        this.baokuanyouxi.visible = _open;

        if(_open == false)return;

        this.RefreshBaoKuanYouXi();
        Laya.timer.loop(2000,this,this.LoopBaoKuan);
    }
    LoopBaoKuan()
    {
        this.index ++;
        if(this.index > PlayerAccount.CurrentAccount.CurAccountData.redata.length - 1)
        {
            this.index = 0;
        }
        this.RefreshBaoKuanYouXi();
    }

    index = 0;
    RefreshBaoKuanYouXi()
    {
        var data = PlayerAccount.CurrentAccount.CurAccountData.redata[this.index];
        var icon = this.baokuanyouxi.getChildByName("icon") as Laya.Image;
        icon.skin = data.icon;
        var btn:Laya.Button = this.baokuanyouxi.getChildByName("btn") as Laya.Button;
        btn.clickHandler = Laya.Handler.create(this,this.ClickBNavigate,[data],false);
    }

    ShowCaiNiXiHuanView(_open:boolean)
    {
        this.cainixihuan.visible = _open;
        
        if(_open == false)return;

        this.cainixihuan.bottom = 0;
        this.cainixihuanlist.hScrollBarSkin = "";
        this.cainixihuanlist.array = PlayerAccount.CurrentAccount.CurAccountData.redata;
        this.cainixihuanlist.renderHandler = Laya.Handler.create(this,this.RenderList,null,false);
        Laya.timer.loop(10,this,this.LoopList,[this.cainixihuanlist]);
        this.loopdir = 1;

        // for(var i = 0;i < this.cainixihuanlist.array.length;i++)
        // {
        //     this.cainixihuanlist.array[i].shownum ++;
        // }
    }

    ShowChouTi(_open:boolean)
    {
        this.chouti.visible = _open;
        this.choutiBtn.visible = true;
        if(_open == false)return;
        ADPlayer.ClearBanner();
        this.choutiBtn.clickHandler = Laya.Handler.create(this,this.OpenChouTiView);
        this.closeCt.clickHandler = Laya.Handler.create(this,this.OnBackHome);
    }

    OnBackHome()
    {
       // if(WindowManager.Instance.curOpenedView.name == "HomeWindow") return;
       WindowManager.Instance.ClosePopView(WindowType.RecommendWindow);
       

       // WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
    }

    OpenChouTiView(_open:boolean)
    {
        
        this.choutiview.visible = _open;
        this.sharebtn.visible = true;
         
        if(_open == false)
        {
            if(WindowManager.Instance.curOpenedView.name == "HomeWindow")WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
            return;
        }

        this.choutilist.vScrollBarSkin = "";
        this.choutilist.array = PlayerAccount.CurrentAccount.CurAccountData.redata;
        this.choutilist.renderHandler = Laya.Handler.create(this,this.RenderList,null,false);
        this.ctList.hScrollBarSkin = "";
        this.ctList.array = PlayerAccount.CurrentAccount.CurAccountData.redata;
        this.ctList.renderHandler = Laya.Handler.create(this,this.Renderct2List,null,false);
        Laya.timer.loop(10,this,this.LoopList,[this.ctList]);
        this.loopdir = 1;
        this.closeCt.clickHandler = Laya.Handler.create(this,this.OpenChouTiView,[false],false);
        this.sharebtn.clickHandler = Laya.Handler.create(this,this.OnShare,null,false);
    }
    ShowSiWangTuiJianView(_open:boolean)
    {
        this.siwangtuijian.visible = _open;

        if(_open == false)return;

        this.siwangtuijianlist.vScrollBarSkin = "";
        this.siwangtuijianlist.array = PlayerAccount.CurrentAccount.CurAccountData.redata;
        this.siwangtuijianlist.renderHandler = Laya.Handler.create(this,this.RenderList,null,false);
        Laya.timer.loop(10,this,this.LoopList,[this.siwangtuijianlist]);
        this.loopdir = 1;
        for(var i = 0;i < this.siwangtuijianlist.array.length;i++)
        {
            this.siwangtuijianlist.array[i].shownum ++;
        }
        
        // HpApi.Instance.endPageList.sort(this.ListSort);
    }

    OnShare()
    {
        WxApi.shareImage("互推界面分享",null,"1");
    }

    static ListSort(a:ReData,b:ReData):number
    {
        if(a.sort > b.sort)
        {
            return 1;
        }
        else
        {
            return - 1;
        }
    }

    RenderList(_box:Laya.Box,index:number)
    {
        var data:ReData = _box.dataSource as ReData;
        var icon:Laya.Image = _box.getChildAt(0).getChildByName("icon") as Laya.Image;
        icon.skin = data.icon;
        var name:Laya.Text = _box.getChildAt(0).getChildByName("name") as Laya.Text;
        name.text = data.title;
        
        var btn:Laya.Button = _box.getChildAt(0).getChildByName("btn") as Laya.Button;

        var num:Laya.Text = _box.getChildAt(0).getChildByName("num") as Laya.Text;
        if(num)num.text = GameUtils.getRandomInt(24841,654878).toString() + "人玩";
        
        if(this.cheat)
        {
            btn.clickHandler = Laya.Handler.create(this,this.ClickBNavigate,[data],false);
        }
        else
        {
            btn.clickHandler = Laya.Handler.create(this,this.ClickNavigate,[data],false);
        }
       
    }

    Renderct2List(_box:Laya.Box,index:number)
    {
        var data:ReData = _box.dataSource as ReData;
        var icon:Laya.Image = _box.getChildByName("icon") as Laya.Image;
        icon.skin = data.icon;
        var btn:Laya.Button = _box.getChildByName("btn") as Laya.Button;

        if(this.cheat)
        {
            btn.clickHandler = Laya.Handler.create(this,this.ClickBNavigate,[data],false);
        }
        else
        {
            btn.clickHandler = Laya.Handler.create(this,this.ClickNavigate,[data],false);
        }
        
    }

    ClickBNavigate(data:ReData)
    {
        var appid = data.appid;
        var path = data.path;
        let param = {"appId":appid.toString(),"path":path.toString()};
        RecommendWindow.Instance.OpenChouTiView(true);
        if(WindowManager.Instance.curOpenedView.name == "ReviveWindow")
        {
            RecommendWindow.Instance.closeCt.clickHandler = Laya.Handler.create(this,()=>
        {
            RecommendWindow.Instance.ShowRecommendWindow(false,false,false,true,false);
        });

        }
        if(Main.OnWx)
        {
            WxApi.Instance.recordAldEvent("导出-总点击数","1");
            
            if(data.jump)
            {
                Laya.Browser.window.wx.navigateToMiniProgram(
                    {
                        appId:appid.toString(),
                        path:path.toString(),
    
                        extraData:
                        {
                            next_appid:data.next_appid,
                            next_path:data.next_path
                        },
                        envVersion:'develop',
                        success:res=>
                        {
                            WxApi.Instance.recordAldEvent("导出成功-总用户数","1");
                            WxApi.Instance.recordAldEvent("导出成功-" + data.title,"1");
                            console.log("跳转成功...");
                        },
                        fail:res=>
                        {
                            //WxApi.Instance.recordAldEvent("跳转到游戏失败:"+ data.title,"1");
                           
                           
                            //this.closeCt.clickHandler = Laya.Handler.create()
                            console.log("跳转失败...");
                        }
                    });
            }
            else
            {
                var marray = [data.image];
                Laya.Browser.window.wx.previewImage({
                    current: data.image,
                    urls: marray,
                    success: res=> {
                        WxApi.Instance.recordAldEvent("显示游戏二维码成功","1");
                    },
                    fail: res=> {
                        //WxApi.Instance.recordAldEvent("显示游戏二维码失败","1");
                    }
                });
            }
        }
    }

    ClickNavigate(data:ReData)
    {
        var appid = data.appid;
        var path = data.path;
        let param = {"appId":appid.toString(),"path":path.toString()};
        
        if(Main.OnWx)
        {
            WxApi.Instance.recordAldEvent("导出-总点击数","1");
            //navigate2Mini(data,Account.Instance.curplayerData.openid);

            if(data.jump)
            {
                Laya.Browser.window.wx.navigateToMiniProgram(
                    {
                        appId:appid.toString(),
                        path:path.toString(),
    
                        extraData:
                        {
                            next_appid:data.next_appid,
                            next_path:data.next_path
                        },
                        envVersion:'develop',
    
                        success:res=>
                        {
                            WxApi.Instance.recordAldEvent("导出成功-总用户数","1");
                            WxApi.Instance.recordAldEvent("导出成功-" + data.title,"1");
                            console.log("跳转成功...");
                        },
                        fail:res=>
                        {
                            //WxApi.Instance.recordAldEvent("跳转到游戏失败","1");
                            console.log("跳转失败...");
                        }
                    });
            }
            else
            {
                var marray = [data.image];
                Laya.Browser.window.wx.previewImage({
                    current: data.image,
                    urls: marray,
                    success: res=> {
                        WxApi.Instance.recordAldEvent("显示游戏二维码成功","1");
                    },
                    fail: res=> {
                        //WxApi.Instance.recordAldEvent("显示游戏二维码失败","1");
                    }
                });
            }

            
        }
    }

    loopdir = 1;
    LoopList(_list:Laya.List)
    {
        if(this.loopdir > 0)
        {
            if(_list.scrollBar.value < _list.scrollBar.max)
            {
                _list.scrollBar.value += 1;
            }
            else
            {
                this.loopdir *= -1;
            }
        }
        else
        {
            if(_list.scrollBar.value > 0)
            {
                _list.scrollBar.value -= 1;
            }
            else
            {
                this.loopdir *= -1;
            }
        }
    }

    OnHide()
    {
        this.cheat = false;
        this.ShowRecommendWindow(false,false,false,false,false);
        Laya.timer.clearAll(this);
    }
    

    
}