#pragma semicolon 1

#include <sourcemod>
#include <cstrike>
#include <multicolors>

bool g_bHappyHour;
bool g_bHappyHourAdmin;

int g_iMorningStart;
int g_iMorningEnd;
int g_iNightStart;
int g_iNightEnd;
float g_fMessageTimer;

Handle g_h_MessageTimer;

public Plugin myinfo =
{
	name = "Happy Hour",
	author = "Dogan + Neon",
	description = "Create an happy hour with more rank points",
	version = "1.3.0",
	url = ""
};

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
	CreateNative("HH_IsItHappyHour", Native_IsItHappyHour);
	RegPluginLibrary("HappyHour");

	return APLRes_Success;
}

public void OnPluginStart()
{
	g_bHappyHour = false;
	g_bHappyHourAdmin = false;

	CreateTimer(15.0, Timer_CheckTime, _, TIMER_REPEAT);

	ConVar cvar;
	HookConVarChange((cvar = CreateConVar("sm_happyhour_morning_start", "1000", "starttime of happy hour in the morning (timezone UTC+1 in summer, UTC+2 in winter)")), g_cvMorningStart);
	g_iMorningStart = cvar.IntValue;

	HookConVarChange((cvar = CreateConVar("sm_happyhour_morning_end", "1400", "endtime of happy hour in the morning/afternoon (timezone UTC+1 in summer, UTC+2 in winter)")), g_cvMorningEnd);
	g_iMorningEnd = cvar.IntValue;

	HookConVarChange((cvar = CreateConVar("sm_happyhour_night_start", "2300", "starttime of happy hour in the night (timezone UTC+1 in summer, UTC+2 in winter)")), g_cvNightStart);
	g_iNightStart = cvar.IntValue;

	HookConVarChange((cvar = CreateConVar("sm_happyhour_night_end", "0300", "endtime of happy hour in the night (timezone UTC+1 in summer, UTC+2 in winter)")), g_cvNightEnd);
	g_iNightEnd = cvar.IntValue;

	HookConVarChange((cvar = CreateConVar("sm_happyhour_message_interval", "60.0", "interval for repetetive message of happy hour in chat")), g_cvMessageTimer);
	g_fMessageTimer = cvar.FloatValue;
	delete cvar;

	RegConsoleCmd("sm_hh", Command_DisplayHappyHour, "Shows if happy hour is currently enabled or not");

	RegAdminCmd("sm_forcehh", AdminCommand_HappyHour, ADMFLAG_GENERIC, "Toggle to enable/disable Happy Hour. Resets after Mapswitch.");

	AutoExecConfig(true, "plugin.HappyHour");
}

public void g_cvMorningStart(ConVar convar, const char[] oldValue, const char[] newValue)
{
	g_iMorningStart = convar.IntValue;
}

public void g_cvMorningEnd(ConVar convar, const char[] oldValue, const char[] newValue)
{
	g_iMorningEnd = convar.IntValue;
}

public void g_cvNightStart(ConVar convar, const char[] oldValue, const char[] newValue)
{
	g_iNightStart = convar.IntValue;
}

public void g_cvNightEnd(ConVar convar, const char[] oldValue, const char[] newValue)
{
	g_iNightEnd = convar.IntValue;
}

