local PLUGIN = PLUGIN
PLUGIN.name = "Animation Context Menu"
PLUGIN.author = "RefoselTeamWork"
PLUGIN.description = "Замена стандартного Context Menu на меню выбора анимаций"

PLUGIN.AnimationTable = {
    [1] = {name = "Помахать руками", anim = "rp_wave", loop = false},
    [2] = {name = "Помахать рукой", anim = "gesture_wave_original", loop = false},
    [3] = {name = "Показать пальцем вперед", anim = "rp_point", loop = false},
    [4] = {name = "Показать пальцем назад", anim = "rp_point_back", loop = false},
    [5] = {name = "Поправить галстук", anim = "menu_gman", loop = false},
    [6] = {name = "Приседания", anim = "rp_sport1_loop", loop = true},
    [7] = {name = "Отжимания", anim = "rp_sport2_loop", loop = true},
    [8] = {name = "Подъем корпуса", anim = "rp_sport3_loop", loop = true},
    [9] = {name = "Берпи", anim = "rp_sport4_loop", loop = true},
    [10] = {name = "Стоять злобно", anim = "rp_angry_loop", loop = true},
    [11] = {name = "Стоять напуганно", anim = "idle_all_scared", loop = true},
    [12] = {name = "Стоять, сложив руки", anim = "pose_standing_01", loop = true},
    [13] = {name = "Стоять, руки на поясе", anim = "pose_standing_02", loop = true},
    [14] = {name = "Стоять, держась за пояс", anim = "rp_cop_idle", loop = true},
    [15] = {name = "Сидеть, рука на колене", anim = "pose_ducking_01", loop = true},
    [16] = {name = "Сидеть в позе лотоса", anim = "pose_ducking_02", loop = true},
    [17] = {name = "Сидеть в сонном состоянии", anim = "rp_sit_loop", loop = true},
    [18] = {name = "Лежать в плохом состоянии", anim = "rp_injured_loop", loop = true},
    [19] = {name = "Танец 1", anim = "rp_dance1_loop", loop = true},
    [20] = {name = "Танец 2", anim = "rp_dance2_loop", loop = true},
    [21] = {name = "Танец 3", anim = "rp_dance3_loop", loop = true},
    [23] = {name = "Согласие", anim = "gesture_agree_original", loop = false},
    [24] = {name = "Несогласие", anim = "gesture_disagree_original", loop = false},
    [25] = {name = "Позвать с собой", anim = "gesture_becon_original", loop = false},
    [26] = {name = "Поклониться", anim = "gesture_bow_original", loop = false},
    [27] = {name = "Отдать честь", anim = "gesture_salute_original", loop = false},
    [28] = {name = "Пить кофе", anim = "rp_drinking", loop = true, prop = {model = "models/props_junk/garbage_coffeemug001a.mdl", spawnfunc = function(mod, ent)
        local att = ent:LookupAttachment("anim_attachment_RH")
        local lvec = Vector(1,0,1)

        timer.Create("WhileThatPropExist"..ent:EntIndex(), 0, 0, function()
            if IsValid(mod) then
                local tab = ent:GetAttachment(att)

                if (tab) then
                    mod:SetPos(tab.Pos+tab.Ang:Up()*lvec.z+tab.Ang:Right()*lvec.y+tab.Ang:Forward()*lvec.x)
                    mod:SetAngles(tab.Ang)
                end

                if !IsValid(ent) or !ent:Alive() then
                    timer.Remove("WhileThatPropExist"..ent:EntIndex())
                    mod:Remove()
                end
            else
                timer.Remove("WhileThatPropExist"..ent:EntIndex())
            end
        end)
    end}},
    [29] = {name = "Смотреть на объект", anim = "rp_medic_idle", loop = true},
    [30] = {name = "Записывать в блокнот", anim = "rp_writing", loop = true, prop = {model = "models/props_lab/clipboard.mdl", spawnfunc = function(mod, ent)
        local att = ent:LookupAttachment("anim_attachment_RH")
        local lvec = Vector(2,-2,2)

        timer.Create("WhileThatPropExist"..ent:EntIndex(), 0, 0, function()
            if IsValid(mod) then
                local tab = ent:GetAttachment(att)

                if (tab) then
                    mod:SetPos(tab.Pos+tab.Ang:Up()*lvec.z+tab.Ang:Right()*lvec.y+tab.Ang:Forward()*lvec.x)
                    mod:SetAngles(tab.Ang+Angle(140,0,-100))
                end

                if !IsValid(ent) or !ent:Alive() then
                    timer.Remove("WhileThatPropExist"..ent:EntIndex())
                    mod:Remove()
                end
            else
                timer.Remove("WhileThatPropExist"..ent:EntIndex())
            end
        end)
    end}},
    [31] = {name = "Спать", anim = "rp_sleep", loop = true},
    [32] = {name = "Воинское приветствие", anim = "rp_salute1", loop = true, loop_stop = true},
    [33] = {name = "Танец в присядь", anim = "rp_dance_squat", loop = true},
}

