// tslint:disable-next-line:ban-types
const Messages: any = {
  C0001: '确定要删除这条数据吗？',
  C0002: '确定要把这条数据无效吗？',
  C0003: '保存成功',
  C0004: '确定要作废该药品及药品目录信息吗？',
  C0005: '确定要启用该药品信息吗？',
  C0006: '删除成功',
  C0007: '确定要进行审核操作吗？',
  E0001: '请输入{0}',
  E0002: '{0}的最大长度是{1}',
  E0003: '{0}的格式不正确',
  E0004: '请输入数字',
  E0005: '请输入{0}',
  E0006: '{0}的最小长度是{1}',
  E0007: '',
  I0001: '',
  W0001: ''
};

class MessageUtil {
  public devMode = false;

  public getMessage(messageId: string, params: string[]): string {
    const message = this.replaceMessage(messageId, params);
    if (this.devMode) {
      return `${message}(${messageId})`;
    } else {
      return message;
    }
  }

  public replaceMessage(messageId: string, params: string[]): string {
    let message: string = Messages[messageId] || messageId;
    if (params) {
      params.forEach((param: string, index: number) => {
        const reg = new RegExp(`\\{${index}\\}`, 'g');
        message = message.replace(reg, param);
      });
    }
    return message;
  }

  public convertToXML(): any {
    if (document.implementation && document.implementation.createDocument) {
      const xmlDom = document.implementation.createDocument('', '', null);
      const messageListEl = xmlDom.createElement('MessageList');
      // tslint:disable-next-line: forin
      for (const key in Messages) {
        const messageEl = xmlDom.createElement('Message');
        const idEl = xmlDom.createElement('Id');
        idEl.textContent = key;
        const textEl = xmlDom.createElement('Text');
        textEl.textContent = Messages[key];
        messageEl.appendChild(idEl);
        messageEl.appendChild(textEl);
        messageListEl.appendChild(messageEl);
      }
      return messageListEl;
    }
    return null;
  }
}

export const MESSAGE_UTIL: MessageUtil = new MessageUtil();
