--[[
 -- @Description:
 -- Hello , DiaoSi ~ ( or not ? ) ,glad you saw my comment
 -- If you think my mod is okay , you can sponsor my breakfast by a tiny amount donation  ($_$)
 -- paypal:td1madao@163.com     alipay:td1madao@163.com
 -- If any question ,Please email me
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- @version 1.0.0
 -- @author td1madao
 -- @email td1madao@163.com
 -- @qq 360810498
 -- @date 16/8/6
 ]]
local TD = TD.td1madao.rivendst
local MakePlayerCharacter = require "prefabs/player_common"
local assets = {
    Asset("ANIM", "anim/player_basic.zip"),
    Asset("ANIM", "anim/player_idles_shiver.zip"),
    Asset("ANIM", "anim/player_actions.zip"),
    Asset("ANIM", "anim/player_actions_axe.zip"),
    Asset("ANIM", "anim/player_actions_pickaxe.zip"),
    Asset("ANIM", "anim/player_actions_shovel.zip"),
    Asset("ANIM", "anim/player_actions_blowdart.zip"),
    Asset("ANIM", "anim/player_actions_eat.zip"),
    Asset("ANIM", "anim/player_actions_item.zip"),
    Asset("ANIM", "anim/player_actions_uniqueitem.zip"),
    Asset("ANIM", "anim/player_actions_bugnet.zip"),
    Asset("ANIM", "anim/player_actions_fishing.zip"),
    Asset("ANIM", "anim/player_actions_boomerang.zip"),
    Asset("ANIM", "anim/player_bush_hat.zip"),
    Asset("ANIM", "anim/player_attacks.zip"),
    Asset("ANIM", "anim/player_idles.zip"),
    Asset("ANIM", "anim/player_rebirth.zip"),
    Asset("ANIM", "anim/player_jump.zip"),
    Asset("ANIM", "anim/player_amulet_resurrect.zip"),
    Asset("ANIM", "anim/player_teleport.zip"),
    Asset("ANIM", "anim/wilson_fx.zip"),
    Asset("ANIM", "anim/player_one_man_band.zip"),
    Asset("ANIM", "anim/shadow_hands.zip"),
    Asset("SOUND", "sound/sfx.fsb"),
    Asset("SOUND", "sound/wilson.fsb"),
    Asset("ANIM", "anim/rivendst.zip"),
    Asset("ANIM", "anim/ghost_rivendst_build.zip"),
}
local prefabs = {}
local start_inv = { "rivendstsword" }
local master_postinit = function() end

local common_postinit = function(inst)
    TD.translate()
    inst.MiniMapEntity:SetIcon("rivendst.tex")
    inst:AddTag("rivendst")
    inst:AddTag("td1madao_mod")
    inst:AddTag("lol_category")
    TD.addC(inst, "rivendst_global_anim")
    TD.common(inst, function()
        TD.addC(inst, "rivendst_common_perioder")
        TD.addC(inst, "rivendst_common_task_manager")
        TD.addC(inst, "rivendst_common_skill_manager")
        TD.getC(inst, "rivendst_common_skill_manager"):common_declare("q", MADAO.RIVENDST_Q_CD, 1)
        TD.getC(inst, "rivendst_common_skill_manager"):common_declare("w", MADAO.RIVENDST_W_CD, 2)
        TD.getC(inst, "rivendst_common_skill_manager"):common_declare("e", MADAO.RIVENDST_E_CD, 3)
        TD.getC(inst, "rivendst_common_skill_manager"):common_declare("r", MADAO.RIVENDST_R_CD, 4)
        TD.addC(inst, "rivendst_common_synchronizer")
        TD.getC(inst, "rivendst_common_perioder"):common_addTask("td1madaoCdRefresh", function()
            TD.getC(inst, "rivendst_common_skill_manager"):common_refresh()
        end, 1)
        TD.player(inst, function()
            TD.getC(inst, "rivendst_common_task_manager"):player_task(0.5, function()
                TD.addC(inst, "rivendst_player_skill_register")
                TD.callC(inst, "rivendst_player_skill_register", function(comp)
                    comp:player_register("q", MADAO.RIVENDST_Q_UI_X, MADAO.RIVENDST_UI_Y)
                    comp:player_register("w", MADAO.RIVENDST_W_UI_X, MADAO.RIVENDST_UI_Y)
                    comp:player_register("e", MADAO.RIVENDST_E_UI_X, MADAO.RIVENDST_UI_Y)
                    comp:player_register("r", MADAO.RIVENDST_R_UI_X, MADAO.RIVENDST_UI_Y)
                    TD.getC(inst, "rivendst_common_skill_manager"):player_refresh(true)
                    TD.getC(inst, "rivendst_player_skill_register"):player_regist_lv_and_exp()
                end)
                if not TD.smart() then
                    TD.addC(inst, "rivendst_player_mouse_listener")
                end
                TD.player_init(inst)
            end, "td1madao_rivendst_ui_register")
            if TD.START_LOGGER then
                inst:ListenForEvent("keyup", function(inst, data)
                    if data.inst == inst and data.key == TD.DEBUG_KEY then
                        TD.START_LOGGER = not TD.START_LOGGER
                        TD.START_MONITOR = not TD.START_MONITOR
                    end
                end)
            end
        end)
        TD.server(function()
            TD.addC(inst, "rivendst_server_lv_manager")
            inst:ListenForEvent("onattackother", function(inst, data)
                local addition = 1
                if TD.getC(inst, "rivendst_common_synchronizer").server_rivenRBuff then
                    addition = 1.5
                end
                if TD.getC(inst, "rivendst_common_synchronizer").server_aftercast then
                    inst.components.combat.damagemultiplier = TD.getC(inst, "rivendst_common_synchronizer").server_damagemultiplierDefault * 1.5 * addition
                else
                    inst.components.combat.damagemultiplier = TD.getC(inst, "rivendst_common_synchronizer").server_damagemultiplierDefault * addition
                end
                TD.getC(inst, "rivendst_common_synchronizer").server_aftercast = false
            end)
            inst:ListenForEvent("attacked", function(inst, data)
                local dmg = data.damage or 1
                local syn = TD.getC(inst, "rivendst_common_synchronizer")
                syn.server_armor = syn.server_armor - dmg
                if syn.server_armor <= 0 then
                    syn.server_armor = 0
                    inst.components.health.absorb = 0
                    TD.remove(inst.rivenServerForcefieldfx)
                    inst.rivenServerForcefieldfx = nil
                end
            end)
            inst:ListenForEvent("entity_death", function(world, data)
                local enemy = data.inst
                if data and data.cause and (data.afflicter == inst) then
                    local exp = 1
                    local health = 1
                    if enemy.components.health then
                        health = enemy.components.health.maxhealth
                    end
                    local damage = 10
                    if enemy.components.combat and enemy.components.combat.defaultdamage then
                        damage = enemy.components.combat.defaultdamage
                    end
                    if enemy.components.combat then
                        exp = math.floor(health * damage * (enemy.components.combat.playerdamagepercent or 1) / 1000)
                    end
                    if exp > 1 then
                        TD.getC(inst, "rivendst_server_lv_manager"):server_getExp(exp)
                    end
                end
            end, TheWorld)
            inst.soundsname = "wendy"
            inst.defaultWeapon = TD.spawn("rivendstweapon")
        end)
    end)
end

return MakePlayerCharacter("rivendst", prefabs, assets, common_postinit, master_postinit, start_inv)