-- ui_arty_container
--[[
Copyright (C) 2012 Alundaio
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License


Modified by Yasti for use with Artifact Containers
UI Modified by ponney68 --]]
-------------------------------------------------------------------
last_art_spawn = 0
class "load_item" (CUIListBoxItem)
function load_item:__init(height) super(height)
	self.file_name		= "filename"

	self:SetTextColor(GetARGB(255, 170, 170, 170))

	self.fn = self:GetTextItem()
	self.fn:SetFont(GetFontLetterica18Russian())
	self.fn:SetEllipsis(true)
end

function load_item:__finalize()
end

-------------------------------------------------------------------
class "arty_ui" (CUIScriptWnd)

function arty_ui:__init(owner,section) super()
	self.owner = owner
	self.section = section

	local ini = ini_file("plugins\\arty_container.ltx")
	self.artefact = alun_utils.collect_section(ini,"artifacts",true)
	self.container = alun_utils.collect_section(ini,"containers",true)
	self.artycon = alun_utils.collect_section(ini,"artyandcon",true)
	self:InitControls()
	self:InitCallBacks()
	last_art_spawn = 0

end

function arty_ui:__finalize()
end

function arty_ui:FillPartsList()
	self.list_box_parts:RemoveAll()

	local function fill_list(actor,itm)
		if self.container[self.section] then
			if (itm) then
				if self.artefact[itm:section()] then
					self:AddItemToList(itm,self.list_box_parts)
				end
			end
			
		elseif self.section == "container_tool" then
			if (itm) then
				if self.artycon[itm:section()] then
					self:AddItemToList(itm,self.list_box_parts)
				end
			end
		end
	end
	db.actor:iterate_inventory(fill_list,db.actor)
end

function arty_ui:InitControls()
	self:SetWndRect			(Frect():set(0,0,1024,768))

	self:SetAutoDelete(true)

	self.xml				= CScriptXmlInit()
	local ctrl
	self.xml:ParseFile			("ui_itm_main.xml")

	ctrl					= CUIWindow()
	self.xml:InitWindow			("itm_arty:file_item:main",0,ctrl)

	self.file_item_main_sz	= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())

	self.xml:InitWindow			("itm_arty:file_item:fn",0,ctrl)
	self.file_item_fn_sz	= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())

	self.xml:InitWindow			("itm_arty:file_item:fd",0,ctrl)
	self.file_item_fd_sz	= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())
	
	self.form				= self.xml:InitStatic("itm_arty:form",self)

	--self.form:SetWndPos(vector2():set(device().width/2-(self.form:GetWidth()+70), device().height/2 - self.form:GetHeight()))
	self.form:SetWndPos(vector2():set(0, 0))

	-- Background for forms
	--self.xml:InitStatic("itm_arty:form:list_background",self.form)

	if (self.use_parts) then
		--self.xml:InitStatic("itm_arty:form:list_parts_background",self.form)
	end

	-- Item picture
	self.picture			= self.xml:InitStatic("itm_arty:form:icon",self.form)
	self.picture_parts		= self.xml:InitStatic("itm_arty:form:icon_parts",self.form)
	
	--Item Used Title
	local used_name = game.translate_string(alun_utils.read_from_ini(nil,self.section,"inv_name","string","error"))
	self.caption_item_used = self.xml:InitTextWnd("itm_arty:form:caption_item_used",self.form)
	self.caption_item_used:SetText(used_name)
	
	--Parts Window Title
	self.caption_parts_title = self.xml:InitTextWnd("itm_arty:form:caption_parts_title",self.form)
	
	if self.section == "container_tool" then
		self.caption_parts_title:SetText("Контейнеры")
	else
		self.caption_parts_title:SetText("Артефакты")
	end
	
	-- Arty Container picture
	if self.section == "lead_box" then
		self.pic = self.xml:InitStatic("itm_arty:form:icon_tool_lead",self.form)
	else
		self.pic = self.xml:InitStatic("itm_arty:form:icon_tool",self.form)
	end

	local inv_grid_width = alun_utils.read_from_ini(ini,self.section,"inv_grid_width","float",0)
	local inv_grid_height = alun_utils.read_from_ini(ini,self.section,"inv_grid_height","float",0)
	local inv_grid_x = alun_utils.read_from_ini(ini,self.section,"inv_grid_x","float",0)
	local inv_grid_y = alun_utils.read_from_ini(ini,self.section,"inv_grid_y","float",0)

	local x1 = inv_grid_x*50
	local y1 = inv_grid_y*50

	local w = inv_grid_width*50
	local h = inv_grid_height*50

	local x2 = x1 + w
	local y2 = y1 + h

	local w,h = w,h
	if (utils.is_widescreen()) then
	w,h = w/1.2,h/1
	else
	w,h = w/1,h/1
	end
	self.pic:InitTexture("ui\\ui_icon_equipment")
	self.pic:SetTextureRect(Frect():set(x1,y1,x2,y2))
	self.pic:SetWndSize(vector2():set(w,h))

	-- Caption
	self.caption_parts 		= self.xml:InitTextWnd("itm_arty:form:caption_parts",self.form)
	self.list_box_parts			= self.xml:InitListBox("itm_arty:form:list_parts",self.form)
		--self.list_box_parts:SetWndPos(vector2():set(self.list_pos.x+self.list_box:GetWidth()+5, self.list_pos.y))

	local frame = self.xml:InitFrame("itm_arty:form:list_frame_parts",self.form)
		--frame:SetWndPos(vector2():set(self.list_pos.x+self.list_box:GetWidth()+5, self.list_pos.y))
	
	self.list_box_parts:ShowSelectedItem(true)
	self:Register(self.list_box_parts, "list_window_parts")
