From 9c3d550224599dd64fa534e9bf7f70f8d6f75bba Mon Sep 17 00:00:00 2001
From: Nikola Petrov <nikola@petrovv.com>
Date: Mon, 3 Feb 2025 22:04:27 +0100
Subject: [PATCH] Show user_id and gen

---
 view/inc/Vapp.hpp | 12 ++++++--
 view/src/Vapp.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++----
 view/src/main.cpp |  6 ++--
 3 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/view/inc/Vapp.hpp b/view/inc/Vapp.hpp
index 7690acb..b75cab3 100644
--- a/view/inc/Vapp.hpp
+++ b/view/inc/Vapp.hpp
@@ -1,4 +1,4 @@
-
+#include <vector>
 #include "sql.hpp"
 
 class Vapp
@@ -10,6 +10,14 @@ public:
   void deinit();
 
 private:
-  bool showDemoWindow;
+  bool showDemoWindow = false;
+  bool showSelection = false;
   sqlite3 *db;
+  sqlite3_stmt *get_gen_num;
+  std::vector<int64_t> ids;
+  int64_t gens = -1;
+  int selected_id = -1;
+  bool update_gen = false;
+  int selected_gen = -1;
+  bool enableAll = true;
 };
\ No newline at end of file
diff --git a/view/src/Vapp.cpp b/view/src/Vapp.cpp
index 2e53485..bed6620 100644
--- a/view/src/Vapp.cpp
+++ b/view/src/Vapp.cpp
@@ -1,4 +1,5 @@
 #include <cinttypes>
+#include <cmath>
 
 #include "Vapp.hpp"
 #include "values/Dna.hpp"
@@ -7,14 +8,45 @@
 #include <imgui.h>
 #include <raylib.h>
 
+const char select_user_id[] = "SELECT USER_ID FROM user_table GROUP BY USER_ID;";
+
 void Vapp::init()
 {
   sql::init();
   sql::open(DB_NAME, &db);
+
+  sqlite3_stmt *stmt;
+  sql::prepare_v2(db, select_user_id, -1, &stmt, NULL);
+
+  while (sql::step(stmt) != SQL_DONE)
+  {
+    int type = sql::column_type(stmt, 0);
+    if (type != SQL_NULL)
+    {
+      int64_t user_id = sql::column_int64(stmt, 0);
+      ids.push_back(user_id);
+    }
+  }
+  sql::prepare_v2(db, max_gen, -1, &get_gen_num, nullptr);
 }
 
 void Vapp::update()
 {
+  if (update_gen && selected_id >= 0)
+  {
+    sql::bind_int64(get_gen_num, 1, ids[selected_id]);
+    while (sql::step(get_gen_num) != SQL_DONE)
+    {
+      int type = sql::column_type(get_gen_num, 0);
+      if (type != SQL_NULL)
+      {
+        gens = sql::column_int64(get_gen_num, 0);
+      }
+    }
+    sql::reset(get_gen_num);
+
+    update_gen = false;
+  }
 }
 
 void Vapp::draw()
@@ -25,18 +57,48 @@ void Vapp::draw()
 
   if (ImGui::BeginMainMenuBar())
   {
-    if (ImGui::BeginMenu("Window"))
-    {
-      if (ImGui::MenuItem("Demo Window", nullptr, showDemoWindow))
-        showDemoWindow = !showDemoWindow;
-
-      ImGui::EndMenu();
+    ImGui::MenuItem("Demo Window", nullptr, &showDemoWindow, enableAll);
+    ImGui::MenuItem("Selection", nullptr, &showSelection, enableAll);
+    if(ImGui::MenuItem("Draw", nullptr, false, enableAll)){
+      enableAll = false;
+      showSelection = false;
     }
     ImGui::EndMainMenuBar();
   }
 
   if (showDemoWindow)
     ImGui::ShowDemoWindow(&showDemoWindow);
+
+  if (showSelection)
+  {
+    ImGui::Begin("Selection", &showSelection);
+    if (ImGui::BeginListBox("##list_id", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, -FLT_MIN)))
+    {
+      for (int n = 0; n < ids.size(); n++)
+      {
+        const bool is_selected = (selected_id == n);
+        if (ImGui::Selectable(TextFormat("%d", ids[n]), is_selected))
+        {
+          selected_id = n;
+          gens = -1;
+          update_gen = true;
+        }
+      }
+      ImGui::EndListBox();
+    }
+    ImGui::SameLine();
+    if (ImGui::BeginListBox("##list_gen", ImVec2(-FLT_MIN, -FLT_MIN)))
+    {
+      for (int n = 0; n <= gens; n++)
+      {
+        const bool is_selected = (selected_gen == n);
+        if (ImGui::Selectable(TextFormat("%d", n), is_selected))
+          selected_gen = n;
+      }
+      ImGui::EndListBox();
+    }
+    ImGui::End();
+  }
 }
 
 void Vapp::deinit()
diff --git a/view/src/main.cpp b/view/src/main.cpp
index f237b22..162e602 100644
--- a/view/src/main.cpp
+++ b/view/src/main.cpp
@@ -5,11 +5,11 @@
 
 int main(int argc, char *argv[])
 {
-  int screenWidth = 800;
-  int screenHeight = 800;
+  int screenWidth = 1000;
+  int screenHeight = 1000;
 
   Vapp app;
-
+  SetConfigFlags(FLAG_WINDOW_RESIZABLE);
   InitWindow(screenWidth, screenHeight, "VIEW");
   SetTargetFPS(60);
   rlImGuiSetup(true);