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

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

    OnShow(data?: any) {
        this.InitDate();
        this.InitHandler();
        this.InitUi();
        if (GuideController.Instance.IsGuide()) {
            this.mainBox.visible = false;
            //GuideController.Instance.Guide1();
        }
    }

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


    InitUi() {
        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;
    InitDate() {
        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")
        }
    }
    
    InitHandler() {
        this.on(Laya.Event.MOUSE_DOWN, this, this.StartGame)
        this.settingBtn.CZ = () => {
            //ViewManager.Instance.AddView(ViewType.SettingUi);
            this.SetSettingBox();
            //this.ClearHuiTui();
        };
        this.moreGameBtn.clickHandler = new Laya.Handler(this, () => {
            
            //this.CreateFull();
        });
        this.coutiBtn.clickHandler = new Laya.Handler(this, () => {
            
           // YouZiHuiTuiController.Instance.ShowBannerHuiTui_main();
            //this.CreateChouTi();
        });
        this.skinBtn.CZ = () => {
            ViewManager.Instance.AddView(ViewType.SKinShopView);
            
        };
        this.updateBtn.CZ = () => {
            ViewManager.Instance.AddView(ViewType.IntensifyView);
            
            //YouZiHuiTuiController.Instance.CreateMoreGame();
        };
        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.SoundSetting();
        });
        this.shake_btn.clickHandler = new Laya.Handler(this, () => {
            this.ShakeSetting();
        });
    }

    //显示关卡
    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() {
        this.mainBox.visible = false;
        this.sliderBox.visible = false;
        this.off(Laya.Event.MOUSE_DOWN, this, this.StartGame)
        this.gameStart.stop();
        GameManager.Instance.GameStart();
        MouseEvent.Instance.StartGameClick();
        MainRoleManager.Instance.StartGame();
        MateManager.Instance.TryOutFriendPlayAni();
        StreetManager.Instance.SetBlock();
        GuideController.Instance.StartGuide();
        
        //YouZiHuiTuiController.Instance.ClearBannerHuiTui_main();
    }
    TryStartGame() {
        this.sliderBox.visible = false;
        this.gameStart.stop();
        this.off(Laya.Event.MOUSE_DOWN, this, this.StartGame)
        GameManager.Instance.GameStart();
        MouseEvent.Instance.StartGameClick();
        MainRoleManager.Instance.StartGame();
        MateManager.Instance.TryOutFriendPlayAni();
        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.SoundShow(SettingController.Instance.sound);
        this.ShakeShow(SettingController.Instance.vibrate);
    }
    SoundSetting() {
        SettingController.Instance.sound = !SettingController.Instance.sound;
        this.SoundShow(SettingController.Instance.sound);
    }
    private SoundShow(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("开启声音");
        }
    }
    ShakeSetting() {
        SettingController.Instance.vibrate = !SettingController.Instance.vibrate;
        this.ShakeShow(SettingController.Instance.vibrate);
    }
    ShakeShow(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.resizeHander);
        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);
    }
}