--	else
		--self.form:SetWndSize(vector2():set(self.list_box:GetWidth()+35, self.form:GetHeight()))
--	end
	
	-- Button OK
	ctrl					= self.xml:Init3tButton("itm_arty:form:btn_ok",	self.form)
	self:Register			(ctrl, "button_ok")

	-- Button Cancel
	ctrl = self.xml:Init3tButton	("itm_arty:form:btn_cancel",	self.form)
	self:Register			(ctrl, "button_back")
end

function arty_ui:InitCallBacks()
	self:AddCallback("button_ok",		ui_events.BUTTON_CLICKED,         self.OnButton_ok,			self)
	self:AddCallback("button_back",		ui_events.BUTTON_CLICKED,             self.OnButton_back_clicked,	self)

	self:AddCallback("list_window", ui_events.LIST_ITEM_CLICKED, 			  self.OnListItemClicked,		self)
	self:AddCallback("list_window", ui_events.WINDOW_LBUTTON_DB_CLICK,		  self.OnListItemDbClicked,		self)

	self:AddCallback("list_window_parts", ui_events.LIST_ITEM_CLICKED, 		  self.OnPartsListItemClicked,		self)
	self:AddCallback("list_window_parts", ui_events.WINDOW_LBUTTON_DB_CLICK,  self.OnPartsListItemDbClicked,	self)
end

function arty_ui:OnPartsListItemClicked()
	if self.list_box_parts:GetSize()==0 then return end

	local item = self.list_box_parts:GetSelectedItem()
	if not (item) then
		self.picture_parts:SetTextureRect(Frect():set(0,0,0,0))
		self.caption_parts:SetText("")
		return
	end

	local se_item = item.item_id and alife():object(item.item_id)
	if (se_item == nil or not db.actor:object(se_item:section_name())) then
		self.list_box_parts:RemoveItem(item)
		return
	end
	
	local sec = se_item:section_name()

	local w,h = item.width,item.height
	if (utils.is_widescreen()) then
	w,h = item.width/1.2,item.height/1
	else
	w,h = item.width/1,item.height/1
	end
	self.picture_parts:InitTexture("ui\\ui_icon_equipment")
	self.picture_parts:SetTextureRect(Frect():set(item.x1,item.y1,item.x2,item.y2))
	self.picture_parts:SetWndSize(vector2():set(w,h))

	if not (self.picture_parts.x) then
		local pos = self.picture_parts:GetWndPos()
		self.picture_parts.x = pos.x
		self.picture_parts.y = pos.y
	end

	self.picture_parts:SetWndPos(vector2():set(self.picture_parts.x-w/2, self.picture_parts.y-h/2))
	
	if self.section == "container_tool" then
		self.caption_parts:SetText("Изъять из контейнера")
	else
		self.caption_parts:SetText("Положить в контейнер")
	end
end

function arty_ui:OnPartsListItemDbClicked()
	self:OnButton_ok()
end

function arty_ui:OnButton_back_clicked()
	alife():create(self.section,db.actor:position(),db.actor:level_vertex_id(),db.actor:game_vertex_id(),db.actor:id())
	self:HideDialog()
end

function arty_ui:OnKeyboard(dik, keyboard_action)
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then
		if (dik == DIK_keys.DIK_RETURN) then

		elseif (dik == DIK_keys.DIK_ESCAPE) then
			self:OnButton_back_clicked()
		end
	end
	return true
end

