import {_decorator, Button, Component, EditBox, instantiate, Label, Node, Prefab, RichText, ScrollView, Slider, Toggle, ToggleContainerComponent, UITransform} from 'cc';

import {GetTMGInstance, TMGContext, TMGDelegate} from '../GMESDK/Scripts/TMGContext';

const {ccclass, property} = _decorator;

@ccclass('UpdatePostion')
export class UpdatePostion extends Component {
  @property(cc.Node) currentPositionSceneNode: cc.Node = null;
  @property(cc.Node) mainNode: cc.Node = null;
  @property(EditBox) public UserIDEditBox: EditBox|null = null;
  @property(EditBox) public positionXEditBox: EditBox|null = null;
  @property(EditBox) public positionYEditBox: EditBox|null = null;
  @property(EditBox) public positionZEditBox: EditBox|null = null;
  @property(Slider) public positionXSlider: Slider|null = null;
  @property(Slider) public positionYSlider: Slider|null = null;
  @property(Slider) public positionZSlider: Slider|null = null;
  @property(Button) public backButton: Button|null = null;
  private tmgContext: TMGContext|null;
  public static g_mySelfuserID: string = '';


  start() {
    this.tmgContext = GetTMGInstance();
    this.UserIDEditBox.string = UpdatePostion.g_mySelfuserID;
    if (this.positionXSlider) {
      this.positionXSlider.progress = 0.5;
      this.positionXSlider.node.on('slide', this.onPositionXChanged, this);
    }
    if (this.positionYSlider) {
      this.positionYSlider.progress = 0.5;
      this.positionYSlider.node.on('slide', this.onPositionYChanged, this);
    }
    if (this.positionZSlider) {
      this.positionZSlider.progress = 0.5;
      this.positionZSlider.node.on('slide', this.onPositionZChanged, this);
    }
    if (this.backButton) {
      this.backButton.node.on(Button.EventType.CLICK, this.onBackButtonClicked, this);
    }
    this.positionXEditBox.string = '0.0';
    this.positionYEditBox.string = '0.0';
    this.positionZEditBox.string = '0.0';
  }

  update(deltaTime: number) {}

  private onBackButtonClicked() {
    this.currentPositionSceneNode.active = false;
    this.mainNode.active = true;
    console.log('Back button clicked');
  }

  private onPositionXChanged(slider: Slider) {
    if (this.positionXEditBox) {
      const mappedValue = slider.progress * 2 - 1;  // Map from [0, 1] to [-1, 1]
      this.positionXEditBox.string = mappedValue.toString();
    }

    let position = new Float32Array([
      Number(this.positionXEditBox.string), Number(this.positionYEditBox.string),
      Number(this.positionZEditBox.string)
    ]);

    let axisForward = new Float32Array([1.0, 0.0, 0.0]);
    let axisRight = new Float32Array([0.0, 1.0, 0.0]);
    let axisUp = new Float32Array([0.0, 0.0, 1.0]);
    if (this.UserIDEditBox.string = UpdatePostion.g_mySelfuserID) {
      this.tmgContext.GetRoom().UpdateSelfPosition(position, axisForward, axisRight, axisUp);
    } else {
      this.tmgContext.GetRoom().UpdateOtherPosition(this.UserIDEditBox.string, position);
    }
  }

  private onPositionYChanged(slider: Slider) {
    if (this.positionYEditBox) {
      const mappedValue = slider.progress * 2 - 1;  // Map from [0, 1] to [-1, 1]
      this.positionYEditBox.string = mappedValue.toString();
    }
    let position = new Float32Array([
      Number(this.positionXEditBox.string), Number(this.positionYEditBox.string),
      Number(this.positionZEditBox.string)
    ]);

    let axisForward = new Float32Array([1.0, 0.0, 0.0]);
    let axisRight = new Float32Array([0.0, 1.0, 0.0]);
    let axisUp = new Float32Array([0.0, 0.0, 1.0]);
    if (this.UserIDEditBox.string = UpdatePostion.g_mySelfuserID) {
      this.tmgContext.GetRoom().UpdateSelfPosition(position, axisForward, axisRight, axisUp);
    } else {
      this.tmgContext.GetRoom().UpdateOtherPosition(this.UserIDEditBox.string, position);
    }
  }

  private onPositionZChanged(slider: Slider) {
    if (this.positionZEditBox) {
      const mappedValue = slider.progress * 2 - 1;  // Map from [0, 1] to [-1, 1]
      this.positionZEditBox.string = mappedValue.toString();
    }

    let position = new Float32Array([
      Number(this.positionXEditBox.string), Number(this.positionYEditBox.string),
      Number(this.positionZEditBox.string)
    ]);

    let axisForward = new Float32Array([1.0, 0.0, 0.0]);
    let axisRight = new Float32Array([0.0, 1.0, 0.0]);
    let axisUp = new Float32Array([0.0, 0.0, 1.0]);
    if (this.UserIDEditBox.string = UpdatePostion.g_mySelfuserID) {
      this.tmgContext.GetRoom().UpdateSelfPosition(position, axisForward, axisRight, axisUp);
    } else {
      this.tmgContext.GetRoom().UpdateOtherPosition(this.UserIDEditBox.string, position);
    }
  }
}
