#!/home/nonroot/xenforo_list_commands/venv/bin/python3

from settings import (get_connection_racetimer, xenforo_api)
import requests


def get_all_ze_commands():
    with get_connection_racetimer() as conn:
        with conn.cursor(buffered=True) as cur:
            sql_statement = """
                select * 
                from unloze_racetimer_css.ze_commands_info zci
                order by command asc
            """
            cur.execute(sql_statement)
            res = cur.fetchall()
    conn.close()
    return res

def create_thread():
    headers = {
          'Content-type' : 'application/x-www-form-urlencoded',
           'XF-Api-User': '4',
          'XF-Api-Key' : xenforo_api
          }
    url_thread = "https://unloze.com/api/threads/"
    data = {
    'node_id': 74, # node ID is the forum ID, 74 should be ze
    'title': 'This is a test thread',
    'message': 'This is the thread message body contents. This is the thread message body contents.'
    }

    r = requests.post(url_thread, headers=headers, data=data)
    print(r.status_code)

def edit_thread_post(res):
    headers = {
          'Content-type' : 'application/x-www-form-urlencoded',
           'XF-Api-User': '4',
          'XF-Api-Key' : xenforo_api
          }
    url_post_to_update = "https://unloze.com/api/posts/27293"
    msg = f"""
        This Thread serves the purpose of automatically listing all available commands on the zombie escape server that are available for normal players, vips and Admins. The Zombie Escape server currently has a total of {len(res)} Commands which are too many for any normal player or admin to remember. This thread hopefully serves the purpose of being a bookmark for finding all commands.\nSome commands like listmaps only work in console.\n\nThe thread will automatically be updated twice a day to include any new commands that are added on Zombie Escape. Use this thread as the ultimate commands reference (feedback is welcome ofc on how listing content should be done).\n\n"""
    msg_normal_commands = "[COLOR=rgb(184, 49, 47)][B][SIZE=8]Commands for normal players:[/SIZE][/B][/COLOR]\n\n"
    msg_vip_commands = "[COLOR=rgb(184, 49, 47)][B][SIZE=8]Commands for vips players:[/SIZE][/B][/COLOR]\n\n"
    msg_admin_commands = "[COLOR=rgb(184, 49, 47)][B][SIZE=8]Commands for admins:[/SIZE][/B][/COLOR]\n\n"

    for result in res:
        command = result[0]
        description = result[1]
        flag = result[2]
        flag = flag.strip()
        if flag == 'No flag registered':
            msg_normal_commands += f"{command} : {description}\n"
        elif flag == 'ADMFLAG_CUSTOM1 ' or flag == 'ADMFLAG_CUSTOM2' or flag == 'ADMFLAG_RESERVATION':
            msg_vip_commands += f"{command} : {description}\n"
        else:
            msg_admin_commands += f"{command} : {description}\n"
    msg += msg_normal_commands + "\n"
    msg += msg_vip_commands + "\n"
    msg += msg_admin_commands + "\n"
    data = {
        'message': msg
    }
    r = requests.post(url_post_to_update, headers=headers, data=data)
    print(r.status_code)

def main():
    res = get_all_ze_commands()
    #create_thread()
    edit_thread_post(res)

if __name__ == '__main__':
    main()
