import { ui } from "../ui/layaMaxUI";
import { DispatcherManager, EventType } from "../Game/DispatcherManager";
import { Scene3DManager } from "../Game/Scene3DManager";
import {MouseEvent} from "../Game/MouseEvent";
import {LevelController} from "../Game/LevelController";
import { PlayerManager } from "../Game/PlayerManager";
import {GameManager} from "../Game/GameManager";
import UserModel, { UserAttr } from "../Net/UserModel";
import NetManager, { PROTOCOLS } from "../Net/NetManager";
import { FollowNumber } from "../Util/MathTool";
import { WXSdk } from "../Net/WXSdk";
import  {ViewManager, ViewType } from "../Net/ViewManager";
import {MainRoleManager} from "../Manager/MainRoleManager";
import {CameraManager} from "../Manager/CameraManager";

import Image = Laya.Image;
import { SettingController } from "../Game/SettingController";
import {MateManager} from "../Manager/MateManager";
import {GuideController} from "../Manager/GuideController";
import StreetManager from "../Manager/StreetManager";
import { FYHD_Sdk } from "../Net/FYHD_Sdk";
export default class MainView extends ui.MainViewUI implements SC {
    private static instance: MainView;
    static get Instance() {
        return MainView.instance;
    }
    constructor() {
        super();
        MainView.instance = this;
        this.Resize();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.Resize);
    }

    OnShow(data?: any) {
        this.RefreshData();
        this.RegButton();
        this.RefreshUI();
        this.SetData();
        this.SetTopAd();
        if (GuideController.Instance.RequireGuide()) {
            this.mainBox.visible = false;
            //GuideController.Instance.Guide1();
        }
    }

    


    RefreshUI() {
        this.user_id.text = "user_id:" + UserModel.Instance.CurModel.m_userid;
        this.SetPlayerGrade(UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
        this.SetStage();
        this.SetStagePro(0);
        this.SetSettingBox(false);
    }
    private isTryed = false;
    RefreshData() {
        this.GetImage();
        this.coinFollow = new FollowNumber(0.1, 0, 0, 0.1);
        this.proWidth = this.pro.width;
        this.isTryed = false;
    }
    AdaptiveUI() {

    }
    moreFunc(ist){
        if(ist){
                   
        }else{
            console.log("mainui")
        }
    }
    
    RegButton() {
        this.on(Laya.Event.MOUSE_DOWN, this, this.StartGame)
        this.settingBtn.CZ = () => {
            this.SetSettingBox();
        };
        this.skinBtn.CZ = () => {
            ViewManager.Instance.AddView(ViewType.SKinShopView);
        };
        this.updateBtn.CZ = () => {
            ViewManager.Instance.AddView(ViewType.IntensifyView);
        };
        DispatcherManager.on(EventType.Event.ShowMainUi, this, this.ShowMainBox);
        DispatcherManager.on(EventType.Event.SetMonery, this, this.SetPlayerGrade);
        DispatcherManager.on(EventType.Event.SetStage, this, this.SetStage);
        DispatcherManager.on(EventType.Event.SetStagePro, this, this.SetStagePro);

        this.sound_btn.clickHandler = new Laya.Handler(this, () => {
            this.ClickSound();
        });
        this.shake_btn.clickHandler = new Laya.Handler(this, () => {
            this.ClickShake();
        });
        this.ExitBtn.clickHandler = new Laya.Handler(this,this.ClickExit);
        this.MoreGameBtn.clickHandler = new Laya.Handler(this,this.ClickExport);
        this.ExitBtn.visible = FYHD_Sdk.iswhite;
    }

    ClickExit(){
        ViewManager.Instance.AddView(ViewType.ExitView)
    }
    ClickExport(){
        ViewManager.Instance.AddView(ViewType.ExportView);
    }

    //显示关卡
    SetStage() {
        this.stageValue.text = PlayerManager.Instance.stage.toString();
    }
    proWidth: number;
    //显示关卡进度
    SetStagePro(value) {
        value = value >= 1 ? 1 : value;
        this.pro1.width = this.proWidth * value;
    }
    private coinFollow: FollowNumber;
    private tempTarGrade: number;
    private SetPlayerGrade(num) {
        this.tempTarGrade = num;
        this.coinFollow.target = num;
        Laya.timer.frameLoop(1, this, this.T2);
    }
    private T2() {
        this.coinFollow.update();
        let v = Math.ceil(this.coinFollow.follow);
        this.countValue.text = v.toString();
        if (v >= this.tempTarGrade) {
            this.countValue.text = this.tempTarGrade.toString();
            Laya.timer.clear(this, this.T2);
        }
    }
    StartGame(e: Laya.Event) {
        if (this.isTryed) {
            this.TryStartGame();
            return;
        }
        if (e.target instanceof Laya.UIComponent) {
            if (e.target.name != "mainBox")
                return;
        }
        console.log("click");

        let param = [{ "t": UserModel.Instance.CurModel.ServerTime }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.Start, param,
            (data: any) => {
                if (data["play_id"]) {
                    PlayerManager.Instance.Play_Id = data["play_id"];
                }
                WXSdk.Start(PlayerManager.Instance.stage.toString());
                if (PlayerManager.Instance.stage >= 3) {
                    ViewManager.Instance.AddView(ViewType.TrialView);
                    
                } else {
                    this.MainStartGame();
                }
            },
            (data) => {
            });
    }
    MainStartGame() {
        GameManager.GameNum++;
        this.mainBox.visible = false;
        this.sliderBox.visible = false;
        this.off(Laya.Event.MOUSE_DOWN, this, this.StartGame)
        this.gameStart.stop();
        GameManager.Instance.Start();
        MouseEvent.Instance.StartGameClick();
        MainRoleManager.Instance.Start();
        MateManager.Instance.TrialModelPlayAni();
        StreetManager.Instance.SetBlock();
        GuideController.Instance.GuideOn();
        //YouZiHuiTuiController.Instance.ClearBannerHuiTui_main();
    }
    TryStartGame() {
        GameManager.GameNum++;
        this.sliderBox.visible = false;
        this.gameStart.stop();
        this.off(Laya.Event.MOUSE_DOWN, this, this.StartGame)
        GameManager.Instance.Start();
        MouseEvent.Instance.StartGameClick();
        MainRoleManager.Instance.Start();
        MateManager.Instance.TrialModelPlayAni();
        StreetManager.Instance.SetBlock();
        
    }
    TryClick() {
        this.isTryed = true;
        this.mainBox.visible = false;
    }

    ShowMainBox() {
        this.mainBox.visible = true;
        this.sliderBox.visible = true;
        this.gameStart.play(0, true);
        this.SetPlayerGrade(UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
        this.isTryed = false;
        this.on(Laya.Event.MOUSE_DOWN, this, this.StartGame);
    }

    /**
     * 设置模块
     */
    SetSettingBox(ist = true) {
        if (!ist) {
            this.settingBox.visible = false;
            return;
        }
        this.settingBox.visible = !this.settingBox.visible;
    }
    GetImage() {
        this.sound_open = this.sound_btn.getChildByName("kai") as Image;
        this.sound_guan = this.sound_btn.getChildByName("guan") as Image;
        this.shake_open = this.shake_btn.getChildByName("kai") as Image;
        this.shake_guan = this.shake_btn.getChildByName("guan") as Image;
        this.ShowSound(SettingController.Instance.sound);
        this.ShowShake(SettingController.Instance.vibrate);
    }
    ClickSound() {
        SettingController.Instance.sound = !SettingController.Instance.sound;
        this.ShowSound(SettingController.Instance.sound);
    }
    private ShowSound(isshow) {
        if (!isshow) {
            this.sound_open.visible = false;
            this.sound_guan.visible = true;
            SettingController.Instance.SettingSound(0);
           Laya.LocalStorage.setItem("sound","0");
            console.log("关闭声音");
        } else {
            this.sound_open.visible = true;
            this.sound_guan.visible = false;
            SettingController.Instance.SettingSound(1);
             Laya.LocalStorage.setItem("sound","1");
            console.log("开启声音");
        }
    }
    ClickShake() {
        SettingController.Instance.vibrate = !SettingController.Instance.vibrate;
        this.ShowShake(SettingController.Instance.vibrate);
    }
    ShowShake(isshow) {
        if (!isshow) {
            this.shake_open.visible = false;
            this.shake_guan.visible = true;
            Laya.LocalStorage.setItem("vibrate","0");
            console.log("关闭震动");
        } else {
            this.shake_open.visible = true;
            this.shake_guan.visible = false;
            Laya.LocalStorage.setItem("vibrate","1");
            console.log("开启震动");
        }
    }
    private sound_open: Image;
    private sound_guan: Image;
    private shake_open: Image;
    private shake_guan: Image;

    OnClose() {
        this.off(Laya.Event.MOUSE_DOWN, this, this.StartGame);
        DispatcherManager.off(Laya.Event.RESIZE, this, this.Resize);
        DispatcherManager.off(EventType.Event.ShowMainUi, this, this.ShowMainBox);
        DispatcherManager.off(EventType.Event.SetMonery, this, this.SetPlayerGrade);
        DispatcherManager.off(EventType.Event.SetStage, this, this.SetStage);
        DispatcherManager.off(EventType.Event.SetStagePro, this, this.SetStagePro);
        MainView.instance = null;
        Laya.timer.clearAll(this);
    }

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


    private SetData() {
        FYHD_Sdk.SetHuTuiList(this.BottomList, 2, "");
        this.GunGun();
    }
    GunGun() {
        var maxBtm;
        try {
            this.BottomList.scrollBar.value = 0;
            maxBtm = this.BottomList.scrollBar.max;
        } catch (error) {
            console.error(error);
            return;
        }
        var tempBtm = 1;
        Laya.timer.frameLoop(1, this, () => {
            this.BottomList.scrollBar.value += tempBtm;
            if (this.BottomList.scrollBar.value >= maxBtm || this.BottomList.scrollBar.value <= 0) {
                tempBtm = -tempBtm;
            }
        });
    }

    topAdData: any;
    topLeftAdIndex: any;
    topRightAdIndex: any;
    //左右抖动互推
    SetTopAd() {
        if(Laya.Browser.onPC)return;
        this.topAdData = FYHD_Sdk.pushData || [];
        this.LeftPushIcon.visible = this.RightPushIcon.visible = this.topAdData.length >= 2;
        if (this.topAdData.length < 2) return;
        this.topLeftAdIndex = { index: 0 };
        this.topRightAdIndex = { index: 1 };
        this._setTopAd(this.LeftPushIcon, this.topLeftAdIndex);
        this._setTopAd(this.RightPushIcon, this.topRightAdIndex);
    }
    private _setTopAd(topAd: Laya.Button, adIndex: any) {
        let _icon = topAd.getChildByName("icon") as Laya.Image;
        let _name = topAd.getChildByName("name") as Laya.Text;
        topAd.clickHandler =new Laya.Handler(this, () => {
            FYHD_Sdk.ShowHuTuiJump(this.topAdData[adIndex.index]);
            // SDKZS.navigate2Mini(this.topAdData[adIndex.index])
        });
        this._setIcon(_icon,_name, adIndex);
        Laya.timer.loop(3000, this, () => {
            this._setIcon(_icon,_name, adIndex);
            this.lefticon.play(null, false);
            this.righticon.play(null, false);
        });
    }
    private _setIcon(_icon: Laya.Image, _name:Laya.Text, adIndex: any) {
        adIndex.index = (adIndex.index + 2) % this.topAdData.length;
        _icon.skin = this.topAdData[adIndex.index].icon;
        _name.text = this.topAdData[adIndex.index].togame_name;
    }
}