//====================================================================================================
//
// Name: [entWatch] Interface
// Author: zaCade & Prometheum
// Description: Handle the interface of [entWatch]
//
//====================================================================================================
#include <multicolors>

#pragma newdecls required

#include <sdktools>
#include <sourcemod>
#include <clientprefs>
#include <entWatch_core>
#include <entWatch_helpers>

/* COOKIES */
Handle g_hCookie_InterfaceHidden;

/* BOOLEANS */
bool g_bInterfaceHidden[MAXPLAYERS+1];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name         = "[entWatch] Interface",
	author       = "zaCade & Prometheum",
	description  = "Handle the interface of [entWatch]",
	version      = "4.0.0"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	LoadTranslations("entWatch.interface.phrases");

	g_hCookie_InterfaceHidden = RegClientCookie("EW_InterfaceHidden", "", CookieAccess_Private);

	RegConsoleCmd("sm_hud", Command_ToggleHUD);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	CreateTimer(1.0, OnDisplayHUD, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE|TIMER_REPEAT);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientCookiesCached(int client)
{
	g_bInterfaceHidden[client] = GetClientCookieBool(client, g_hCookie_InterfaceHidden);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	g_bInterfaceHidden[client] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_ToggleHUD(int client, int args)
{
	g_bInterfaceHidden[client] = !g_bInterfaceHidden[client];

	if (g_bInterfaceHidden[client])
	{
		SetClientCookieBool(client, g_hCookie_InterfaceHidden, true);

		CReplyToCommand(client, "\x07%s[entWatch] \x07%sYou will now no longer see the HUD.", "E01B5D", "F16767");
		return Plugin_Handled;
	}
	else
	{
		SetClientCookieBool(client, g_hCookie_InterfaceHidden, false);

		CReplyToCommand(client, "\x07%s[entWatch] \x07%sYou will now see the HUD again.", "E01B5D", "F16767");
		return Plugin_Handled;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnDisplayHUD(Handle timer)
{
    int iHUDPages[3];
    char sHUDPanels[3][8][255]

    for (int index; index < EW_GetItemCount(); index++)
    {
        CItem item = EW_GetItemData(index);

        if (item.bClient && item.dConfig.bDisplayInterface)
        {
            char sShort[32];
            item.dConfig.GetShort(sShort, sizeof(sShort));

            char sLine[96];
            switch(item.dConfig.iMode)
            {
                case MODE_COOLDOWN:
                {
                    if (item.flTimeReady > GetEngineTime())
                    {
                        Format(sLine, sizeof(sLine), "%s [%d]: %N", sShort, RoundToCeil(item.flTimeReady - GetEngineTime()), item.iClient);
                    }
                    else
                    {
                        Format(sLine, sizeof(sLine), "%s [%s]: %N", sShort, "R", item.iClient);
                    }
                }
                case MODE_MAXUSES:
                {
                    if (item.iTimesUsed < item.dConfig.iMaxUses)
                    {
                        Format(sLine, sizeof(sLine), "%s [%d/%d]: %N", sShort, item.iTimesUsed, item.dConfig.iMaxUses, item.iClient);
                    }
                    else
                    {
                        Format(sLine, sizeof(sLine), "%s [%s]: %N", sShort, "D", item.iClient);
                    }
                }
                case MODE_COOLDOWNMAXUSES:
                {
                    if (item.iTimesUsed < item.dConfig.iMaxUses)
                    {
                        if (item.flTimeReady > GetEngineTime())
                        {
                            Format(sLine, sizeof(sLine), "%s [%d]: %N", sShort, RoundToCeil(item.flTimeReady - GetEngineTime()), item.iClient);
                        }
                        else
                        {
                            Format(sLine, sizeof(sLine), "%s [%d/%d]: %N", sShort, item.iTimesUsed, item.dConfig.iMaxUses, item.iClient);
                        }
                    }
                    else
                    {
                        Format(sLine, sizeof(sLine), "%s [%s]: %N", sShort, "D", item.iClient);
                    }
                }
                case MODE_COOLDOWNCHARGES:
                {
                    if (item.flTimeReady > GetEngineTime())
                    {
                        Format(sLine, sizeof(sLine), "%s [%d]: %N", sShort, RoundToCeil(item.flTimeReady - GetEngineTime()), item.iClient);
                    }
                    else
                    {
                        Format(sLine, sizeof(sLine), "%s [%d/%d]: %N", sShort, item.iTimesUsed, item.dConfig.iMaxUses, item.iClient);
                    }
                }
                default:
                {
                    Format(sLine, sizeof(sLine), "%s [%s]: %N", sShort, "N/A", item.iClient);
                }
            }

            switch(GetClientTeam(item.iClient))
            {
                case(2):
                {
                    if (strlen(sHUDPanels[1][iHUDPages[1]]) + strlen(sLine) + 2 >= sizeof(sHUDPanels[][])) iHUDPages[1]++;

                    StrCat(sHUDPanels[1][iHUDPages[1]], sizeof(sHUDPanels[][]), sLine);
                    StrCat(sHUDPanels[1][iHUDPages[1]], sizeof(sHUDPanels[][]), "\n");
                }
                case(3):
                {
                    if (strlen(sHUDPanels[2][iHUDPages[2]]) + strlen(sLine) + 2 >= sizeof(sHUDPanels[][])) iHUDPages[2]++;

                    StrCat(sHUDPanels[2][iHUDPages[2]], sizeof(sHUDPanels[][]), sLine);
                    StrCat(sHUDPanels[2][iHUDPages[2]], sizeof(sHUDPanels[][]), "\n");
                }
            }

            if (strlen(sHUDPanels[0][iHUDPages[0]]) + strlen(sLine) + 2 >= sizeof(sHUDPanels[][])) iHUDPages[0]++;

            StrCat(sHUDPanels[0][iHUDPages[0]], sizeof(sHUDPanels[][]), sLine);
            StrCat(sHUDPanels[0][iHUDPages[0]], sizeof(sHUDPanels[][]), "\n");
        }
    }

    static int iPageUpdate;
    static int iPageCurrent[3];

    if (iPageUpdate >= 5)
    {
        for (int iPageID; iPageID < 3; iPageID++)
        {
            if (iPageCurrent[iPageID] >= iHUDPages[iPageID])
                iPageCurrent[iPageID] = 0;
            else
                iPageCurrent[iPageID]++;
        }

        iPageUpdate = 0;
    }
    else
        iPageUpdate++;

    for (int client = 1; client <= MaxClients; client++)
    {
        if (!IsClientInGame(client) || (IsFakeClient(client) && !IsClientSourceTV(client)) || g_bInterfaceHidden[client])
            continue;

        int iPanelID;
        switch(GetClientTeam(client))
        {
            case(2): iPanelID = 1;
            case(3): iPanelID = 2;
        }

        if (sHUDPanels[iPanelID][iPageCurrent[iPanelID]][0])
        {
            Handle hMessage = StartMessageOne("KeyHintText", client);
            BfWriteByte(hMessage, 1);
            BfWriteString(hMessage, sHUDPanels[iPanelID][iPageCurrent[iPanelID]]);
            EndMessage();
        }
    }
    return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock void SetClientCookieBool(int client, Handle hCookie, bool value)
{
	char sValue[32];
	IntToString(view_as<int>(value), sValue, sizeof(sValue));

	SetClientCookie(client, hCookie, sValue);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock bool GetClientCookieBool(int client, Handle hCookie)
{
	char sValue[32];
	GetClientCookie(client, hCookie, sValue, sizeof(sValue));

	return view_as<bool>(StringToInt(sValue));
}