
import { DispatcherManager, EventType } from "../Game/DispatcherManager";
import { ui } from "../ui/layaMaxUI";
import  {ViewManager, ViewType } from "../Net/ViewManager";
import { PlayerManager } from "../Game/PlayerManager";
import { NetInfoManager } from "../Game/NetInfoManager";
import UserModel, { UserAttr } from "../Net/UserModel";
import NetManager, { PROTOCOLS } from "../Net/NetManager";
import {GuangGao} from "../Net/GuangGao";
import { WXSdk } from "../Net/WXSdk";
import { AppSetting } from "../Game/AssetManager";

export default class IntensifyView extends ui.IntensifyViewUI implements SC {


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

    }
    OnShow(data?: any) {
        this.RefreshData();
        this.RegButton();
        this.RefreshUI();
        

        GuangGao.CheatBanner(null);
    }
    private cost1: number;
    private cost2: number;
    private tempPowerLevel: number;
    private tempRateLevel: number;
    private freecoinValue: number;
    RefreshUI() {
        this.InitUi();
        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();
    }
    
    private iscoinShowfreecoinbtn = false;
    RefreshData() {
        this.tempPowerLevel = PlayerManager.Instance.powerLevel;
        this.tempRateLevel = PlayerManager.Instance.rateLevel;
        this.RefreshData2();
    }
    RefreshData2() {
        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;
        }
    }
    

    RegButton() {
        this.backBtn.CZ = () => {
            ViewManager.Instance.CloseView(ViewType.IntensifyView);
        };
        this.atkupBtn.clickHandler = new Laya.Handler(this, this.AtkUp);
        this.rateupBtn.clickHandler = new Laya.Handler(this, this.RateUp);
        this.getcoinBtn.clickHandler = new Laya.Handler(this, this.GetCoin);
    }
    AtkUp() {
        if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.cost1) {
            ViewManager.Instance.ShowTips("金币不足!", 1);
            return;
        }
        PlayerManager.Instance.PowerUp(this.tempPowerLevel + 1, "money", this.cost1, (iscom) => {
            if (iscom) {
                WXSdk.AldEvent("升级界面花钱升级攻击力");
                this.tempPowerLevel++;
                this.InitUi();
                DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
            }
        })
    }
    RateUp() {
        if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.cost2) {
            ViewManager.Instance.ShowTips("金币不足!", 1);
            return;
        }
        PlayerManager.Instance.SpeedUp(this.tempRateLevel + 1, "money", this.cost2, (iscom) => {
            if (iscom) {
                WXSdk.AldEvent("升级界面花钱升级速度");
                this.tempRateLevel++;
                this.InitUi();
                DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
            }
        })
    }
    GetCoin() {
        GuangGao.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) => {

                    });
            }
        });
    }

    RefreshCoinShow() {
        //this.coinValue.text = AppSetting.freecoinValue.toString();
        if (this.iscoinShowfreecoinbtn) {
            this.getcoinBtn.gray = false;
            this.getcoinBtn.mouseEnabled = true;
        } else {
            this.getcoinBtn.gray = true;
            this.getcoinBtn.mouseEnabled = false;
        }
    }

    InitUi() {
        this.atkLv.text = "LV" + this.tempPowerLevel;
        let al = NetInfoManager.Instance._atk.length
        let t = this.tempPowerLevel > al ? al : this.tempPowerLevel;
        this.cost1 = NetInfoManager.Instance.GetAtk(t)["cost"];
        this.atkValue.text = this.cost1.toString();

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

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

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

    }


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

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