import Command from '../command';
import { COMMAND_TYPE } from '../constants';

export default class SetScaleCommand extends Command {
  constructor(service, uuid, newScale, oldScale) {
    super(service);
    this.type = COMMAND_TYPE.SET_SCALE;
    this.name = '修改缩放';
    this.updatable = true;
    this.object = service.getObjectByUuid(uuid);
    this.oldScale =
      oldScale && oldScale.length === 3
        ? new service.THREE.Vector3(oldScale[0], oldScale[1], oldScale[2])
        : this.object
        ? this.object.scale.clone()
        : new service.THREE.Vector3(1, 1, 1);
    this.newScale = new service.THREE.Vector3(
      Number(newScale[0]) || 1,
      Number(newScale[1]) || 1,
      Number(newScale[2]) || 1
    );
  }

  execute() {
    if (!this.object) return;
    this.object.scale.copy(this.newScale);
    this.object.updateMatrixWorld(true);
  }

  undo() {
    if (!this.object) return;
    this.object.scale.copy(this.oldScale);
    this.object.updateMatrixWorld(true);
  }

  canUpdate(command, deltaTime, mergeWindow) {
    return (
      command &&
      command.type === this.type &&
      this.object &&
      command.object &&
      command.object.uuid === this.object.uuid &&
      deltaTime <= mergeWindow
    );
  }

  update(command) {
    this.newScale.copy(command.newScale);
  }

  getChangedKeys() {
    return ['scale'];
  }
}