if SERVER then
    concommand.Add("rp_set_taunt", function(ply, cmd, args)
        local arg = tonumber(args[1])
        if arg and arg > 0 and arg <= #PLUGIN.AnimationTable then
            local tab = PLUGIN.AnimationTable[arg]
            PLUGIN:SetTAnimation(ply, tab.anim, not tab.loop, arg)
        end
    end)

    function PLUGIN:SetTAnimation(ply, anim, autostop, id)
        ply:SetNW2String('TauntAnim', anim)
        ply:SetNW2Float('TauntID', id)
        ply:SetNW2Float('TAnimDelay', select(2, ply:LookupSequence(anim)))
        ply:SetNW2Float('TAnimStartTime', CurTime())
        ply:SetCycle(0)

        local wep = ply:GetActiveWeapon()
        if IsValid(wep) and anim != "" then
            ply.TauntPreviousWeapon = wep:GetClass()
            ply:SetActiveWeapon(nil)
        elseif anim == "" and isstring(ply.TauntPreviousWeapon) then
            ply:SelectWeapon(ply.TauntPreviousWeapon)
            ply.TauntPreviousWeapon = nil
        end

        if autostop then
            local delay = select(2, ply:LookupSequence(anim))
            timer.Create("TauntAGRP"..ply:EntIndex(), delay, 1, function()
                if not IsValid(ply) then return end
                local anim2 = ply:GetNW2String('TauntAnim')
                if anim == anim2 then
                    PLUGIN:SetTAnimation(ply, "")
                end
            end)
        end
    end

    function PLUGIN:PlayerSwitchWeapon(ply, oldWeapon, newWeapon)
        local str = ply:GetNW2String('TauntAnim')
        if str != "" then
            return true
        end
    end

    function PLUGIN:Think()
        for _, ply in ipairs(player.GetAll()) do
            local str = ply:GetNW2String('TauntAnim')
            if str != "" and (ply:InVehicle() or not ply:Alive() or ply:WaterLevel() >= 2) then
                PLUGIN:SetTAnimation(ply, "")
            end
        end
    end
else
    CreateConVar("rp_taunt_firstperson", 0, FCVAR_ARCHIVE, "", 0, 1)
    CreateConVar("rp_taunt_realistic_firstperson", 0, FCVAR_ARCHIVE, "", 0, 1)
end

function PLUGIN:SetupMove(ply, mvd, cmd)
    local str = ply:GetNW2String('TauntAnim')
    if str != "" then
        mvd:SetMaxSpeed(1)
        mvd:SetMaxClientSpeed(1)
        if SERVER and ply:KeyDown(IN_SPEED) then
            PLUGIN:SetTAnimation(ply, "")
        end
    end
end

function PLUGIN:CalcMainActivity(ply, vel)
    local str = ply:GetNW2String('TauntAnim')
    local num = ply:GetNW2Float('TAnimDelay')
    local id = ply:GetNW2Float('TauntID')
    local st = ply:GetNW2Float('TAnimStartTime')
    
    if str != "" and ply:Alive() then
        local ls = PLUGIN.AnimationTable[id] and PLUGIN.AnimationTable[id].loop_stop
        
        if ply:GetCycle() >= 1 then
            if not ls then
                ply:SetCycle(0)
                if SERVER then
                    ply:SetNW2Float('TAnimStartTime', CurTime())
                end
            end
        else
            ply:SetCycle((CurTime()-st)/num)

            local tab = PLUGIN.AnimationTable[id] and PLUGIN.AnimationTable[id].prop
            if CLIENT and istable(tab) and (not IsValid(ply.TauntProp) or ply.TauntProp:GetModel() != tab.model) then
                if IsValid(ply.TauntProp) then
                    ply.TauntProp:Remove()
                end
                ply.TauntProp = ClientsideModel(tab.model)
                tab.spawnfunc(ply.TauntProp, ply)
            elseif CLIENT and not istable(tab) and IsValid(ply.TauntProp) then
                ply.TauntProp:Remove()
            end
        end

        return -1, ply:LookupSequence(str)
    else
        if SERVER then
            ply:SetNW2Float('TauntID', 0)
        elseif CLIENT and IsValid(ply.TauntProp) then
            ply.TauntProp:Remove()
        end
    end
end

ix.util.Include("cl_plugin.lua")
