/*
    NEWS REPORTERS
*/

#include <YSI_Coding\y_hooks>

#define check_reporter              if(!IsAReporter(playerid)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not a reporter.");

new LiveInvite[MAX_PLAYERS];

hook OnPlayerConnect(playerid)
{
    LiveInvite[playerid] = INVALID_PLAYER_ID;
}

hook OnPlayerDisconnect(playerid, reason)
{
    Iter_Remove(live_players, playerid);

    LiveInvite[playerid] = INVALID_PLAYER_ID;
}

CB:OnPlayerAcceptsLiveInvite(playerid, player)
{
    if(LiveInvite[playerid] == INVALID_PLAYER_ID) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"No one has sent you an offer.");
    if(LiveInvite[playerid] != player) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player didn't sent you an invite.");
    if(!IsAReporter(LiveInvite[playerid]))
    {
        LiveInvite[playerid] = INVALID_PLAYER_ID;
    }

    Iter_Add(live_players, playerid);
    Iter_Add(live_players, player);

    SCM(LiveInvite[playerid], COLOR_YELLOW, "%s has accepted your invite!", GetName(playerid));
    SCM(playerid, COLOR_PLATINUM, "You have accepted the live interview. Respect the rules!");

    GiveRaportPoints(LiveInvite[playerid], 1, 1);

    LiveInvite[playerid] = INVALID_PLAYER_ID;
    return 1;
}

stock IsAReporter(playerid)
{
    if(groupVariables[playerVariables[playerid][pGroup]][gType] == 3) return 1;
    return 0;
}

CMD:news(playerid, params[])
{
    check_reporter

    if(Iter_Count(live_players) > 0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You can't place ads right now.");

    if(GetVehicleModel(GetPlayerVehicleID(playerid)) != 582) 
    {
        return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not in a news van.");
    }

    if(isnull(params))
    {
        return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/news [text]");
    }

    if(GetPVarInt(playerid, "news_delay") > gettime())
    {
        return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You can send another ad in %d seconds.", GetPVarInt(playerid, "news_delay") - gettime());
    }

    new hour, minute, second;
    gettime(hour, minute, second);

    if(minute >= 50 && minute <= 59) SetPVarInt(playerid, "news_delay", gettime() + 5);
    else SetPVarInt(playerid, "news_delay", gettime() + 120);

    new price = strlen(params) * 2;
    GivePlayerCash(playerid, price);

    va_SendClientMessageToAll(COLOR_NEWS, "NR %s: %s", GetName(playerid), params);

    SCM(playerid, COLOR_PINK, "Ai primit $%s deoarece anuntul tau are %d caractere.", FormatNumber(price), strlen(params));

    GiveRaportPoints(playerid, 0, 1);
    return 1;
}

CMD:live(playerid, params[])
{
    if(playerVariables[playerid][pAdminLevel] < 6)
    {
        check_reporter

        if(playerVariables[playerid][pGroupRank] < 2) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You need rank 2+ in order to take interviews.");
    }

    new player;
    if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/live [player id/name]");
    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Player not connected.");
    if(Iter_Contains(live_players, player)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is already in an interview.");
    if(GetPlayerScore(player) < 5) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player needs to have level 5+ in order to take an interview.");

    if(LiveInvite[player] == playerid) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You already sent a live request to that player.");
    LiveInvite[player] = playerid;

    SCM(player, COLOR_D, "Reporter %s invited you to take an interview. Type /accept live %d to accept!", GetName(playerid), playerid);
    SCM(playerid, COLOR_PINK, "Invite sent!");
    return 1;
}

CMD:endlive(playerid, params[])
{
    if(playerVariables[playerid][pAdminLevel] == 0)
    {
        check_reporter

        if(playerVariables[playerid][pGroupRank] < 2) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You need rank 2+ in order to take interviews.");
    }

    new player;
    if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/endlive [player id/name]");
    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Player not connected.");
    if(!Iter_Contains(live_players, player)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not in an interview.");

    Iter_Remove(live_players, player);

    SCM(player, COLOR_YELLOW, "%s stopped the interview.", GetName(playerid));
    SCM(playerid, COLOR_AD, "You have stopped the interview with %s.", GetName(player));
    return 1;
}