local assets=
{
	Asset("ANIM", "anim/mushasword_base.zip"),
	Asset("ANIM", "anim/swap_mushasword_base.zip"),
    	Asset("ATLAS", "images/inventoryimages/mushasword_base.xml"),
	Asset("IMAGE", "images/inventoryimages/mushasword_base.tex"),

}


---------------------------
local function levelexp(inst,data)

	local max_exp = 31
	local exp = math.min(inst.level, max_exp)

if inst.level <= 30 then
inst.components.talker:Say("-[Broken Blade]- \n[Grow Points]".. (inst.level))
inst:RemoveTag("levelup")
end
if inst.level >=0 and inst.level <5 then
inst.components.talker:Say("-[Broken Blade] \nNext:[5]\n[Grow Points]".. (inst.level))
inst:AddTag("level1")
elseif inst.level >=5 and inst.level <10 then
inst.components.talker:Say("-[Broken Blade] \nNext:[10]\n[Grow Points]".. (inst.level))
inst:AddTag("level2")
inst:RemoveTag("level1")
elseif inst.level >=10 and inst.level <15 then
inst.components.talker:Say("-[Broken Blade] \nNext:[15]\n[Grow Points]".. (inst.level))
inst:AddTag("level3")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
elseif inst.level >=15 and inst.level <30 then
inst.components.talker:Say("-[Broken Blade] \nNext:[30]\n[Grow Points]".. (inst.level))
inst:AddTag("level4")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
elseif inst.level >=30 then
inst.components.talker:Say("-[Broken Blade] \n[MAX]")
inst:AddTag("level5")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
end
end


local function onpreload(inst, data)
	if data then
		if data.level then
		inst.level = data.level
			levelexp(inst)
end
	end
		end
local function onsave(inst, data)
	data.level = inst.level
	data.charge_time = inst.charge_time
end

