-- ui_loot
-- Alundaio
-- ponney68
--[[
   Copyright (C) 2012 Alundaio

   This program is free software; you can redistribute and/or modify
   it under the terms of the Open S.T.A.L.K.E.R. Mod License version 1.0.
-]]
-------------------------------------------------------------------
-- This table based on "species" of mutants 
killed_mutant_tbl =
	{
	cat				= {file="ui\\ui_actor_monsters_pda_2",x="0",y="0",type="small"},
	fracture			= {file="ui\\ui_actor_monsters_pda_2",x="393",y="200",type="human"},
	bird				= {file="ui\\ui_actor_monsters_pda_2",x="393",y="0",type="small"},
	zombie				= {file="ui\\ui_actor_monsters_pda_2",x="0",y="200",type="human"},

	snork				= {file="ui\\ui_actor_monsters_pda",x="393",y="0",type="human"},
	dog				= {file="ui\\ui_actor_monsters_pda",x="0",y="800",type="small"},
	pseudodog			= {file="ui\\ui_actor_monsters_pda",x="393",y="200",type="small"},
	psy_dog				= {file="ui\\ui_actor_monsters_pda",x="393",y="200",type="small"},
	poltergeist			= {file="ui\\ui_actor_monsters_pda",x="0",y="400",type="small"},
	bloodsucker			= {file="ui\\ui_actor_monsters_pda",x="393",y="400",type="human"},
	controller			= {file="ui\\ui_actor_monsters_pda",x="393",y="800",type="human"},
	chimera				= {file="ui\\ui_actor_monsters_pda",x="0",y="600",type="large"},
	tushkano			= {file="ui\\ui_actor_monsters_pda",x="0",y="0",type="small"},
	rat				= {file="ui\\ui_actor_monsters_pda",x="0",y="0",type="small"},
	flesh				= {file="ui\\ui_actor_monsters_pda",x="393",y="600",type="large"},

	burer				= {file="ui\\ui_actor_monsters_pda_1",x="0",y="0",type="large"},
	boar				= {file="ui\\ui_actor_monsters_pda_1",x="393",y="0",type="large"},
	giant				= {file="ui\\ui_actor_monsters_pda_1",x="0",y="200",type="large"},
	}

-- Sound table for randomize
small_mutant_snd_tbl =
	{
	small_1	= {sound="inv_mutant_loot_smaller"},
	small_2	= {sound="inv_mutant_loot_animal2"},
	small_3	= {sound="inv_mutant_loot_crow"},
	}

large_mutant_snd_tbl =
	{
	large_1	= {sound="inv_mutant_loot_animal"},
	large_2	= {sound="inv_mutant_loot_grease"},
	large_3	= {sound="inv_mutant_loot_grease2"},
	}

human_mutant_snd_tbl =
	{
	human_1	= {sound="inv_mutant_loot_human"},
	human_2	= {sound="inv_mutant_loot_human2"},
	human_3	= {sound="inv_mutant_loot_rotten"},
	}
--------------------------------------------------------------------
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 "loot_ui" (CUIScriptWnd)

function loot_ui:__init(owner,id,section,clsid) super()
	self.owner = owner
	self.section = section
	self.clsid = clsid
	self.id = id

	self:InitControls()
	self:InitCallBacks()

	self:FillList()
end

function loot_ui:__finalize()
end

function loot_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			("loot:file_item:main",0,ctrl)

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

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

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

	self.form				= self.xml:InitStatic("loot:form",self)
	--self.form:SetWndPos(vector2():set(device().width/4-(self.form:GetWidth()/4), device().height/2 - self.form:GetHeight()))

	-- Item Icon
	self.picture 			= self.xml:InitStatic("loot:form:icon",self)


	-- List Box
	self.xml:InitFrame			("loot:form:list_frame",self.form)

	self.list_box			= self.xml:InitListBox("loot:form:list",self.form)

	self.list_box:ShowSelectedItem	(true)
	self:Register			(self.list_box, "list_window")

	-- Button OK
	ctrl = self.xml:Init3tButton("loot:form:btn_ok",self.form)
	self:Register(ctrl, "button_ok")
end