function arty_ui:OnButton_ok()
	local break_con
	local break_arty
	
	if (self.list_box_parts) then
		index = self.list_box_parts:GetSelectedIndex()
		if (index ~= -1) then
			item = self.list_box_parts:GetItemByIndex(index)
			local se_parts = item and item.item_id and alife():object(item.item_id)
			if (se_parts) then
				sec = se_parts:section_name()
				alife():release(se_parts,true)
			else
				return
			end
		end
	end
	
	if self.section == "container_tool" then
		if (string.find(sec, "(lead.-_box)",3)) then
			break_con = "lead_box"
			break_arty = sec:gsub("_lead_box", "")		
		elseif (string.find(sec, "(af.-_iam)",3)) then  --Yasti. Searching for container section in string
			break_con = "af_iam"
			break_arty = sec:gsub("_af_iam", "")
		elseif (string.find(sec, "(af.-_aac)",3)) then
			break_con = "af_aac"
			break_arty = sec:gsub("_af_aac", "")
		elseif (string.find(sec, "(af.-_aam)",3)) then
			break_con = "af_aam"
			break_arty = sec:gsub("_af_aam", "")
		end
		
		alife():create(break_con, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		local se_obj = alife():create(break_arty, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
		last_art_spawn = se_obj.id
	else
		local new_item = tostring(sec.."_"..self.section)
		alife():create(new_item, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id())
	end
	if not db.actor:object("container_tool") then
		axr_misery.create_items("container_tool")
	end
	self:HideDialog()

	-- ponney68 added actor_effects
	if self.section == "container_tool" then
		if (actor_effects.use_item(self.section.."_"..break_con.."_dummy")) then
			actor_effects.use_item(self.section.."_"..break_con.."_dummy")
			level_weathers.get_weather_manager():forced_weather_change()
		end
	else
		if (actor_effects.use_item(self.section.."_dummy")) then
			actor_effects.use_item(self.section.."_dummy")
			level_weathers.get_weather_manager():forced_weather_change()
		end
	end
end

function arty_ui:AddItemToList(item,listbox,condition)
	local _itm			= load_item(self.file_item_main_sz.y)
	local sec = item and item:section()
	local inv_name 		= item and game.translate_string(alun_utils.read_from_ini(nil,sec,"inv_name","string","error")) or "none"

	_itm:SetWndSize		(self.file_item_main_sz)

	_itm.fn:SetWndPos(vector2():set(0,0))
	_itm.fn:SetWndSize	(self.file_item_fn_sz)
	_itm.fn:SetText		(inv_name)
	
	if (item) then

		_itm.item_id = item:id()
		if condition then
			_itm.fage     = _itm:AddTextField(condition, self.file_item_fd_sz.x)
		end
		if self.section == "container_tool" then
			if (string.find(sec, "(af.-_iam)",3)) then  --Yasti. Searching for container section in string
				sec = sec:gsub("_af_iam", "")
			elseif (string.find(sec, "(af.-_aac)",3)) then
				sec = sec:gsub("_af_aac", "")
			elseif (string.find(sec, "(af.-_aam)",3)) then
				sec = sec:gsub("_af_aam", "")
			elseif (string.find(sec, "(lead.-_box)",3)) then
				sec = sec:gsub("_lead_box", "")
			end
		end
		
		local inv_grid_width = alun_utils.read_from_ini(ini,sec,"inv_grid_width","float",0)
		local inv_grid_height = alun_utils.read_from_ini(ini,sec,"inv_grid_height","float",0)
		local inv_grid_x = alun_utils.read_from_ini(ini,sec,"inv_grid_x","float",0)
		local inv_grid_y = alun_utils.read_from_ini(ini,sec,"inv_grid_y","float",0)

		_itm.x1 = inv_grid_x*50
		_itm.y1 = inv_grid_y*50

		_itm.width = inv_grid_width*50
		_itm.height = inv_grid_height*50

		_itm.x2 = _itm.x1 + _itm.width
		_itm.y2 = _itm.y1 + _itm.height
	end

	--[[
	_itm.picture = self.xml:InitStatic("itm_arty:form:picture",self.form)
	_itm.picture:InitTexture("ui\\ui_icon_equipment")
	_itm.picture:SetTextureRect(Frect():set(_itm.x1,_itm.y1,_itm.x2,_itm.y2))
	_itm.picture:SetWndSize(vector2():set(inv_grid_width*50,inv_grid_height*50))
	--]]

	listbox:AddExistingItem(_itm)
end

function del_art_spawn()
	if last_art_spawn ~= 0 then
		last_art_spawn = 0
	end
end