import { ui } from "../ui/layaMaxUI";
import { IWindow } from "./Component/IWindow";
import { WindowManager, WindowType } from "./WindowManager";
import { PlayerAccount, AccountConfig, signdata, entity, AccountUser } from "../Scripts/Network/PlayerAccount";
import { NetworkManager, PROTOCOLS } from "../Scripts/Network/NetworkManager";
import { GameUtils, TextData } from "../GameUtils/GameUtils";
import WxApi from "../WXSDK/wxsdk";
import ADPlayer from "../WXSDK/ADPlayer";

export enum SignItemState
{
    ready,locking,complete
}

export class JiangLi
{
    constructor(_num:number,_amount:number)
    {
        this.jianglinum =_num;
        this.jiangliamount = _amount;
    }
    public jianglinum:number;
    public jiangliamount:number;
}

export class QianDao
{
    public qiandaonum:number;
    public mold:SignItemState;
    public jianglis:string;
    public jiangliamount:string;
    public miaoshu:string;
    public logo:string;
    public weight:string;

    public get JiangLiShuJu():JiangLi
    {
        var jianglishuju:Array<string> = this.jianglis.split(";");
        
        if(jianglishuju.length > 1)
        {
            return new JiangLi(Number(6000),Number(1));
        }
        else
        {
            return new JiangLi(Number(this.jianglis),Number(this.jiangliamount));
        }
    }
}

export class SignWindow extends ui.Window.SignWindowUI implements IWindow
{
    private static Viewmode:SignWindow;
    constructor()
    {
        super();
        SignWindow.Viewmode = this;
    }

    public static get Mode()
    {
        return SignWindow.Viewmode;
    }

    private JiangLiLieBiao:Object = {};
    private qiandaoshujus:Array<QianDao> = [];
    //private donghuas:Laya.FrameAnimation[] = [];

    OnOpen(_data = null)
    {
        this.qiandaoshujus = this.GetQinDaoShuJu();

        this.ShuaXinLieBiao();

        this.ShuangBei.visible = SignWindow.keyiqiandao;

        this.ShuangBei.clickHandler = Laya.Handler.create(this,this.OnDoubleSign,null,false);
        this.return.clickHandler = Laya.Handler.create(this,this.GoHome);
        this.ptBtn.clickHandler = Laya.Handler.create(this,this.OnSign,null,false);
        // ADPlayer.showBannerAd();
        ADPlayer.MoveButton(this.BtnBox);

        this.height = Laya.stage.height;
    }

    GoHome()
    {
        WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
    }

    GetQinDaoShuJu():QianDao[]
    {
        var qiandaos = [];

        var peizhi = PlayerAccount.CurrentAccount.CurAccountData.GetConfig(AccountConfig.sign);

        var qiandaotian = Number(PlayerAccount.CurrentAccount.CurAccountData.GetSign(signdata.sign_days));

        var diyizhou = qiandaotian < 7;
        var afsafssfa = diyizhou?0:7;

        for(var i = afsafssfa;i < afsafssfa + 7;i++)
        {
            this.JiangLiLieBiao[peizhi[i].id] = peizhi[i].rewards;
        }

        for(var key in this.JiangLiLieBiao)
        {
            var jianglinum = this.JiangLiLieBiao[key];
            var jiangli = PlayerAccount.CurrentAccount.CurAccountData.GetRewardConfig(jianglinum);
            var afsfsafs = new QianDao();
            afsfsafs.miaoshu = jiangli.group_name;
            afsfsafs.logo = jiangli.icon;
            afsfsafs.qiandaonum = Number(jiangli.id);
            afsfsafs.jianglis = jiangli.reward_item;
            afsfsafs.jiangliamount = jiangli.reward_num;
            afsfsafs.weight = jiangli.reward_weight;

            var fasafsaf = Number(key);

            if(qiandaotian > fasafsaf - 1)afsfsafs.mold = SignItemState.complete;
            if(qiandaotian < fasafsaf - 1)afsfsafs.mold = SignItemState.locking;
            if(qiandaotian == fasafsaf - 1 && SignWindow.keyiqiandao)
            {
                afsfsafs.mold = SignItemState.ready;
            }
            
            qiandaos.push(afsfsafs);
        }
        return qiandaos;
    }

