import { ui } from "../ui/layaMaxUI";
import { PlayerManager } from "../Manager/PlayerManager";
import { GameTools } from "../GameUtils/GameTools";
import { AccountManager } from "../NetWork/AccountManager";
import { signdata } from "../NetWork/PlayerData";
import GuangGao from "../NetWork/GuangGao";
import { ViewManager, ViewType } from "./ViewManager";
import { ServerManager, TeamWave } from "../NetWork/ServerManager";
export class SignView extends ui.SignViewUI{
    private static ins:SignView;
    constructor(){
        super();
        SignView.ins = this;
    }
    public static get Instance():SignView{
        if(SignView.ins == null){
            return new SignView();
        }
        return SignView.ins;
    }
    OnShow(){
        this.signReward = AccountManager.Instance.curplayerData.GainCollocation("sign");
        this.RegButton();
        this.RefreshUI();
        this.InitData();
        if(!SignView.canSgin) {
            this.receiveBtn.visible = false;
        }else{
            this.receiveBtn.visible = true;
        }
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    RefreshUI(){
        this.GoldNum.text = AccountManager.Instance.curplayerData.GainAccount("money");
    }
    RegButton(){
        this.receiveBtn.clickHandler = new Laya.Handler(this,this.ClickReceive);
        this.closeBtn.clickHandler = new Laya.Handler(this,this.ClickClose);
    }
    ClickReceive(){
        if(Laya.Browser.onWeiXin){
            GuangGao.ShowVideo((isLoad)=>{
                if(isLoad){
                    var agrs = [{ "video": "0" }];
                    ServerManager.Instance.SendHttp(TeamWave.QianDao,agrs,Laya.Handler.create(this,(e)=>
                    {
                        this.InitData();
                        this.RefreshUI();
                        // HomeWindow.Instance.refreshDiamond();
                        this.showReward(e.rewards,()=>{
                            this.ClickClose();
                        });
                    }),Laya.Handler.create(this,this.OnSignError),true);
                }
            })
        }
        else{
            this.ClickClose();
        }
    }
    OnSignError(e){
        console.log(e)
    }
    ClickClose(){
        ViewManager.Instance.ShowView(ViewType.HomeView);
    }
    signReward:any;
    InitData() {
        let day = Number(AccountManager.Instance.curplayerData.GetSign(signdata.sign_days));
        let signtime = Number(AccountManager.Instance.curplayerData.GetSign(signdata.sign_time));
        let curIndex = day%7 + 1;
        
        let first = day < 7;
        let index = first?0:7;

        let rewards = [];
        for(var i = index;i < index + 7;i++)
        {
            rewards[this.signReward[i].id] = this.signReward[i].rewards;
        }
        for(let i = 1 + index;i <= 7 + index;i++) {
            let t = i%8;
            var rewardid = rewards[i];
            var reward = AccountManager.Instance.curplayerData.GetRewardConfig(rewardid);
            let dayBox = this.signList.getChildByName('day' + t).getChildAt(0) as Laya.Image;
            // let isToday = dayBox.getChildByName('isToday') as Laya.Image;
            let item = dayBox.getChildByName('item') as Laya.Image;
            let itemnum = dayBox.getChildByName('itemnum') as Laya.Text;
            let isGet = dayBox.getChildByName('isGet') as Laya.Image;

            // isToday.visible = false;
            isGet.visible = false;
            let type = reward.reward_item;
            if(type == 1000) {//金币
                itemnum.text = reward.reward_num;
            }else{//皮肤
                item.skin = 'subpackage2/res/skin/' + reward.icon + '.png';
                itemnum.visible = false;
                // pifu.skin = 'subpackage2/res/skin/' + reward.icon + '.png';
            }
            
            if(t < curIndex) {
                isGet.visible = true;
            }
        }
    }
    static get canSgin():boolean
    {
        let todayZero = new Date(AccountManager.Instance.ServerTime * 1000);
        todayZero.setHours(0, 0, 0, 0);
        var zeroTime = todayZero.valueOf();
        var signtime = Number(AccountManager.Instance.curplayerData.GetSign(signdata.sign_time));
        var signTime = signtime*1000;
        var can = signTime < zeroTime?true:false;
        return can;
    }

    static get CanVideoSign():boolean
    {
        let todayZero = new Date(AccountManager.Instance.ServerTime * 1000);
        todayZero.setHours(0, 0, 0, 0);
        let lasttime = new Date(AccountManager.Instance.ServerTime * 1000);
        lasttime.setHours(24, 0, 0, 0);
        var mtime = lasttime.valueOf();
        var zeroTime = todayZero.valueOf();
        var signtime = Number(AccountManager.Instance.curplayerData.GetSign(signdata.sign_time));
        var signTime = signtime*1000;
        var can = (signTime > zeroTime && signTime < mtime)?true:false;
        return can;
    }
    showReward(rewards:any,callback:Function) {
        for(let key in rewards) {
            let itemId = Number(key);
            let itemnum = Number(rewards[key]);
            if(itemId == 1000) {//金币
                GameTools.GetMoney(itemnum,null,Laya.Handler.create(this,()=>{
                    callback && callback();
                }))
            }else{
                Laya.timer.once(1000,this,()=>{
                    callback && callback();
                })
            }
        }
    }
    OnHide(){
        
    }
}