
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 { ThingArr } from "../Manager/StreetManager";
import UserModel, { UserAttr } from "../game_module/UserModel";
import GameManager from "../GameLogic/GameManager";
import BannerAndVideo1 from "../game_module/BannerAndVideo1";
import { WXSdk } from "../game_module/WXSdk";
import { AppSetting } from "../GameLogic/AssetManager";

export default class AttributeUpView extends ui.AttributeUpViewUI 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 iscanShowVideoBtn = true;
    private isatkvideo = false;
    private isfirstatkvideo = true;
    private isfirstratevideo = true;
    private isratevideo = false;

    private atkShow = false;
    InitUi() {
        this.SetUi();
    }
    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) {
            if (this.iscanShowVideoBtn && this.isfirstatkvideo) {
                this.isatkvideo = true;
                this._atk.visible = false;
                this._atkVideo.visible = true;
                this.atkBtn.mouseEnabled = true;
                this.atkBtn.gray = false;
                this.click1.visible = true;
                this.atkShow = true;
            } else {
                this.atkBtn.mouseEnabled = false;
                this.atkBtn.gray = true;
                this.isatkvideo = false;
                this.click1.visible = false;
                this.atkShow = false;
            }
            //this.atkBtn.mouseEnabled = false;
        } else {
            this._atk.visible = true;
            this._atkVideo.visible = false;
            this.atkBtn.gray = false;
            this.atkBtn.mouseEnabled = true;
            this.isatkvideo = false;
            this.click1.visible = true;
            this.atkShow = true;
        }
        if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.cost2) {
            if (this.iscanShowVideoBtn && this.isfirstratevideo) {
                this.isratevideo = true;
                this._rate.visible = false;
                this._rateVideo.visible = true;
                this.rateBtn.mouseEnabled = true;
                this.rateBtn.gray = false;
                if (this.atkShow) this.click2.visible = false;
                else this.click2.visible = true;


            } else {
                this.rateBtn.mouseEnabled = false;
                this.rateBtn.gray = true;
                this.isratevideo = false;

                this.click2.visible = false;

            }
        } else {
            this._rate.visible = true;
            this._rateVideo.visible = false;
            this.rateBtn.gray = false;
            this.rateBtn.mouseEnabled = true;
            this.isratevideo = false;
            if (this.atkShow) this.click2.visible = false;
            else this.click2.visible = true;

        }
    }
    // private tempAtk: number;
    // private tempRate: number;
    InitDate() {
        this.tempPowerLevel = PlayerManager.Instance.powerLevel;
        this.tempRateLevel = PlayerManager.Instance.rateLevel;
        let detail = UserModel.Instance.CurModel.user_video.detail;
        if (!detail) this.iscanShowVideoBtn = true;
        else {
            let freet = detail.free;
            if (!freet) { this.iscanShowVideoBtn = true; return }
            if (freet < AppSetting.freeUpdateVideotime) {
                this.iscanShowVideoBtn = true; return
            } {
                this.iscanShowVideoBtn = false;
            }
        }
    }
    InitHandler() {
        // this.on(Laya.Event.CLICK,this,this.Test);
        this.backBtn.CZ = () => {
            this.BackClickHandler();
        };
        this.atkBtn.clickHandler = new Laya.Handler(this, this.AtkBtnHandler);
        this.rateBtn.clickHandler = new Laya.Handler(this, this.RateBtnHandler);
    }
    AtkBtnHandler() {
        if (this.isatkvideo) {
            BannerAndVideo1.ShowVideo((iscom) => {
                if (iscom) {
                    PlayerManager.Instance.GradePower(this.tempPowerLevel + 1, "video:free", this.cost1, (iscom) => {
                        if (iscom) {
                            WXSdk.AldEvent("失败界面看视屏升级攻击力");
                            this.tempPowerLevel++;
                            this.isfirstatkvideo = false;
                            this.SetUi();
                            DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
                        }
                    });
                }
            })
        } else {
            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 (this.isratevideo) {
            BannerAndVideo1.ShowVideo((iscom) => {
                if (iscom) {
                    PlayerManager.Instance.GradeRate(this.tempRateLevel + 1, "video:free", this.cost2, (iscom) => {
                        if (iscom) {
                            WXSdk.AldEvent("失败界面看视屏升级速度");
                            this.tempRateLevel++;
                            this.isfirstratevideo = false;
                            this.SetUi();
                        }
                    })
                }
            })
        } else {
            PlayerManager.Instance.GradeRate(this.tempRateLevel + 1, "money", this.cost2, (iscom) => {
                if (iscom) {
                    WXSdk.AldEvent("失败界面花钱升级速度");
                    this.tempRateLevel++;
                    this.SetUi();
                }
            })
        }


    }
    BackClickHandler() {
        ViewManager.Instance.CloseView(ViewType.AttributeUpView);
        GameManager.Instance.ReStart();
    }


    OnClose() {
        DispatcherManager.off(Laya.Event.RESIZE, this, this.resizeHander);
        if (this.tempPowerLevel != PlayerManager.Instance.powerLevel || this.tempRateLevel != PlayerManager.Instance.rateLevel) {
            PlayerManager.Instance.SetArr();
            DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
        }
        BannerAndVideo1.Clear();
    }

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