--[[
BY
 _____   _____   __   _
|  _  \ /  _  \ |  \ | |
| | | | | | | | |   \| |
| | | | | | | | | |\   |
| |_| | | |_| | | | \  |
|_____/ \_____/ |_|  \_|

]]

require "prefabutil"

local containers = require("containers")

local assets =
{
    Asset("ANIM", "anim/booksdon.zip"),

	Asset("ANIM", "anim/donbooks_ui_ing.zip"),
	Asset("ANIM", "anim/donbooks_ui_ptbr.zip"),
	Asset("ANIM", "anim/donbooks_ui_chi.zip"),
}

local langsigla = STRINGS.DONRPG_LANG_SIGLA

local donbooksui_book_potionsvol1 = {
 widget = { animbank = ("donbooks_ui_book_potionsvol1_"..langsigla), animbuild = ("donbooks_ui_"..langsigla), pos = Vector3(0, 0, 0), side_align_tip = 160,buttoninfo  = {text = STRINGS.DONBTN_CLOSE, position = Vector3(-6, -463, 0), fn = function(inst)if inst.components.container ~= nil then inst.components.container:Close() elseif inst.replica.container ~= nil then inst.replica.container:Close() end	end, validfn = function(inst) return inst.replica.container ~= nil end,	}}, type = "chest", acceptsstacks = false, issidewidget = false, itemtestfn = function(container, item, slot) return false end,
}
local donbooksui_book_potionsvol2 = {
 widget = { animbank = ("donbooks_ui_book_potionsvol2_"..langsigla), animbuild = ("donbooks_ui_"..langsigla), pos = Vector3(0, 0, 0), side_align_tip = 160,buttoninfo  = {text = STRINGS.DONBTN_CLOSE, position = Vector3(-6, -463, 0), fn = function(inst)if inst.components.container ~= nil then inst.components.container:Close() elseif inst.replica.container ~= nil then inst.replica.container:Close() end	end, validfn = function(inst) return inst.replica.container ~= nil end,	}}, type = "chest", acceptsstacks = false, issidewidget = false, itemtestfn = function(container, item, slot) return false end,
}
local donbooksui_book_toolsandwpvol1 = {
 widget = { animbank = ("donbooks_ui_book_toolsandwpvol1_"..langsigla), animbuild = ("donbooks_ui_"..langsigla), pos = Vector3(0, 0, 0), side_align_tip = 160,buttoninfo  = {text = STRINGS.DONBTN_CLOSE, position = Vector3(-6, -463, 0), fn = function(inst)if inst.components.container ~= nil then inst.components.container:Close() elseif inst.replica.container ~= nil then inst.replica.container:Close() end	end, validfn = function(inst) return inst.replica.container ~= nil end,	}}, type = "chest", acceptsstacks = false, issidewidget = false, itemtestfn = function(container, item, slot) return false end,
}
local donbooksui_book_toolsandwpvol2 = {
 widget = { animbank = ("donbooks_ui_book_toolsandwpvol2_"..langsigla), animbuild = ("donbooks_ui_"..langsigla), pos = Vector3(0, 0, 0), side_align_tip = 160,buttoninfo  = {text = STRINGS.DONBTN_CLOSE, position = Vector3(-6, -463, 0), fn = function(inst)if inst.components.container ~= nil then inst.components.container:Close() elseif inst.replica.container ~= nil then inst.replica.container:Close() end	end, validfn = function(inst) return inst.replica.container ~= nil end,	}}, type = "chest", acceptsstacks = false, issidewidget = false, itemtestfn = function(container, item, slot) return false end,
}
local donbooksui_book_toolsandwpvol3 = {
 widget = { animbank = ("donbooks_ui_book_toolsandwpvol3_"..langsigla), animbuild = ("donbooks_ui_"..langsigla), pos = Vector3(0, 0, 0), side_align_tip = 160,buttoninfo  = {text = STRINGS.DONBTN_CLOSE, position = Vector3(-6, -463, 0), fn = function(inst)if inst.components.container ~= nil then inst.components.container:Close() elseif inst.replica.container ~= nil then inst.replica.container:Close() end	end, validfn = function(inst) return inst.replica.container ~= nil end,	}}, type = "chest", acceptsstacks = false, issidewidget = false, itemtestfn = function(container, item, slot) return false end,
}
local donbooksui_book_necronomicon = {
 widget = { animbank = ("donbooks_ui_book_necronomicon_"..langsigla), animbuild = ("donbooks_ui_"..langsigla), pos = Vector3(0, 0, 0), side_align_tip = 160,buttoninfo  = {text = STRINGS.DONBTN_CLOSE, position = Vector3(-6, -463, 0), fn = function(inst)if inst.components.container ~= nil then inst.components.container:Close() elseif inst.replica.container ~= nil then inst.replica.container:Close() end	end, validfn = function(inst) return inst.replica.container ~= nil end,	}}, type = "chest", acceptsstacks = false, issidewidget = false, itemtestfn = function(container, item, slot) return false end,
}


local oldwidgetsetup = containers.widgetsetup

function containers.widgetsetup(container, prefab, data, ...)
if container.inst.prefab == "donbookpotionsvol1" or prefab == "donbookpotionsvol1" then
	for k, v in pairs(donbooksui_book_potionsvol1) do container[k] = v end container:SetNumSlots(0) return
end
return oldwidgetsetup(container, prefab, data, ...)
end

