--[[
File: lwc_bdet.script
Copyright: © Èðáès , Real Wolf
]]

local tbl = { detector_elite = "_d3", detector_simple = "_d1", detector_advances = "_d2" }
local tbls = { bolt_d1 = "detector_simple", bolt_d2 = "detector_advances", bolt_d3 = "detector_elite" }

local osect,oid,ocond,ncond,nsect,ob,nb,update,update1,nb1,update2,nb2,nb2c,wnd_s,texture,t
function used(obj)
	if tbl[obj:section()] then
		local bolt = db.actor:item_in_slot(5)
		if bolt ~= nil then
			if bolt:section() == "bolt" then
				db.actor:drop_item(bolt)
				nb = amk.spawn_item_in_inv("bolt"..tbl[obj:section()])
				ncond = obj:condition()
				update = true
			elseif string.find(bolt:section(),"bolt_d") then
				osect = bolt:section()
				ocond = bolt:condition()
				ncond = obj:condition()
				db.actor:drop_item(bolt)
				nb = amk.spawn_item_in_inv("bolt"..tbl[obj:section()])
				ob = amk.spawn_item_in_inv(tbls[osect])
				update = true				
			end
			if wnd_s == nil then
				wnd_s = CUIStatic()
				wnd_s:SetAutoDelete(true)
				inventory_wnd:AttachChild(wnd_s)
				wnd_s:SetStretchTexture(true)	
				wnd_s:SetWndRect(390,433,50,50)
			end
			wnd_s:InitTexture(obj:section())
			texture = obj:section()
		end	
	end
	if obj:section() == "wpn_knife" or obj:section() == "wpn_machette" then
		local slot = db.actor:item_in_slot(0)
		if slot then
			local sect = slot:section()
			amk.spawn_item_in_inv(string.sub(sect,1,(string.len(sect)-2)))
			db.actor:drop_item(slot)
			amk.remove_item(slot)
		end
		amk.spawn_item_in_inv(obj:section().."_f")
		if wnd_k == nil then
			wnd_k = CUIStatic()
			wnd_k:SetAutoDelete(true)
			inventory_wnd:AttachChild(wnd_k)
			wnd_k:SetStretchTexture(true)	
			wnd_k:SetWndRect(240,430,100,50)
		end
		wnd_k:InitTexture(obj:section())
		texture1 = obj:section()
	end	
end		

function Update()
	if update == true then
		local sobj = level.object_by_id(nb.id)
		if sobj ~= nil then
			sobj:set_condition(ncond)
			amk.save_variable("bs_cnd",ncond)
			update = false
		end 
		if ob ~= nil then 
			local sobj1 = level.object_by_id(ob.id)
			if sobj1 ~= nil then
				sobj1:set_condition(ocond)
				local t = amk.load_table("dc")
				t[ob.id] = ocond
				amk.save_table("dc", t)
			end
		end
	end
	if update1 == true then
		local sobj = level.object_by_id(nb1.id)
		if sobj then
			sobj:set_condition(amk.load_variable("bs_cnd"))
			update1 = false
		end
	end
	if update2 == true then
		local sobj = level.object_by_id(nb2.id)
		if sobj then
			sobj:set_condition(nb2c)
			local t = amk.load_table("dc")
			t[nb2.id] = nb2c
			amk.save_table("dc", t)
			update2 = false
		end
	end
end

function Save()
	if db.actor:item_in_slot(5) ~= nil then
		if string.find(db.actor:item_in_slot(5):section(),"bolt_d") then
			amk.save_variable("bs",db.actor:item_in_slot(5):section())
			amk.save_variable("bs_cnd",db.actor:item_in_slot(5):condition())
		else
			amk.del_variable("bs")
			amk.del_variable("bs_cnd")
		end	
	end			
end

function Load()
	if wnd_k ~= nil then
		wnd_k:Show(false)
	end
	if wnd_s ~= nil then
		wnd_s:Show(false)
	end
	local bolt = db.actor:item_in_slot(5)
	if amk.load_variable("bs") then
		db.actor:drop_item(bolt)
		nb1 = amk.spawn_item_in_inv(amk.load_variable("bs"))
		texture = tbls[ amk.load_variable("bs") ]
		amk.del_variable("bs")
		update1 = true
	else
		texture = "empty"
	end
	local knife = db.actor:item_in_slot(0)
	if knife then
		local sect = knife:section()
		texture1 = string.sub(sect,1,(string.len(sect)-2))
	else
		texture1 = "empty"
	end
	local t = amk.load_table("dc")
	for k,v in pairs(t) do
		local obj = level.object_by_id(k)
		if obj then
			obj:set_condition(v)
		end
	end
