import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { Hall } from "../hall/Hall";
import { EditBox } from "cc";
import { Tips } from "../../scripts/mgr/Tips";
import { EventKeyboard } from "cc";
const { ccclass, property } = _decorator;

export interface BindGameInputLayerListener {
  OnInput(gameId: number, input: string): void;
}

@ccclass("BindGameInputLayer")
export class BindGameInputLayer extends BaseUI {
  static async show(gameId: number, listener: BindGameInputLayerListener) {
    let layer = await Hall.ins.showLayer("prefab/layer/BindGameInputLayer");
    layer.getComponent(BindGameInputLayer).init(gameId, listener);
  }

  @property(EditBox)
  public edit_code: EditBox = null;

  protected onLoad(): void {
    super.onLoad();
  }

  private listener: BindGameInputLayerListener;
  private gameId: number;
  private init(gameId: number, listener: BindGameInputLayerListener) {
    this.gameId = gameId;

    this.listener = listener;

    this.FindNode("edit_code").on(
      EditBox.EventType.TEXT_CHANGED,
      this.onTextChanged,
      this
    );
  }
  private _prepareString: string = "";
  onTextChanged(event: EventKeyboard) {
    let inputString = this.edit_code.string;
    inputString = inputString.trim();
    inputString = inputString.replace("\n", "");
    this._prepareString = inputString;

    this.setText("lbl_code_string", this._prepareString);
  }

  protected simpleOnBtnClick(name: string): void {
    switch (name) {
      case "btn_bind":
        let inputString = this._prepareString;
        if (inputString.length == 0) {
          Tips.show("Please input code");
          return;
        }
        this.closePage();
        this.listener.OnInput(this.gameId, inputString);
        break;
    }
  }
}
