"use strict";

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  var desc = Object.getOwnPropertyDescriptor(m, k);
  if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
    desc = {
      enumerable: true,
      get: function () {
        return m[k];
      }
    };
  }
  Object.defineProperty(o, k2, desc);
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});
var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});
var __importStar = void 0 && (void 0).__importStar || function () {
  var ownKeys = function (o) {
    ownKeys = Object.getOwnPropertyNames || function (o) {
      var ar = [];
      for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
      return ar;
    };
    return ownKeys(o);
  };
  return function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
    __setModuleDefault(result, mod);
    return result;
  };
}();
var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RelocationNode = void 0;
const tree_model_1 = __importDefault(require("tree-model"));
const domain_1 = require("../../domain");
const agv_constant_1 = require("../../entity/agv.constant");
const translation_util_1 = __importStar(require("../../utils/translation.util"));
const scope_setting_1 = require("../../scope-setting");
const webscope_service_1 = require("../../webscope-service");
const TranslationUtil = translation_util_1.default;
class RelocationNode {
  setX(v) {
    this.x = v;
    return this;
  }
  getX() {
    return this.x;
  }
  setY(v) {
    this.y = v;
    return this;
  }
  getY() {
    return this.y;
  }
  setYaw(v) {
    this.yaw = v;
    return this;
  }
  getYaw() {
    return this.yaw;
  }
  setStationName(name) {
    this.stationName = name;
    return this;
  }
  getStationName() {
    return this.stationName;
  }
  setDataModel(dataModel) {
    this.dataModel = dataModel;
    const x = dataModel.get(this.RelocationXKey, null);
    if (this.isNumber(x)) {
      this.x = x;
    }
    const y = dataModel.get(this.RelocationYKey, null);
    if (this.isNumber(y)) {
      this.y = y;
    }
    const yaw = dataModel.get(this.RelocationYawKey, null);
    if (this.isNumber(yaw)) {
      this.yaw = yaw;
    }
    const stationName = dataModel.get(this.RelocationStationNameKey, null);
    if (this.isString(stationName)) {
      this.stationName = stationName;
    }
  }
  getDataModel() {
    this.dataModel.set(this.RelocationXKey, this.x, domain_1.XmlLabelType.FLOAT64);
    this.dataModel.set(this.RelocationYKey, this.y, domain_1.XmlLabelType.FLOAT64);
    this.dataModel.set(this.RelocationYawKey, this.yaw, domain_1.XmlLabelType.FLOAT64);
    this.dataModel.set(this.RelocationStationNameKey, this.stationName, domain_1.XmlLabelType.STRING);
    return this.dataModel;
  }
  isNumber(value) {
    return Number.isFinite(value);
  }
  isString(value) {
    return typeof value === 'string';
  }
  getValueFactory() {
    var _a, _b;
    return (_b = (_a = this.programModel).getValueFactory) === null || _b === void 0 ? void 0 : _b.call(_a);
  }
  setId(id) {
    this.id = id;
  }
  update(config) {
    let updated = false;
    for (const key in config) {
      const raw = config[key];
      const value = raw && typeof raw === 'object' && 'value' in raw ? raw.value : raw;
      if (key === this.RelocationXKey) {
        if (this.isNumber(Number(value)) && Number(value) !== this.getX()) {
          this.setX(Number(value));
          updated = true;
        }
      } else if (key === this.RelocationYKey) {
        if (this.isNumber(Number(value)) && Number(value) !== this.getY()) {
          this.setY(Number(value));
          updated = true;
        }
      } else if (key === this.RelocationYawKey) {
        if (this.isNumber(Number(value)) && Number(value) !== this.getYaw()) {
          this.setYaw(Number(value));
          updated = true;
        }
      } else if (key === this.RelocationStationNameKey) {
        if (this.isString(value) && value !== this.getStationName()) {
          this.setStationName(value);
          updated = true;
        } else if (value === null && this.getStationName() !== '') {
          this.setStationName('');
          updated = true;
        }
      }
    }
    return updated;
  }
  dropNode() {}
  toJSON() {
    const result = {};
    const model = this.getDataModel();
    if (!model || typeof model.getKeys !== 'function') {
      return result;
    }
    model.getKeys().forEach(key => {
      result[key] = {
        value: model.get(key, null),
        hasDefined: !!model.get(key, null)
      };
    });
    return result;
  }
  cloneNode(node) {
    const treeModelInstance = new tree_model_1.default();
    const parseTree = treeModelInstance.parse({});
    const {
      children,
      programNode
    } = node;
    if (programNode) {
      const nodeData = programNode.getDataModel();
      this.setDataModel(nodeData);
    }
    const currentNode = this.programModel.getProgramTree().addChild(parseTree, node.nodeId, this, 0);
    currentNode.model.disabled = node.disabled;
    currentNode.model.isBreakpoint = node.isBreakpoint;
    currentNode.model.suppressedComment = node.suppressedComment;
    return currentNode;
  }
  constructor(programModel) {
    this.RelocationXKey = 'x';
    this.RelocationYKey = 'y';
    this.RelocationYawKey = 'yaw';
    this.RelocationStationNameKey = 'station_name';
    this.id = '';
    this.x = 0;
    this.y = 0;
    this.yaw = 0;
    this.stationName = '';
    this.programModel = programModel;
    this.node = this;
  }
  generateScript(scriptWriter) {
    var _a, _b, _c, _d;
    const tree_node = (_b = (_a = this.programModel) === null || _a === void 0 ? void 0 : _a.getProgramTree().getTreeNodeById(this.id)) === null || _b === void 0 ? void 0 : _b.model;
    if (!tree_node) {
      return;
    }
    const row = tree_node.lineNumber;
    const valueFactory = (_d = (_c = (0, webscope_service_1.getWebscopeService)()) === null || _c === void 0 ? void 0 : _c.getValueFactory) === null || _d === void 0 ? void 0 : _d.call(_c);
    if (!valueFactory) {
      return;
    }
    const headerFunName = `getHeader_${row}()`;
    const priorityFunName = `setPriority_${row}()`;
    scriptWriter.appendLine(`function ${headerFunName}`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`local map_header = agvc.AgvcInterface.getCurrentMapHeader()`);
    scriptWriter.appendLine(`local header = {`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`id = tostring(socket.gettime() * 1000),`);
    scriptWriter.appendLine(`name = map_header.name,`);
    scriptWriter.appendLine(`map_id = map_header.map_id,`);
    scriptWriter.appendLine(`stamp =  socket.gettime() * 1000`);
    scriptWriter.decreaseIndent();
    scriptWriter.appendLine(`}`);
    scriptWriter.appendLine(`return header`);
    scriptWriter.end();
    scriptWriter.lineFeed();
    scriptWriter.appendLine(`function ${priorityFunName}`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`local priorityParam = { "web", "" }`);
    scriptWriter.appendLine(`local res = agvc.AgvcInterface.setPriority(priorityParam)`);
    const releasePriorityExpression = valueFactory.createExpression();
    releasePriorityExpression.append(`not res or res < 0`);
    scriptWriter.ifCondition(releasePriorityExpression);
    scriptWriter.appendLine(`local release = agvc.AgvcInterface.releasePriority()`);
    scriptWriter.appendLine(`local priority = agvc.AgvcInterface.setPriority(priorityParam)`);
    scriptWriter.appendLine(`textmsg("set priority result:" ..tostring(priority))`);
    scriptWriter.appendLine(`return priority`);
    scriptWriter.end();
    scriptWriter.appendLine(`textmsg("set priority result:" ..tostring(res))`);
    scriptWriter.appendLine(`return res`);
    scriptWriter.end();
    scriptWriter.lineFeed();
    const x = this.getX();
    const y = this.getY();
    const yaw = this.getYaw();
    scriptWriter.appendLine(`local pose = {`);
    scriptWriter.increaseIndent();
    scriptWriter.appendLine(`x = ${x},`);
    scriptWriter.appendLine(`y = ${y},`);
    scriptWriter.appendLine(`yaw = ${yaw},`);
    scriptWriter.decreaseIndent();
    scriptWriter.appendLine(`}`);
    scriptWriter.lineFeed();
    scriptWriter.appendLine(`${priorityFunName}`);
    scriptWriter.appendLine(`local header = ${headerFunName}`);
    scriptWriter.appendLine(`local relocation_result = agvc.AgvcInterface.relocation(pose, header)`);
    scriptWriter.appendLine(`while relocation_result == 10100201 do`);
    scriptWriter.appendLine(`  ${priorityFunName}`);
    scriptWriter.appendLine(`  relocation_result = agvc.AgvcInterface.relocation(pose, header)`);
    scriptWriter.appendLine(`  textmsg("relocation retry result: " .. tostring(relocation_result))`);
    scriptWriter.appendLine(`  sleep(0.3)`);
    scriptWriter.appendLine(`end`);
    scriptWriter.appendLine(`textmsg("relocation call result: " .. tostring(relocation_result))`);
    scriptWriter.lineFeed();
  }
  hasDefined() {
    return true;
  }
  getTitle() {
    let title;
    const language = (0, scope_setting_1.getCurrentLanguage)();
    title = translation_util_1.default.toAimLanguage(language, 'Relocalization', translation_util_1.TranslationNamespace.AGV);
    return title;
  }
  getIcon(hasDefined) {
    return hasDefined ? `icon${agv_constant_1.PluginsProgramNodeId.AUBO_RELOCATION}Complete` : `icon${agv_constant_1.PluginsProgramNodeId.AUBO_RELOCATION}`;
  }
}
exports.RelocationNode = RelocationNode;
//# sourceMappingURL=relocation-node.js.map