end

local inventory_wnd = nil
local rec_wnd = nil
local btn = nil
local btn1 = nil

class "ui_inv_det_slot" (CUIScriptWnd)

function ui_inv_det_slot:__init(owner) super()
   	self:Init(0,0,1,1)		
end

function ui_inv_det_slot:__finalize() 
end

function ui_inv_det_slot:on_click()
	local slot = db.actor:item_in_slot(5)
	if slot and slot:section() ~= "bolt" then
		nb2 = amk.spawn_item_in_inv(tbls[slot:section()])
		nb2c = slot:condition()
		db.actor:drop_item(slot)
		amk.spawn_item_in_inv("bolt")
		amk.del_variable("bs")
		amk.del_variable("bs_cnd")
		wnd_s:InitTexture("empty")		
		texture = "empty"
		update2 = true
	end
end

function ui_inv_det_slot:on_click1()
	local slot = db.actor:item_in_slot(0)
	if slot then
		local sect = slot:section()
		amk.spawn_item_in_inv(string.sub(sect,1,(string.len(sect)-2)))
		db.actor:drop_item(slot)
		amk.remove_item(slot)
		wnd_k:InitTexture("empty")				
		texture1 = "empty"
	end
end

function on_inventory_open()
	rec_wnd = ui_inv_det_slot()	
	inventory_wnd = level.main_input_receiver()

	btn = CUIButton()
	btn:SetAutoDelete(true)
	btn:Init("empty", 390 ,433 , 50, 50)
	btn:SetWidth(50)
	btn:SetHeight(50)
	inventory_wnd:AttachChild(btn)
	rec_wnd:Register(btn,"btn_slot")
	rec_wnd:AddCallback("btn_slot", ui_events.BUTTON_CLICKED, rec_wnd.on_click, rec_wnd)

	btn1 = CUIButton()
	btn1:SetAutoDelete(true)
	btn1:Init("empty", 205,430,150,50)
	btn1:SetWidth(100)
	btn1:SetHeight(50)
	inventory_wnd:AttachChild(btn1)
	rec_wnd:Register(btn1,"btn1_slot")
	rec_wnd:AddCallback("btn1_slot", ui_events.BUTTON_CLICKED, rec_wnd.on_click1, rec_wnd)
	
	if wnd_s == nil then
		wnd_s = CUIStatic()
		wnd_s:SetAutoDelete(true)
		inventory_wnd:AttachChild(wnd_s)
		wnd_s:SetStretchTexture(true)	
		wnd_s:SetWndRect(390,433,50,50)
	end
	if texture ~= nil then
		wnd_s:InitTexture(texture)
	end

	if wnd_k == nil then
		wnd_k = CUIStatic()
		wnd_k:SetAutoDelete(true)
		inventory_wnd:AttachChild(wnd_k)
		wnd_k:SetStretchTexture(true)	
		wnd_k:SetWndRect(240,430,100,50)
	end
	if texture1 ~= nil then
		wnd_k:InitTexture(texture1)
	end

	if amk.load_variable("fake_weight") == 1 then
		if db.actor:object("fake_weight") then
			db.actor:drop_item(db.actor:object("fake_weight"))
			local obj = alife():object(db.actor:object("fake_weight"):id())
			if obj then
				alife():release(obj)
			end
		end
	end

end

function protected_detach(wnd, btn, btn1)
	if wnd then
		wnd:DetachChild(btn)
		wnd:DetachChild(btn1)
	end
end

function on_inventory_hide()

	local drop, err_msg = pcall(protected_detach, inventory_wnd, btn, btn1)
	if (not drop) and (err_msg) then
		qqq("ERROR: lwc_bdet, fail to detach buttons, reason: "..tostring(err_msg))
	end

	if amk.load_variable("fake_weight") == 1 then
		if db.actor:item_in_slot(6) then
			if system_ini():line_exist(db.actor:item_in_slot(6):section(),"batt_life_standby") then
				if not db.actor:object("fake_weight") then
					if not inventory.belt["power_supply"] then
						amk.spawn_item_in_inv("fake_weight")
					end
				end
			end
		end
	end
end

function NetDestroy()
	if wnd_k ~= nil then
		wnd_k:Show(false)
	end
	if wnd_s ~= nil then
		wnd_s:Show(false)
	end
end