{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "database = {}\n",
    "\n",
    "with open(\"data/database.json\", \"r\") as f:\n",
    "    database = json.load(f)\n",
    "\n",
    "db = database[\"db\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path \n",
    "\n",
    "datapath = Path(r\"E:\\Games\\SteamLibrary\\steamapps\\common\\Stationeers\\Stationpedia\\exported_textures\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "images = list(datapath.glob(\"*.png\"))\n",
    "names = [image.name  for image in images]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_candidates = {}\n",
    "\n",
    "def filter_candidates(candidates):\n",
    "    max_match_len = 0\n",
    "    max_len_matches = []\n",
    "    for can in candidates:\n",
    "        name, match, mapping = can\n",
    "        match_len = len(match)\n",
    "        if match_len > max_match_len:\n",
    "            max_match_len = 0\n",
    "            max_len_matches = [(name, match, mapping)]\n",
    "        elif match_len == max_match_len:\n",
    "            max_len_matches.append((name, match, mapping))\n",
    "    if len(max_len_matches) > 1:\n",
    "        for can in max_len_matches:\n",
    "            name, match = can\n",
    "            if f\"{match}_White\" in name:\n",
    "                return [name]\n",
    "    return [(name, mapping) for name, _, mapping in max_len_matches]\n",
    "\n",
    "for entry in db.values():\n",
    "    candidates = []\n",
    "    for name in names:\n",
    "        if entry[\"name\"] in name:\n",
    "            candidates.append((name, entry[\"name\"],  entry[\"name\"]))\n",
    "        if entry[\"name\"].removeprefix(\"Item\") in name:\n",
    "            candidates.append((name, entry[\"name\"].removeprefix(\"Item\"),  entry[\"name\"]))\n",
    "        if entry[\"name\"].removeprefix(\"Structure\") in name:\n",
    "            candidates.append((name, entry[\"name\"].removeprefix(\"Structure\"),  entry[\"name\"]))\n",
    "    image_candidates[entry[\"name\"]] = filter_candidates(candidates)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ItemAuthoringToolRocketNetwork []\n",
      "ImGuiCircuitboardAirlockControl []\n",
      "ItemBiomass []\n",
      "StructureBlocker []\n",
      "CartridgePlantAnalyser []\n",
      "StructureElevatorLevelIndustrial []\n",
      "ItemPlantEndothermic_Creative []\n",
      "Flag_ODA_10m []\n",
      "Flag_ODA_4m []\n",
      "Flag_ODA_6m []\n",
      "Flag_ODA_8m []\n",
      "ItemHorticultureBelt []\n",
      "StructureHydroponicsTrayData []\n",
      "ItemKitLiquidRegulator []\n",
      "ItemKitPortablesConnector []\n",
      "Landingpad_GasConnectorInwardPiece []\n",
      "Landingpad_LiquidConnectorInwardPiece []\n",
      "StructurePlinth []\n",
      "DynamicGasTankAdvancedOxygen []\n",
      "Rover_MkI_build_states []\n",
      "ItemPlantThermogenic_Creative []\n",
      "StructureWaterBottleFillerPoweredBottom []\n",
      "StructureWaterBottleFillerPowered []\n"
     ]
    }
   ],
   "source": [
    "to_copy = []\n",
    "for name, candidates in image_candidates.items():\n",
    "    if len(candidates) != 1:\n",
    "        print(name, candidates)\n",
    "    else:\n",
    "        to_copy.append((name, candidates[0][0]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|████████████████████████████████████████| 1216/1216 [100%] in 0.6s (1885.28/s) \n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "from alive_progress import alive_bar\n",
    "destpath = Path(\"img/stationpedia\")\n",
    "total_files = len(to_copy)\n",
    "\n",
    "with alive_bar(total_files) as bar:\n",
    "    for name, file in to_copy:\n",
    "        source = datapath / file\n",
    "        dest = destpath / f\"{name}.png\"\n",
    "        shutil.copyfile(source, dest)\n",
    "        bar()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
