import { ui } from "../ui/layaMaxUI";
import { IWindow } from "./Component/IWindow";
import { GameManager, GameState } from "../Scripts/Game/GameManager";
import { WindowManager, WindowType } from "./WindowManager";
import { PlayerAccount, AccountUser, ReData } from "../Scripts/Network/PlayerAccount";
import WxApi from "../WXSDK/wxsdk";
import { RecommendWindow } from "./RecommendWindow";
import { Main } from "../Main";
import { TrialWindow } from "./TrialWindow";
import { OffLineWindow } from "./OffLineWindow";
import { FriendPlyaingWindow } from "./FriendPlayingWindow";
import { MoreFunWindow }   from "./MoreFunWindow";

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

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

    Mask1:Laya.Sprite;
    Mask2:Laya.Sprite;

    OnOpen(_data = null)
    {
        this.baginBtn.clickHandler = Laya.Handler.create(this,this.OnClickBagin,null,false);
        this.skinshop.clickHandler = Laya.Handler.create(this,this.OnOpenSkinShop);
        this.settingBtn.clickHandler = Laya.Handler.create(this,this.OnOpenSetting);
        this.rankBtn.clickHandler = Laya.Handler.create(this,this.OnOpenRank);
        this.signBtn.clickHandler = Laya.Handler.create(this,this.OnOpenSign);
        this.taskBtn.clickHandler = Laya.Handler.create(this,this.OnOpenTask,null,false);

        //新增 LeaveBtn moreFunBtn ---2019.09.21 赵成阳
        this.LeaveBtn.clickHandler = Laya.Handler.create(this,this.onOpenLeave);
        this.moreFunBtn.clickHandler = Laya.Handler.create(this,this.onOpenMoreFun);

        

        this.guide.on(Laya.Event.MOUSE_DOWN,this,this.OnGuideClick);
         WindowManager.Instance.OpenPopView(WindowType.RecommendWindow)
         
         if(!GameManager.Instance.GetGoin){
            WindowManager.Instance.OpenPopView(WindowType.OffLineWindow);
         }
         
        RecommendWindow.Instance.cheat = true;
       
        OffLineWindow.instance.ShowOffLine(true,true,true,true,true);
        RecommendWindow.Instance.cainixihuan.bottom = 0;
        RecommendWindow.Instance.choutiBtn.clickHandler = Laya.Handler.create(this,this.OnCloseCT,null,false);

        this.ShowBaoKuanYouXiView();

        //this.OnOpenOffLine();
        

        this.goldTex.value = PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money);
        this.curswtage.value = (Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.stage))+1).toString();
        //this.ShowGuide();
        this.height = Laya.stage.height;
    }

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

    index = 0;
    RefreshBaoKuanYouXi()
    {
        var data = PlayerAccount.CurrentAccount.CurAccountData.redata[this.index];
        var icon = this.rebtn.getChildByName("icon") as Laya.Image;
        icon.skin = data.icon;
        var name = this.rebtn.getChildByName("name") as Laya.Text;
        name.text = data.title;
        this.rebtn.clickHandler = Laya.Handler.create(this,this.ClickBNavigate,[data],false);
    }

    ClickBNavigate(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");

            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");
                            RecommendWindow.Instance.OpenChouTiView(true);
                            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");
                    }
                });
            }
        }
    }

    OnGuideClick()
    {
        this.guide.on(Laya.Event.MOUSE_MOVE,this,this.OnClickBagin);
    }

    ShowGuide()
    {
        this.Mask1 = new Laya.Sprite();
        this.Mask1.graphics.drawRect(0,0,this.guide1.width, this.guide1.height, "#000000");
        this.Mask1.pivot(this.guide1.width,0);
        this.guide1.mask = this.Mask1;
        this.Mask2 = new Laya.Sprite();
        this.Mask2.graphics.drawRect(0,0,this.guide2.width, this.guide2.height, "#000000");
        this.Mask2.pivot(this.guide2.width,0);
        this.guide2.mask = this.Mask2;
        this.offestx = 0;
        this.offestx2 = 0;
        Laya.timer.frameLoop(1, this, this.RefreshMask1);
    }
    
    offestx = 0;
    RefreshMask1()
    {
        this.offestx += 3;
        if(this.offestx > this.guide1.width)
        {
            Laya.timer.clear(this,this.RefreshMask1);
            this.offestx = 0;
            this.Mask1.pos(this.offestx,0);
            //this.hand.x = this.guide1.x + this.offestx;
            Laya.timer.frameLoop(1, this, this.RefreshMask2);
            return;
        }
        this.Mask1.pos(this.offestx,0);
        this.hand.x = this.guide1.x + this.offestx;
    }

    offestx2 = 0;
    RefreshMask2()
    {
        this.offestx2 += 3;
        if(this.offestx2 > this.guide2.width)
        {
            Laya.timer.clear(this,this.RefreshMask2);
            this.offestx2 = 0;
            this.Mask2.pos(this.guide1.x + this.guide1.width,0);
            //this.hand.x = this.guide2.x + this.guide2.width - this.offestx2;
            Laya.timer.frameLoop(1, this, this.RefreshMask1);
            return;
        }
        this.Mask2.pos(this.offestx2,0);
        this.hand.x = this.guide2.x - this.offestx2;
    }

    OnClickBagin()
    {
        WxApi.Instance.recordAldEvent("主界面点击开始游戏","");
        if(TrialWindow.trial && PlayerAccount.CurrentAccount.CurAccountData.trial)
        {
            WindowManager.Instance.ShowWindow(WindowType.TrialWindow);
        }
        else
        {
            GameManager.Instance.ChangeState(GameState.Queue);
        }
        
    }

    OnOpenSkinShop()
    {
        WxApi.Instance.recordAldEvent("主界面点击皮肤商城","");
        WindowManager.Instance.ShowWindow(WindowType.ShopWindow);
    }
    OnOpenSetting()
    {
        WxApi.Instance.recordAldEvent("主界面点击设置界面","");
        WindowManager.Instance.ShowWindow(WindowType.SettingWindow)
    }

    OnOpenRank()
    {
        WxApi.Instance.recordAldEvent("主界面点击排行榜","");
        WindowManager.Instance.ShowWindow(WindowType.RankWindow);
    }

    OnOpenSign()
    {
        WxApi.Instance.recordAldEvent("主界面点击签到界面","");
        WindowManager.Instance.ShowWindow(WindowType.SignWindow);
    }

    OnOpenTask()
    {
        WxApi.Instance.recordAldEvent("主界面点击任务界面","");
        WxApi.Message("暂未开放");
        //WindowManager.Instance.ShowWindow(WindowType.RankWindow);
    }

    //新增 onOpenLeave onOpenMoreFun ---2019.09.21 赵成阳
    onOpenLeave(){
        WxApi.Instance.recordAldEvent("主界面点击离开游戏","");
        //WindowManager.Instance.ClosePopView(WindowType.RecommendWindow);
        WindowManager.Instance.ShowWindow(WindowType.MoreFunWindow);
        //RecommendWindow.Instance.OpenChouTiView(true);
        
    }
    
    onOpenMoreFun(){
        WxApi.Instance.recordAldEvent("主界面点击更多好玩游戏","");
        WindowManager.Instance.ShowWindow(WindowType.FriendPlayingWindow);
        
    }
    OnOpenOffLine(){
        WxApi.Instance.recordAldEvent("离线收益界面开启","");
        WindowManager.Instance.ShowWindow(WindowType.OffLineWindow);
    }

    OnCloseCT()
    {
        RecommendWindow.Instance.OpenChouTiView(true);
        RecommendWindow.Instance.closeCt.clickHandler = Laya.Handler.create(this,()=>
        {
            WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
        })
    }

    OnHide()
    {
        this.guide.off(Laya.Event.MOUSE_DOWN,this,this.OnGuideClick);
        this.guide.off(Laya.Event.MOUSE_MOVE,this,this.OnClickBagin);
        Laya.timer.clearAll(this);
    }
}