    dangqiandaonghua:Laya.FrameAnimation;
    ShuaXinLieBiao()
    {
        for(var i = 0;i < this.qiandaoPan.numChildren;i++)
        {
            var qindao = this.qiandaoPan.getChildAt(i);
            var shuju = this.qiandaoshujus[i];

            var jianglinum = shuju.JiangLiShuJu.jianglinum;

            var award = qindao.getChildByName("jiangli").getChildByName("money") as Laya.Box;
            award.visible = true;
            var clip = award.getChildAt(1) as Laya.FontClip;
            clip.value = "x" + shuju.JiangLiShuJu.jiangliamount.toString();
            var icon = award.getChildAt(0) as Laya.Image;
            icon.skin = "sign/qiandao_1_5_4.png";

            var bg = qindao.getChildByName("bg") as Laya.Image;
            bg.skin = "sign/qiandao_1_3.png";

            var money = qindao.getChildByName("jiangli").getChildByName("money") as Laya.Box;
            money.visible = false;
            var role = qindao.getChildByName("jiangli").getChildByName("role") as Laya.Box;
            role.visible = false;
            var cb = qindao.getChildByName("jiangli").getChildByName("cb") as Laya.Box;
            cb.visible = false;

            if(jianglinum == 1000)
            {
                money.visible = true;
                var clip = money.getChildAt(1) as Laya.FontClip;
                clip.value = shuju.JiangLiShuJu.jiangliamount.toString();
                var sprite = money.getChildAt(0) as Laya.Image;
                sprite.skin = "sign/qiandao_1_5_4.png";
            }
            else if(jianglinum >= 7001 && jianglinum <= 70012)
            {
                role.visible = true;
                var roleicon = PlayerAccount.CurrentAccount.CurAccountData.GetEntity(jianglinum.toString(),entity.icon);
                var sprite = role.getChildAt(0) as Laya.Image;
                sprite.skin = "shop/"+ roleicon + ".png";
            }
            else if(jianglinum >= 8001 && jianglinum <= 8006)
            {
                cb.visible = true;
                var cbicon = PlayerAccount.CurrentAccount.CurAccountData.GetEntity(jianglinum.toString(),entity.icon);
                var sprite = cb.getChildAt(0) as Laya.Image;
                sprite.skin = "shop/"+ cbicon + ".png";
            }
            
            var cansign = shuju.mold;
            if(cansign == SignItemState.complete)
            {
                var complete = qindao.getChildByName("complete") as Laya.Sprite;
                complete.visible = true;
                bg.skin = "sign/qiandao_1_4_2.png";
            }
            else if(cansign == SignItemState.ready)
            {
                var btn = qindao.getChildByName("button") as Laya.Button;
                btn.clickHandler = Laya.Handler.create(this,this.OnSign,null,false);
                bg.skin = "sign/qiandao_1_4.png";
            }
        }
    }

    OnSign()
    {
        
        var curgold = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money));
        let param = [{"video":"0"}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.Sign,param,Laya.Handler.create(this,function(e)
        {
            console.log("OnSign---111111111");
            var reward = e["rewards"];
            SignWindow.Mode.qiandaoshujus = SignWindow.Mode.GetQinDaoShuJu();
            SignWindow.Mode.ShuangBei.visible = false;
            SignWindow.Mode.ShuaXinLieBiao();

            if(reward["1000"])
            {
                console.log("OnSign---222222");
                var gg = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money)) - curgold;
                GameUtils.GetMoney(gg,null,Laya.Handler.create(this,()=>
                {
                    console.log("OnSign---3333333");
                    WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
                }));
            }
            else
            {

                GameUtils.TweenText(new TextData("皮肤 + 1",new Laya.Vector2(Laya.stage.width/2,Laya.stage.height*0.35),new Laya.Vector2(250,80),30),150,Laya.Handler.create(this,()=>
                {
                    WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
                }))
            }
            
        }),Laya.Handler.create(this,this.OnSignError),true);
    }

    OnDoubleSign()
    {
        console.log("双倍领取doublesign");
        //WxApi.Message("暂未开放");
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                var curgold = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money));
                let param = [{"video":"1"}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.Sign,param,Laya.Handler.create(this,function(e)
                {
                    var reward = e["rewards"];
                    SignWindow.Mode.qiandaoshujus = SignWindow.Mode.GetQinDaoShuJu();
                    SignWindow.Mode.ShuangBei.visible = false;
                    SignWindow.Mode.ShuaXinLieBiao();

                    if(reward["1000"])
                    {
                        var gg = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money)) - curgold;
                        GameUtils.GetMoney(gg,null,Laya.Handler.create(this,()=>
                        {
                            WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
                        }));
                    }
                    else
                    {
                        GameUtils.TweenText(new TextData("皮肤 + 1",new Laya.Vector2(Laya.stage.width/2,Laya.stage.height*0.35),new Laya.Vector2(250,80),30),150,Laya.Handler.create(this,()=>
                        {
                            WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
                        }))
                    }
                    
                }),Laya.Handler.create(this,this.OnSignError),true);
            }
        })
    }

    OnSignError(e)
    {
        console.log(e)
    }

    OnHide()
    {

    }

    static get keyiqiandao():boolean
    {
        let fassafsafs = new Date(PlayerAccount.CurrentAccount.ServerTime * 1000);
        fassafsafs.setHours(0, 0, 0, 0);

        var faafsafs = fassafsafs.valueOf();

        var dadadas = Number(PlayerAccount.CurrentAccount.CurAccountData.GetSign(signdata.sign_time));
        var asagsag = dadadas*1000;
        var faafs = asagsag < faafsafs?true:false;
        return faafs;
    }

}