--[[
local function ShouldAcceptItem(inst, item)
    if item.components.edible and inst.components.hunger and inst.components.eater then
        return inst.components.eater:CanEat(item) and inst.components.hunger:GetPercent() <= 1
    end
end
local function OnGetItemFromPlayer(inst, giver, item)
    if item.components.edible then
        end
        if inst.components.eater:Eat(item) then
	inst.components.hunger:DoDelta(10)
		local fx = SpawnPrefab("splash")
	local pos = Vector3(inst.Transform:GetWorldPosition())
fx.Transform:SetPosition(pos:Get())

        end
    end

local function Onexp(inst, food)
	if food and food.components.edible and food.components.edible.foodtype == "ELEMENTAL" then
	inst.level = inst.level + 1
	levelexp(inst)
	elseif food and food.components.edible and food.components.edible.foodtype == "CRISTAL" then
	inst.level = inst.level + 100
	inst.components.hunger:DoDelta(100)
	levelexp(inst)
inst:AddTag("levelup")
	elseif food and food.components.edible and food.components.edible.foodtype == "EXP" then
	inst.level = inst.level + 10
	inst.components.hunger:DoDelta(30)
	levelexp(inst)
inst:AddTag("levelup")
	elseif food and food.components.edible and food.components.edible.foodtype == "EXP2" then
	inst.level = inst.level + 1000
	inst.components.hunger:DoDelta(100)
	levelexp(inst)
inst:AddTag("levelup")
	end
	end

local function OnDurability(inst, data)
 if  inst:HasTag("talkf") then
if inst.components.hunger.current <= 100 and inst.components.hunger.current > 99 then
	--inst.components.talker:Say("Phoenix Blade \nDurability (100)")
elseif inst.components.hunger.current <= 95 and inst.components.hunger.current > 94 then
	inst.components.talker:Say("Phoenix Blade \nDurability (95)")
elseif inst.components.hunger.current <= 90 and inst.components.hunger.current > 89 then
	inst.components.talker:Say("Phoenix Blade \nDurability (90)")
elseif inst.components.hunger.current <= 85 and inst.components.hunger.current > 84 then
	inst.components.talker:Say("Phoenix Blade \nDurability (85)")
elseif inst.components.hunger.current <= 80 and inst.components.hunger.current > 79 then
	inst.components.talker:Say("Phoenix Blade \nDurability (80)")
elseif inst.components.hunger.current <= 75 and inst.components.hunger.current > 84 then
	inst.components.talker:Say("Phoenix Blade \nDurability (75)")
elseif inst.components.hunger.current <= 70 and inst.components.hunger.current > 69 then
	inst.components.talker:Say("Phoenix Blade \nDurability (70)")
elseif inst.components.hunger.current <= 65 and inst.components.hunger.current > 64 then
	inst.components.talker:Say("Phoenix Blade \nDurability (65)")
elseif inst.components.hunger.current <= 60 and inst.components.hunger.current > 59 then
	inst.components.talker:Say("Phoenix Blade \nDurability (60)")
elseif inst.components.hunger.current <= 55 and inst.components.hunger.current > 54 then
	inst.components.talker:Say("Phoenix Blade \nDurability (55)")
elseif inst.components.hunger.current <= 50 and inst.components.hunger.current > 49 then
	inst.components.talker:Say("Phoenix Blade \nDurability (50)")
elseif inst.components.hunger.current <= 45 and inst.components.hunger.current > 49 then
	inst.components.talker:Say("Phoenix Blade \nDurability (45)")
elseif inst.components.hunger.current <= 40 and inst.components.hunger.current > 39 then
	inst.components.talker:Say("Phoenix Blade \nDurability (40)")
elseif inst.components.hunger.current <= 35 and inst.components.hunger.current > 34 then
	inst.components.talker:Say("Phoenix Blade \nDurability (35)")
elseif inst.components.hunger.current <= 30 and inst.components.hunger.current > 29 then
	inst.components.talker:Say("Phoenix Blade \nDurability (30)")
elseif inst.components.hunger.current <= 25 and inst.components.hunger.current > 24 then
	inst.components.talker:Say("Phoenix Blade \nDurability (25)")
elseif inst.components.hunger.current <= 20 and inst.components.hunger.current > 19 then
	inst.components.talker:Say("Phoenix Blade \nDurability (20)")
elseif inst.components.hunger.current <= 15 and inst.components.hunger.current > 14 then
	inst.components.talker:Say("Phoenix Blade \nDurability (15)")
elseif inst.components.hunger.current <= 10 and inst.components.hunger.current > 9 then
	inst.components.talker:Say("Phoenix Blade \nDurability (10)")
elseif inst.components.hunger.current <= 5 and inst.components.hunger.current > 4 then
	inst.components.talker:Say("Phoenix Blade \nDurability (5)")
elseif inst.components.hunger.current <= 1 and inst.components.hunger.current > 0 then
inst:AddTag("broken")
    inst.components.weapon:SetDamage(1)
inst.components.hunger:SetRate(0)
inst.components.talker:Say("[Completely Broken blade]\nDamage (1)\nDurability (1)")
elseif inst.components.hunger.current <= 0 then
	inst.components.hunger:DoDelta(1)
elseif inst.components.hunger.current > 1 then
	inst:RemoveTag("broken")
end
end
end
]]

local function Upgradedamage(inst)
if inst:HasTag("broken") then
   inst.components.weapon:SetDamage(1)
   end
if not inst:HasTag("broken") then
        if inst:HasTag("level1") then
    inst.components.weapon:SetDamage(20)
	inst.components.talker:Say("-[Broken Phoenix Blade (LV1)]\nDamage (20)\nUnlimited\nDurability")
        elseif inst:HasTag("level2") then
    inst.components.weapon:SetDamage(22)
	inst.components.talker:Say("-[Broken Phoenix Blade (LV2)]\nDamage (22)\nUnlimited\nDurability")
        elseif inst:HasTag("level3") then
    inst.components.weapon:SetDamage(24)
	inst.components.talker:Say("-[Broken Phoenix Blade (LV3)]\nDamage (24)\nUnlimited\nDurability")
        elseif inst:HasTag("level4") then
    inst.components.weapon:SetDamage(26)
	inst.components.talker:Say("-[Broken Phoenix Blade (LV4)]\nDamage (26)\nUnlimited\nDurability")
        elseif inst:HasTag("level5") then
    inst.components.weapon:SetDamage(30)
	inst.components.talker:Say("-[Broken Phoenix Blade (LV5)]\nDamage (30)\nUnlimited\nDurability")
end
end
end

local function OnLoad(inst, data)
    Upgradedamage(inst)
end


local function onattack_FRAME(inst, attacker, target)
local player = GetPlayer()
local expchance = 0.15

if math.random() < expchance and not inst:HasTag("broken") and inst.level < 31 then
	inst.level = inst.level + 1
	levelexp(inst)
end

    if target.components.burnable and target.sg and not target.sg:HasStateTag("frozen") and not target:HasTag("wall") and not target:HasTag("structure") and player:HasTag("framea") and not player:HasTag("DLC") then
            target.sg:GoToState("hit")
        target.components.burnable:Ignite()
