
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 { ThingArr } from "../Manager/StreetManager";
import UserModel, { UserAttr } from "../Net/UserModel";
import {GameManager} from "../Game/GameManager";
import { WXSdk } from "../Net/WXSdk";
import { AppSetting } from "../Game/AssetManager";
import {GuangGao} from "../Net/GuangGao";
import { FYHD_Sdk } from "../Net/FYHD_Sdk";

export default class AttributeUpView extends ui.AttributeUpViewUI implements SC {


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

    }
    OnShow(data?: any) {
        this.RefreshData();
        this.RegButton();
        this.RefreshUI();
        this.InitPushData();
        
        // GuangGao.CheatBanner(null);
        // GuangGao.OpenBanner();
    }
    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;
    RefreshUI() {
        this.SetUi();
    }
    SetUi() {
        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) {
            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;
    RefreshData() {
        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;
            }
        }
    }
    RegButton() {
        // this.on(Laya.Event.CLICK,this,this.Test);
        this.ExitBtn.visible = FYHD_Sdk.iswhite;
        this.ExitBtn.clickHandler = new Laya.Handler(this,this.ClickExit);
        this.backBtn.CZ = () => {
            this.BackClickHandler();
        };
        this.atkBtn.clickHandler = new Laya.Handler(this, this.AtkBtnHandler);
        this.rateBtn.clickHandler = new Laya.Handler(this, this.RateBtnHandler);
    }
    ClickExit(){
        ViewManager.Instance.AddView(ViewType.ExitView);
    }
    AtkBtnHandler() {
        if (this.isatkvideo) {
            GuangGao.ShowVideo((iscom) => {
                if (iscom) {
                    PlayerManager.Instance.PowerUp(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.PowerUp(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) {
            GuangGao.ShowVideo((iscom) => {
                if (iscom) {
                    PlayerManager.Instance.SpeedUp(this.tempRateLevel + 1, "video:free", this.cost2, (iscom) => {
                        if (iscom) {
                            WXSdk.AldEvent("失败界面看视屏升级速度");
                            this.tempRateLevel++;
                            this.isfirstratevideo = false;
                            this.SetUi();
                        }
                    })
                }
            })
        } else {
            PlayerManager.Instance.SpeedUp(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.Resize);
        if (this.tempPowerLevel != PlayerManager.Instance.powerLevel || this.tempRateLevel != PlayerManager.Instance.rateLevel) {
            PlayerManager.Instance.SetInfo();
            DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
        }
        // GuangGao.Clear();
    }

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


    PushDataArray:any[] = [];
    InitPushData(){
        for (let i = 0; i < FYHD_Sdk.pushData.length; i++) {
            if(i<6){
                this.PushDataArray.push(FYHD_Sdk.pushData[i]);
            }
        }
        for (let j = 0; j < this.PushBox.numChildren; j++) {
            let box = this.PushBox.getChildAt(j) as Laya.Box;
            let icon = box.getChildByName("icon") as Laya.Image;
            let btn = box.getChildByName("btn") as Laya.Button;
            let name = box.getChildByName("name") as Laya.Text;
            icon.skin = this.PushDataArray[j].icon;
            name.text = this.PushDataArray[j].togame_name;
            btn.clickHandler = new Laya.Handler(this,this.clickAd,[this.PushDataArray[j]]);
        }
        Laya.timer.loop(3000,this,this.Doudong);
    }
    clickAd(_data) {
        FYHD_Sdk.ClickAd(FYHD_Sdk.Ad_ClickType.PushClick);
        FYHD_Sdk.ShowHuTuiJump(_data);
    }
    PlayAni(){
        this.shakeani.play(null,false);
    }

     /**抖动次数 */
     doudongcishu:number =  2;
     Doudong(){
         this.PlayAni();
         this.doudongcishu ++;
         let _arr = [];
         if(this.doudongcishu%2 == 0){
             for (let i = 0; i < FYHD_Sdk.pushData.length; i++) {
                 if(i<6){
                     _arr.push(FYHD_Sdk.pushData[i]);
                 }
             }
             for (let j = 0; j < this.PushBox.numChildren; j++) {
                 let box = this.PushBox.getChildAt(j) as Laya.Box;
                 let icon = box.getChildByName("icon") as Laya.Image;
                 let btn = box.getChildByName("btn") as Laya.Button;
                 icon.skin = _arr[j].icon;
                 btn.clickHandler = new Laya.Handler(this,this.clickAd,[_arr[j]]);
             }
         }
         else{
             let num = 0;
             for (let i = FYHD_Sdk.pushData.length-1; i > -1; i--) {
                 if(num<6){
                     _arr.push(FYHD_Sdk.pushData[i]);
                 }
                 num++;
             }
             for (let j = 0; j < this.PushBox.numChildren; j++) {
                 let box = this.PushBox.getChildAt(j) as Laya.Box;
                 let icon = box.getChildByName("icon") as Laya.Image;
                 let btn = box.getChildByName("btn") as Laya.Button;
                 icon.skin = _arr[j].icon;
                 btn.clickHandler = new Laya.Handler(this,this.clickAd,[_arr[j]]);
             }
         }
 
         
 
     }
}