
import { DispatcherManager, EventType } from "../GameLogic/DispatcherManager";
import { ui } from "../ui/layaMaxUI";
import ViewManager, { ViewType } from "../game_module/ViewManager";
import { PlayerManager } from "../GameLogic/PlayerManager";
import { CfgManager } from "../GameLogic/CfgManager";
import UserModel, { UserAttr } from "../game_module/UserModel";
import NetManager, { PROTOCOLS } from "../game_module/NetManager";
import BannerAndVideo1 from "../game_module/BannerAndVideo1";
import { WXSdk } from "../game_module/WXSdk";
import { AppSetting } from "../GameLogic/AssetManager";

export default class IntensifyView extends ui.IntensifyViewUI implements UI_Interface {


    constructor() {
        super();
        this.resizeHander();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.resizeHander);

    }
    OnShow(data?: any) {
        this.InitDate();
        this.InitHandler();
        this.InitUi();

        BannerAndVideo1.CheatBanner(null);
    }
    private cost1: number;
    private cost2: number;
    private tempPowerLevel: number;
    private tempRateLevel: number;
    private freecoinValue: number;
    InitUi() {
        this.SetUi();

        this.RefreshCoinShow();

        let t = this.cost1 < this.cost2 ? this.cost1 : this.cost2;
        this.freecoinValue=Math.floor(t * 1.2);
        this.coinValue.text =this.freecoinValue.toString();
    }
    SetUi() {
        this.atkLv.text = "LV" + this.tempPowerLevel;
        let al = CfgManager.Instance._atk.length
        let t = this.tempPowerLevel > al ? al : this.tempPowerLevel;
        this.cost1 = CfgManager.Instance.GetAtk(t)["cost"];
        this.atkValue.text = this.cost1.toString();

        this.rateLv.text = "LV" + this.tempRateLevel;
        let al1 = CfgManager.Instance._rate.length
        let t1 = this.tempRateLevel > al1 ? al1 : this.tempRateLevel;
        this.cost2 = CfgManager.Instance.GetRate(t1)["cost"];
        this.rateValue.text = this.cost2.toString();

        if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.cost1) {
            this.atkBtn.gray = true;
            this.atkBtn.mouseEnabled = false;
        } else {
            this.atkBtn.gray = false;
            this.atkBtn.mouseEnabled = true;
        }

        if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.cost2) {
            this.rateBtn.gray = true;
            this.rateBtn.mouseEnabled = false;
        } else {
            this.rateBtn.gray = false;
            this.rateBtn.mouseEnabled = true;
        }

    }
    // private tempAtk: number;
    // private tempRate: number;

    private iscoinShowfreecoinbtn = false;
    InitDate() {
        this.tempPowerLevel = PlayerManager.Instance.powerLevel;
        this.tempRateLevel = PlayerManager.Instance.rateLevel;
        this.RefreshData();
    }
    RefreshData() {
        let detail = UserModel.Instance.CurModel.user_video.detail;
        if (!detail) this.iscoinShowfreecoinbtn = true;
        else {
            let freet = detail.freecoin;
            if (!freet) { this.iscoinShowfreecoinbtn = true; return }
            if (freet < AppSetting.freeGetcoinTime) {
                this.iscoinShowfreecoinbtn = true; return
            }
            this.iscoinShowfreecoinbtn = false;
        }
    }
    RefreshCoinShow() {
        //this.coinValue.text = AppSetting.freecoinValue.toString();
        if (this.iscoinShowfreecoinbtn) {
            this.coinBtn.gray = false;
            this.coinBtn.mouseEnabled = true;
        } else {
            this.coinBtn.gray = true;
            this.coinBtn.mouseEnabled = false;
        }
    }

    InitHandler() {
        // this.on(Laya.Event.CLICK,this,this.Test);
        this.backBtn.CZ = () => {
            ViewManager.Instance.CloseView(ViewType.IntensifyView);
        };
        this.atkBtn.clickHandler = new Laya.Handler(this, this.AtkBtnHandler);
        this.rateBtn.clickHandler = new Laya.Handler(this, this.RateBtnHandler);
        this.coinBtn.clickHandler = new Laya.Handler(this, this.CoinBtnHandler);
    }
    AtkBtnHandler() {
        if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.cost1) {
            ViewManager.Instance.ShowTips("金币不足!", 1);
            return;
        }
        PlayerManager.Instance.GradePower(this.tempPowerLevel + 1, "money", this.cost1, (iscom) => {
            if (iscom) {
                WXSdk.AldEvent("升级界面花钱升级攻击力");
                this.tempPowerLevel++;
                this.SetUi();
                DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
            }
        })
    }
    RateBtnHandler() {
        if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.cost2) {
            ViewManager.Instance.ShowTips("金币不足!", 1);
            return;
        }
        PlayerManager.Instance.GradeRate(this.tempRateLevel + 1, "money", this.cost2, (iscom) => {
            if (iscom) {
                WXSdk.AldEvent("升级界面花钱升级速度");
                this.tempRateLevel++;
                this.SetUi();
                DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
            }
        })
    }
    CoinBtnHandler() {
        BannerAndVideo1.ShowVideo((isok) => {
            if (isok) {
                var param = [{ "t": UserModel.Instance.CurModel.ServerTime.toString() }, { "reward_item": "1000" }, { "reward_num": (this.freecoinValue).toString() }, { "channel": "video:freecoin" }];
                NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.Fissionvd, param,
                    (data: any) => {
                        WXSdk.AldEvent("升级界面看视屏获得钱");
                        DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
                        this.RefreshData();
                        this.RefreshCoinShow();
                    },
                    (data) => {

                    });
            }
        });
    }


    OnClose() {
        DispatcherManager.off(Laya.Event.RESIZE, this, this.resizeHander);
        if (this.tempPowerLevel != PlayerManager.Instance.powerLevel || this.tempRateLevel != PlayerManager.Instance.rateLevel) {
            PlayerManager.Instance.SetArr();
        }
        BannerAndVideo1.Clear();
    }

    resizeHander() {
        this.height = Laya.stage.height;
    }
}