#pragma semicolon 1
#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.0"
#pragma newdecls required  
#include <sourcemod>

Database g_hDatabase;
char sTitle[256];
char sBuffer[16][256];

public Plugin myinfo =
{
        name = "event notifier ingame",
        author = PLUGIN_AUTHOR,
        description = "plugin simply tells information about the last announced event on this server",
        version = PLUGIN_VERSION,
        url = "www.unloze.com"
};

public void OnPluginStart()
{
    if (!g_hDatabase)
    {
        Database.Connect(SQL_OnDatabaseConnect, "Event_notifier");
    }
    RegConsoleCmd("sm_event", Command_Event_notifier, "Display infos about the next event");
    RegConsoleCmd("sm_events", Command_Event_notifier, "Display infos about the next event");
}

public void OnMapStart()
{
    Event_select_query();
}

public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
{
    if(!db || strlen(error))
    {
        LogError("Database error: %s", error);
        return;
    }
    g_hDatabase = db;
    Event_select_query();
}

public Action Command_Event_notifier(int client, int args)
{
    Panel mSayPanel = new Panel(GetMenuStyleHandle(MenuStyle_Radio));
    mSayPanel.SetTitle(sTitle);
    mSayPanel.DrawItem("", ITEMDRAW_SPACER);

    mSayPanel.DrawText(sBuffer[1]); //server
    mSayPanel.DrawItem("", ITEMDRAW_SPACER);

    mSayPanel.DrawText(sBuffer[2]); //maps
    mSayPanel.DrawItem("", ITEMDRAW_SPACER);

    mSayPanel.DrawText(sBuffer[3]); //date
    mSayPanel.DrawItem("", ITEMDRAW_SPACER);

    mSayPanel.DrawText(sBuffer[4]); //time
    mSayPanel.DrawItem("", ITEMDRAW_SPACER);
        
    mSayPanel.DrawText(sBuffer[5]); //reward

    mSayPanel.DrawItem("", ITEMDRAW_SPACER);
    mSayPanel.DrawItem("1. Got it!", ITEMDRAW_RAWLINE);

    mSayPanel.SetKeys(1023);
    mSayPanel.Send(client, Handler_Menu, 0);
    delete mSayPanel;
    return Plugin_Handled;
}

public void Event_select_query()
{
    if (!g_hDatabase)
    {
        Database.Connect(SQL_OnDatabaseConnect, "Event_notifier");
        return;
    }
    //only 3 servers with events, none exist on jb or ze2 
    int i_port = GetConVarInt(FindConVar("hostport"));
    char sQuery[512];
    Format(sQuery, sizeof(sQuery), "select event_title, event_server, event_maps, event_date, event_time, event_reward from unloze_event.event e where e.event_server like '%s%i%s'", "%", i_port, "%");
    g_hDatabase.Query(SQL_OnQueryCompleted, sQuery);
}

public void SQL_OnQueryCompleted(Database db, DBResultSet results, const char[] error, DataPack data)
{
    if (!db || strlen(error))
    {
        LogError("Query error 3: %s", error);
        delete results;
    }
    if (results.RowCount && results.FetchRow())
    {
        results.FetchString(0, sTitle, sizeof(sTitle));
        Format(sTitle, sizeof(sTitle), "Title: %s", sTitle);

        results.FetchString(1, sBuffer[0], sizeof(sBuffer[]));

        Format(sBuffer[1], sizeof(sBuffer[]), "Server: %s", sBuffer[0]);
        
        results.FetchString(2, sBuffer[0], sizeof(sBuffer[]));

        Format(sBuffer[2], sizeof(sBuffer[]), "Maps: %s", sBuffer[0]);

        results.FetchString(3, sBuffer[0], sizeof(sBuffer[]));

        Format(sBuffer[3], sizeof(sBuffer[]), "Date: %s", sBuffer[0]);

        results.FetchString(4, sBuffer[0], sizeof(sBuffer[]));

        Format(sBuffer[4], sizeof(sBuffer[]), "Time: %s", sBuffer[0]);

        results.FetchString(5, sBuffer[0], sizeof(sBuffer[]));

        Format(sBuffer[5], sizeof(sBuffer[]), "Reward: %s", sBuffer[0]);
    }
    delete results;
}

public int Handler_Menu(Menu menu, MenuAction action, int param1, int param2)
{
    switch(action)
    {
        case MenuAction_Select, MenuAction_Cancel:
            delete menu;
    }
    return 0;
}