public void g_cvMessageTimer(ConVar convar, const char[] oldValue, const char[] newValue)
{
	g_fMessageTimer = convar.FloatValue;

	delete g_h_MessageTimer;

	g_h_MessageTimer = CreateTimer(g_fMessageTimer, MessageHappyHour, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public Action Timer_CheckTime(Handle timer)
{
    if(g_bHappyHourAdmin)
        return Plugin_Continue;

    if((InsideTimeFrame(g_iMorningStart, g_iMorningEnd) == 0) || (InsideTimeFrame(g_iNightStart, g_iNightEnd) == 0))
	{
		g_bHappyHour = true;
	}
    else
	{
		g_bHappyHour = false;
	}

    return Plugin_Continue;
}

public int InsideTimeFrame(int MinTime, int MaxTime)
{
    char sTime[8];
    FormatTime(sTime, sizeof(sTime), "%H%M");

    int CurTime = StringToInt(sTime);

    //Wrap around.
    CurTime = (CurTime <= MinTime) ? CurTime + 2400 : CurTime;
    MaxTime = (MaxTime <= MinTime) ? MaxTime + 2400 : MaxTime;

    if (MinTime <= CurTime <= MaxTime)
	{
        return 0;
	}
	else
	{
		//Wrap around.
		MinTime = (MinTime <= CurTime) ? MinTime + 2400 : MinTime;
		MinTime = (MinTime <= MaxTime) ? MinTime + 2400 : MinTime;

		// Convert our 'time' to minutes.
		CurTime = (RoundToFloor(float(CurTime / 100)) * 60) + (CurTime % 100);
		MinTime = (RoundToFloor(float(MinTime / 100)) * 60) + (MinTime % 100);
		MaxTime = (RoundToFloor(float(MaxTime / 100)) * 60) + (MaxTime % 100);

		return MinTime - CurTime;
	}
}

public void OnMapStart()
{
	if(g_bHappyHourAdmin)
	{
		g_bHappyHourAdmin = false;
	}

	g_h_MessageTimer = CreateTimer(g_fMessageTimer, MessageHappyHour, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public Action Command_DisplayHappyHour(int client, int args)
{
	if(g_bHappyHour)
	{
		ReplyToCommand(client, "[SM] Happy Hour is currently active.");
	}
	else
	{
		int iTimeleftMorning = InsideTimeFrame(g_iMorningStart, g_iMorningEnd);
		int iTimeleftNight = InsideTimeFrame(g_iNightStart, g_iNightEnd);
		int iTimeleft;
		char sTimeleft[32];

		if(iTimeleftMorning >= iTimeleftNight)
			iTimeleft = iTimeleftNight;
		else
			iTimeleft = iTimeleftMorning;

		int iHours = (iTimeleft / 60) % 24;
		int iMinutes = (iTimeleft % 60);

		if(iHours)
			Format(sTimeleft, sizeof(sTimeleft), "%d Hours %02d Minutes", iHours, iMinutes);
		else
			Format(sTimeleft, sizeof(sTimeleft), "%d Minutes", iMinutes);

		ReplyToCommand(client, "[SM] Happy Hour is currently not active. Timeleft: %s.", sTimeleft);
	}

	return Plugin_Handled;
}

public Action AdminCommand_HappyHour(int client, int args)
{
	ToggleHappyHour(client);
	return Plugin_Handled;
}

public void ToggleHappyHour(int client)
{
	g_bHappyHourAdmin = !g_bHappyHourAdmin;

	if(g_bHappyHour)
	{
		g_bHappyHour = false;

		ReplyToCommand(client, "[SM] Deactivated Happy Hour.");
		CPrintToChatAll("[SM] %N deactivated Happy Hour!", client);
		LogAction(client, -1, "\"%L\" deactivated Happy Hour.", client);
	}
	else
	{
		g_bHappyHour = true;

		ReplyToCommand(client, "[SM] Activated Happy Hour.");
		CPrintToChatAll("[SM] %N activated Happy Hour!", client);
		LogAction(client, -1, "\"%L\" activated Happy Hour.", client);
	}
}

public Action MessageHappyHour(Handle timer)
{
	if(g_bHappyHour)
		CPrintToChatAll("{cyan}[UNLOZE] {red}Happy Hour {cyan}is currently active! Everyone gets 50%% Bonus on most rank points!");

	return Plugin_Continue;
}

public int Native_IsItHappyHour(Handle hPlugin, int numParams)
{
	if(g_bHappyHour)
		return true;

	return false;
}