!7

concept ObjectType > Named {
  S conceptClassName;
  
  toString { ret "object type " + quote(name); }
}

concept DynamicThing > Named {
  ObjectType type;
  
  toString { ret (type == null ? "?" : type.name) + " " + quote(name); }
}

cmodule DynamicClassesMultiCRUD > DynVoiceMultiCRUD {
  switchable S caseID;
  transient S defaultCmdsSnippetID = #1027616;
  
  L<Table> makeTables() {
    ret itemPlusList(new Table("Object Types", "object type", ObjectType), 
      map(list(cc, ObjectType), type ->
        new Table(plural(type.name), type.name, DynamicThing, litparams(+type))));
  }
    
  transient NameBasedVoiceCRUD<ObjectType> typesCRUD;
  
  void startDB { cc = dm_handleCaseIDField(); }

  start {
    typesCRUD = voiceCRUDs.get(idx(ObjectType));
    
    // Update voice CRUDs when there is a new object type
    onConceptsChange(cc, rWatcher_noRunFirstTime(
      () -> objectTypes(), names -> objectTypesChanged(names)));
  }
  
  void objectTypesChanged(LS names) enter {
    print("Updating voice CRUDs");
    remakeVoiceCRUDs();
    vmBus_send objectTypesChanged(module(), names);
  }
  
  void startSwingCRUDs {
    swingCRUDs = ll(
      pair("Object Types", CRUD(cc, ObjectType)),
      pair("Objects", CRUD(cc, DynamicThing)));
  }
  
  void startFrontEnd enter {
    S frontEnd = dm_showModuleWithParams("#1027602/ChatBotFrontend", backendModuleID := dm_moduleID());
    if (eq(dm_call(frontEnd, 'conceptCount), 0))
      dm_call(frontEnd, 'importCmdsFromSnippet, defaultCmdsSnippetID);
  }
  
  afterVisualize {
    addComponent(firstSwingCRUDButtons(), jbutton("Start front-end", rThread startFrontEnd));
  }
  
  @Override S moreActions(S s) null {
    if "stats"
      ret "You have stored " + nObjects(countConcepts(cc, ObjectType))
        + " of " + nTypes(countConcepts(cc, DynamicThing));
  }
  
  // API
  
  LS objectTypes() {
    ret itemPlus("object type", collect name(list(cc, ObjectType)));
  }
}