function loot_ui:SetMutantImageAndSound()
	self.mutant_id = game.translate_string(alun_utils.read_from_ini(nil,self.section,"species","string",nil))

	local mutant_f = "ui\\ui_actor_monsters_pda_1"
	local mutant_x = 0
	local mutant_y = 0
	
	mutant_f = tostring(killed_mutant_tbl[self.mutant_id].file)
	mutant_x = tostring(killed_mutant_tbl[self.mutant_id].x)
	mutant_y = tostring(killed_mutant_tbl[self.mutant_id].y)

	local x1 = mutant_x
	local y1 = mutant_y
	local mutant_width = 393
	local mutant_height = 200

	local x2 = x1 + mutant_width
	local y2 = y1 + mutant_height

	self.image			= self.xml:InitStatic("loot:form:image",self.form)
	self.image:InitTexture(tostring(mutant_f))
	self.image:SetTextureRect(Frect():set(x1,y1,x2,y2))

	-- For randomize
	math.randomseed (device():time_global())
	self.rnd = math.random(3)
	-- TEST
	--self.caption_icon 		= self.xml:InitTextWnd("loot:form:caption",self.form)
	--self.caption_icon:SetText(tostring(self.rnd))

	actor_effects.use_weapon(0)
end
	
function loot_ui:InitCallBacks()
	self:AddCallback("button_ok",ui_events.BUTTON_CLICKED,self.OnButton_ok,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)
end

function loot_ui:FillList()
	self.list_box:RemoveAll()

	self.loot = {}
	itms_manager.loot_mutant(nil,self.clsid,self.loot)

	local is_there_loot
	for sec,t in pairs(self.loot) do
		self:AddItemToList(sec,t.count)
		is_there_loot = true
	end

	if (is_there_loot) then
		self:ShowDialog(true)

		-- Mutant image and looting sound
		self:SetMutantImageAndSound()
	else
		local hud = get_hud()
		hud:AddCustomStatic("can_use_weapon_now", true)
		hud:GetCustomStatic("can_use_weapon_now"):wnd():TextControl():SetTextST("Ничего полезного...")
		itms_manager.ShowMessage = true
		itms_manager.ShowMessageInit = game.get_game_time()
		itms_manager.ShowMessageTime = 20
	end
end

function loot_ui:OnListItemClicked()
	if self.list_box:GetSize()==0 then return end

	local item = self.list_box:GetSelectedItem()

	if not (item) then
		return
	end

	local sec = item.sec
	local obj
	local ret = true
	for i=1,self.loot[sec].count do
		obj = self.loot[sec][i]
		if (obj) then
			ret = nil
		end
	end

	if (ret) then
		self.list_box:RemoveItem(item)
		return
	end

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

	if not (self.picture.x) then
		local pos = self.picture:GetWndPos()
		local posform = self.form:GetWndPos()
		self.picture.x = pos.x + posform.x
		self.picture.y = pos.y + posform.y
	end
	self.picture:SetWndPos(vector2():set(self.picture.x-w/2, self.picture.y-h/2))
	-- TEST
	--self.caption_icon 		= self.xml:InitTextWnd("loot:form:caption",self.form)
	--self.caption_icon:SetText(tostring(math.random(3)))
end


function loot_ui:OnListItemDbClicked()

end

function loot_ui:OnButton_ok()
	self:HideDialog()

	-- Randomized looting sounds
	local mutant_t = tostring(killed_mutant_tbl[self.mutant_id].type)
	local mutant_s = nil
	if (mutant_t=="small") then
	mutant_s = tostring(small_mutant_snd_tbl["small_"..self.rnd].sound)
	elseif (mutant_t=="large") then
	mutant_s = tostring(large_mutant_snd_tbl["large_"..self.rnd].sound)
	elseif (mutant_t=="human") then
	mutant_s = tostring(human_mutant_snd_tbl["human_"..self.rnd].sound)
	end
	xr_sound.set_sound_play(db.actor:id(),mutant_s)

	actor_effects.use_item("mutant_looting")
end

function loot_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_ok()
		end
	end
	return true
end

function loot_ui:AddItemToList(sec,count)
	local _itm			= load_item(self.file_item_main_sz.y)
	local inv_name 		= 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)

	_itm.fage			= _itm:AddTextField("x"..count, self.file_item_fd_sz.x)
	_itm.fage:SetFont	(GetFontLetterica16Russian())
	_itm.fage:SetWndPos	(vector2():set(self.file_item_fn_sz.x+4, 0))
	_itm.fage:SetWndSize(self.file_item_fd_sz)

	_itm.sec = sec

	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

	--[[
	_itm.picture = self.xml:InitStatic("loot:form:icon",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))
	--]]

	self.list_box:AddExistingItem(_itm)
end