local fx = SpawnPrefab("statue_transition")
local pos = Vector3(target.Transform:GetWorldPosition())
	fx.Transform:SetPosition(pos:Get())
        attacker.components.sanity:DoDelta(-10)
		target.components.health:DoDelta(-70)
		player:RemoveTag("framea") 

    elseif target.components.burnable and target.sg and not target.sg:HasStateTag("frozen") and not target:HasTag("wall") and not target:HasTag("structure") and player:HasTag("framea") and player:HasTag("DLC") then
            target.sg:GoToState("hit")
        target.components.burnable:Ignite()
local fx = SpawnPrefab("groundpoundring_fx")
local pos = Vector3(target.Transform:GetWorldPosition())
	fx.Transform:SetPosition(pos:Get())
        attacker.components.sanity:DoDelta(-10)
		target.components.health:DoDelta(-70)
		player:RemoveTag("framea") 

	    end
    if target.components.burnable and target.components.burnable:IsBurning() then
        target.components.burnable:Extinguish()
    end
    if target.components.combat then
        target.components.combat:SuggestTarget(attacker)
    end
		player:RemoveTag("framea") 
end

local function onfinished(inst,data)
--[[
local fin = SpawnPrefab("mushasword_base")
fin.Transform:SetPosition(inst.Transform:GetWorldPosition())	
    inst:Remove()
]]--
end

local function OnDropped(inst,data)
   inst:AddTag("talkf")
    end
local function OnPutInInventory(inst)
   inst:RemoveTag("talkf")
    end

local function onequip(inst, owner)
    Upgradedamage(inst)
   inst:AddTag("talkf")
--inst.components.hunger:SetRate(1)
local player = GetPlayer()
    owner.AnimState:OverrideSymbol("swap_object", "swap_mushasword_base", "swap_nightmaresword")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
		player:AddTag("phoenixblade") 

end
local function onunequip(inst, owner) 
    Upgradedamage(inst)
   inst:RemoveTag("talkf")
--inst.components.hunger:SetRate(0)
local player = GetPlayer()
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
		player:RemoveTag("phoenixblade") 
		player:RemoveTag("framea") 

end


local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    
    inst.AnimState:SetBank("nightmaresword")
    inst.AnimState:SetBuild("mushasword_base")
    inst.AnimState:PlayAnimation("idle")
    inst.AnimState:SetMultColour(1, 1, 1, 0.6)

    inst:AddTag("sharp")
        inst:AddTag("metal")
    inst:AddComponent("weapon")
    inst.components.weapon:SetOnAttack(Upgradedamage)

      inst.components.weapon:SetOnAttack(onattack_FRAME)

    -------
    

    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
    	inst.components.inventoryitem.atlasname = "images/inventoryimages/mushasword_base.xml"
     inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "blade_b.tex" )

        inst.entity:AddLight()
	inst.Light:SetRadius(.2)
    inst.Light:SetFalloff(.9)
    inst.Light:SetIntensity(.9)
    inst.Light:SetColour(50/255,50/255,50/255)
	inst.Light:Enable(true)

    inst.OnLoad = OnLoad

--inst:AddTag("items")
    inst:AddComponent("talker")
    inst.components.talker.fontsize = 19
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(1, 0.8, 0.65, 1)
    inst.components.talker.offset = Vector3(200,-250,0)

    inst.components.talker.symbol = "swap_object"

   --[[ inst:AddComponent("health")
    inst.components.health:SetMaxHealth(100)
	inst.components.health.canmurder = false
    inst:AddComponent("hunger")
	inst.components.hunger:SetMax(100)
inst.components.hunger:SetRate(0)
    inst:AddComponent("eater")
    inst.components.eater:SetOnEatFn(Onexp)
	inst.components.eater.strongstomach = true
	inst.components.eater:SetElemental()
inst.components.eater.foodprefs = { "ELEMENTAL", "CRISTAL", "EXP", }
    inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
    inst.components.trader.onaccept = OnGetItemFromPlayer
inst:ListenForEvent("hungerdelta", OnDurability)]]

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )

inst:AddTag("level1")
	inst.level = 0
inst:ListenForEvent("levelup", levelexp)
	inst.OnSave = onsave
	inst.OnPreLoad = onpreload

    return inst
end

return Prefab( "common/inventory/mushasword_base", fn, assets) 