function containers.widgetsetup(container, prefab, data, ...)
if container.inst.prefab == "donbookpotionsvol2" or prefab == "donbookpotionsvol2" then
	for k, v in pairs(donbooksui_book_potionsvol2) do container[k] = v end container:SetNumSlots(0) return
end
return oldwidgetsetup(container, prefab, data, ...)
end

function containers.widgetsetup(container, prefab, data, ...)
if container.inst.prefab == "donbooktoolandweaponsvol1" or prefab == "donbooktoolandweaponsvol1" then
	for k, v in pairs(donbooksui_book_toolsandwpvol1) do container[k] = v end container:SetNumSlots(0) return
end
return oldwidgetsetup(container, prefab, data, ...)
end

function containers.widgetsetup(container, prefab, data, ...)
if container.inst.prefab == "donbooktoolandweaponsvol2" or prefab == "donbooktoolandweaponsvol2" then
	for k, v in pairs(donbooksui_book_toolsandwpvol2) do container[k] = v end container:SetNumSlots(0) return
end
return oldwidgetsetup(container, prefab, data, ...)
end

function containers.widgetsetup(container, prefab, data, ...)
if container.inst.prefab == "donbooktoolandweaponsvol3" or prefab == "donbooktoolandweaponsvol3" then
	for k, v in pairs(donbooksui_book_toolsandwpvol3) do container[k] = v end container:SetNumSlots(0) return
end
return oldwidgetsetup(container, prefab, data, ...)
end

function containers.widgetsetup(container, prefab, data, ...)
if container.inst.prefab == "donbooknecronomicon" or prefab == "donbooknecronomicon" then
	for k, v in pairs(donbooksui_book_necronomicon) do container[k] = v end container:SetNumSlots(0) return
end
return oldwidgetsetup(container, prefab, data, ...)
end


local function onopen(inst)

end

local function onclose(inst)

end


local function MakeDonBook(prefabname, anim)
	local function fn()
		local inst = CreateEntity()

		inst.entity:AddTransform()
		inst.entity:AddAnimState()
		inst.entity:AddLight()
		inst.entity:AddNetwork()

		inst.AnimState:SetBank("booksdon")
		inst.AnimState:SetBuild("booksdon")
		inst.AnimState:PlayAnimation(anim)

		MakeInventoryPhysics(inst)

		inst.entity:SetPristine()

		if not TheWorld.ismastersim then

		if anim == "book_potionsvol1" then
			inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("donbooksui_book_potionsvol1", donbooksui_book_potionsvol1) end
		elseif anim == "book_potionsvol2" then
			inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("donbooksui_book_potionsvol2", donbooksui_book_potionsvol2) end
		elseif anim == "book_toolsandwpvol1" then
			inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("donbooksui_book_toolsandwpvol1", donbooksui_book_toolsandwpvol1) end
		elseif anim == "book_toolsandwpvol2" then
			inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("donbooksui_book_toolsandwpvol2", donbooksui_book_toolsandwpvol2) end
		elseif anim == "book_toolsandwpvol3" then
			inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("donbooksui_book_toolsandwpvol3", donbooksui_book_toolsandwpvol3) end
		elseif anim == "book_necronomicon" then
			inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("donbooksui_book_necronomicon", donbooksui_book_necronomicon) end
		end

		return inst
		end

		if prefabname == "donbooknecronomicon" then
			inst:AddComponent("tradable")
		end

		inst:AddComponent("container")

		if anim == "book_potionsvol1" then
			inst.components.container:WidgetSetup("donbooksui_book_potionsvol1", donbooksui_book_potionsvol1)
		elseif anim == "book_potionsvol2" then
			inst.components.container:WidgetSetup("donbooksui_book_potionsvol2", donbooksui_book_potionsvol2)
		elseif anim == "book_toolsandwpvol1" then
			inst.components.container:WidgetSetup("donbooksui_book_toolsandwpvol1", donbooksui_book_toolsandwpvol1)
		elseif anim == "book_toolsandwpvol2" then
			inst.components.container:WidgetSetup("donbooksui_book_toolsandwpvol2", donbooksui_book_toolsandwpvol2)
		elseif anim == "book_toolsandwpvol3" then
			inst.components.container:WidgetSetup("donbooksui_book_toolsandwpvol3", donbooksui_book_toolsandwpvol3)
		elseif anim == "book_necronomicon" then
			inst.components.container:WidgetSetup("donbooksui_book_necronomicon", donbooksui_book_necronomicon)
		end

		inst.components.container.onopenfn = onopen
		inst.components.container.onclosefn = onclose
		inst.components.container.canbeopened = true

		inst:AddComponent("inventoryitem")
		inst.components.inventoryitem.imagename = (prefabname.."icon")
		inst.components.inventoryitem.atlasname = ("images/inventoryimages/"..prefabname.."icon.xml")

		inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = TUNING.MED_FUEL

        MakeSmallBurnable(inst, TUNING.MED_BURNTIME)
        MakeSmallPropagator(inst)

		MakeHauntableLaunch(inst)


        return inst
    end

    return Prefab(prefabname, fn, assets)
end

return --MakeDonBook("donbookpotionsvol1", "book_potionsvol1"),
       --MakeDonBook("donbookpotionsvol2", "book_potionsvol2"),
       --MakeDonBook("donbooktoolandweaponsvol1", "book_toolsandwpvol1"),
       --MakeDonBook("donbooktoolandweaponsvol2", "book_toolsandwpvol2"),
       --MakeDonBook("donbooktoolandweaponsvol3", "book_toolsandwpvol3"),
       MakeDonBook("donbooknecronomicon", "book_necronomicon")
