import { ui } from "../ui/layaMaxUI";
import { SC, ViewManager, ViewType } from "./ViewManager";
import { AccountManager } from "../Net/AccountManager";
import { ServerManager, BlastToMerge } from "../Net/ServerManager";
import { GameTools,faafssfafsa } from "../Tool/GameTools";
import GuangGao from "../Net/GuangGao";

export enum QianDaoMode {
    start, none, end
}

//奖励类
export class reward {
    constructor(_rewradid: number, _rewradnum: number) {
        this.rewardid = _rewradid;
        this.rewardnum = _rewradnum;
    }
    public rewardid: number;
    public rewardnum: number;
}
//签到物品类
export class SignItem {
    public signday: number;
    public mold: QianDaoMode;
    public rewardids: string;
    public rewardnum: string;
    public desc: string;
    public icon: string;
    public weight: string;
    public get GetRewardData(): reward {
        var data: Array<string> = this.rewardids.split(";");

        if (data.length > 1) {
            return new reward(Number(6000), Number(1));
        }
        else {
            return new reward(Number(this.rewardids), Number(this.rewardnum));
        }
    }
}

export default class SignView extends ui.SignViewUI implements SC{
    private static ins:SignView;
    constructor(){
        super();
        SignView.ins =this;
    }
    public static get Instance():SignView{
        if(SignView.ins){
            return SignView.ins
        }
        else{
            return new SignView();
        }
    }
    SignDatas: SignItem[] = [];
    rewardsdic: Object = {};
    Show(){
        this.SignDatas = this.GainSignDatas();
        this.RefreshDatas();
        this.SignBtn.visible = SignView.CanSign;
        this.SignBtn.clickHandler = Laya.Handler.create(this,this.OnNormalSign);
        this.CloseBtn.clickHandler = Laya.Handler.create(this,this.ClickClose);
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    Close(){
        
    }
    ClickClose(){
        ViewManager.Instance.ShowView(ViewType.MainView);
    }

    /** 更新签到数据*/
    RefreshDatas() {
        for (var i = 0; i < this.ItemList.numChildren; i++) {
            var sign = this.ItemList.getChildAt(i);
            var data = this.SignDatas[i];
            var rewardid = data.GetRewardData.rewardid;
            var icon = sign.getChildByName("icon") as Laya.Image;
            var num = sign.getChildByName("icon").getChildByName("num") as Laya.Text;
            num.text ="X" + data.GetRewardData.rewardnum.toString();
            var roleicon = AccountManager.Instance.curplayerData.GainShiTi(rewardid.toString(), "icon");
            icon.skin = "Game/res/Skin/"+ roleicon + ".png";
            
            var cansign = data.mold;
            if (cansign == QianDaoMode.end) {
                var complete = sign.getChildByName("complete") as Laya.Image;
                complete.visible = true;
            }
            else if (cansign == QianDaoMode.start) {
                var btn = sign.getChildByName("btn") as Laya.Button;
                btn.clickHandler = Laya.Handler.create(this, this.OnDoubleSign, null, false);
            }
        }
    }
    /**签到方法 */
    OnSign() {
        var curgold = Number(AccountManager.Instance.curplayerData.GainAccount("money"));
        let param = [{ "video": "0" }];
        ServerManager.Instance.SendHttp(BlastToMerge.QianDao, param, Laya.Handler.create(this, function (e) {
            var reward = e["rewards"];
            SignView.Instance.SignDatas = SignView.Instance.GainSignDatas();
           SignView.Instance.SignBtn.visible = false;
           SignView.Instance.RefreshDatas();
            if (reward["1000"]) {
                var gg = Number(AccountManager.Instance.curplayerData.GainAccount("money")) - curgold;
                GameTools.GetMoney(gg, null, Laya.Handler.create(this, () => {
                    ViewManager.Instance.ShowView(ViewType.MainView);
                }));
            }
            else {
                GameTools.TweenText(new faafssfafsa("人物 + 1", new Laya.Vector2(Laya.stage.width / 2, Laya.stage.height * 0.35), new Laya.Vector2(250, 80), 30), 150, Laya.Handler.create(this, () => {
                    ViewManager.Instance.ShowView(ViewType.MainView);
                }))
            }
        }), Laya.Handler.create(this, this.OnSignError), true);
    }

    /**普通领取*/
    OnNormalSign() {
        // if (this.gou.visible) {
        //     this.OnDoubleSign();
        // } else {
        //     this.OnSign();
        // }
        this.OnDoubleSign();
    }

   /**看视频双倍领取方法 */
    OnDoubleSign() {
        GuangGao.KaiShiPin((flag) => {
            if (flag) {
                let param = [{ "video": "1" }];
                ServerManager.Instance.SendHttp(BlastToMerge.QianDao, param, Laya.Handler.create(this, function (e) {
                    var reward = e["rewards"];
                    SignView.Instance.SignDatas = SignView.Instance.GainSignDatas();
                    SignView.Instance.SignBtn.visible = false;
                    SignView.Instance.RefreshDatas();
                    GameTools.TweenText(new faafssfafsa("道具 + 1", new Laya.Vector2(Laya.stage.width / 2, Laya.stage.height * 0.35), new Laya.Vector2(250, 80), 30), 150, Laya.Handler.create(this, () => {
                        ViewManager.Instance.ShowView(ViewType.MainView);
                    }))
                }), Laya.Handler.create(this, this.OnSignError), true);
            }
        })
    }
    OnSignError(e) {
        console.log(e);
    }
   /**从本地拿到签到数据 */
    GainSignDatas(): SignItem[] {
        var signdatas = [];
        var config = AccountManager.Instance.curplayerData.GainCollocation("sign");
        var signday = Number(AccountManager.Instance.curplayerData.GainQinDao("sign_days"));
        var once = signday < 7;
        var indes = once ? 0 : 7;
       // if (!once) this.wukong.visible = false; this.dawing.visible = false;
        for (var i = indes; i < indes + 7; i++) {
            this.rewardsdic[config[i].id] = config[i].rewards;
        }
        for (var key in this.rewardsdic) {
            var rewardid = this.rewardsdic[key];
            var reward = AccountManager.Instance.curplayerData.GainJiangLi(rewardid);
            var item = new SignItem();
            item.desc = reward.group_name;
            item.icon = reward.icon;
            item.signday = Number(reward.id);
            item.rewardids = reward.reward_item;
            item.rewardnum = reward.reward_num;
            item.weight = reward.reward_weight;
            var num = Number(key);
            if (signday > num - 1) item.mold = QianDaoMode.end;
            if (signday < num - 1) item.mold = QianDaoMode.none;
            if (signday == num - 1 && SignView.CanSign) {
                item.mold = QianDaoMode.start;
            }
            signdatas.push(item);
        }
        return signdatas;
    }

   /** 是否能签到 */
    static get CanSign(): boolean {
        let SSS = new Date(AccountManager.Instance.ServerTime * 1000);
        SSS.setHours(0, 0, 0, 0);
        var AAA = SSS.valueOf();
        var SignTime = Number(AccountManager.Instance.curplayerData.GainQinDao("sign_time"));
        var DDD = SignTime * 1000;
        var TTT = DDD < AAA ? true : false;
        return TTT;
    }
}