# Copyright (C) 2023  The CivRealm project
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

f = open("packhand_gen.js", "r")
ft = open("packhand_gen.py", "w")

ft.write("\r\n".join([
    "# Generated by generate_js_hand.py",
    "from packhand import *",
    "def client_handle_packet(p_list):",
    "    if p_list == None:",
    "        return",
    "    try:",
    "        for p in p_list:",
    "            if p == None:",
    "                continue",
    "            packet_type = p['pid']\r\n"]))

start = -1
for line in f:
    if " case " in line:
        cur_number = int(line[:-2].split("case")[1])
        if start == -1:
            ft.write(" "*12+"if packet_type == %i:" % cur_number + "\r\n")
            start = 0
        else:
            ft.write(" "*12+"elif packet_type == %i:" % cur_number + "\r\n")
    if start == 0 and "handle_" in line:
        ft.write(" "*16+line.lstrip().split("(")[0] + "(p)" + "\r\n")
        print(line)

ft.write("\r\n".join([
    "        if len(p_list)> 0:",
    "            pass",
    "            #if (debug_active) clinet_debug_collect()",
    "            #if (renderer == RENDERER_2DCANVAS) update_map_canvas_check()",
    "    except Exception:",
    "        print(Exception)\r\n"]))
