--[[ ----------------------------------------------------------------------------------------------
 File       : pdaSkills.script
 Description: PDA: "Skills"
 Copyright  : 2009 © LA-team
 Author     : Gr1ph00n (19.04.2009)
 Editors    : gr1ph00n, Artos, SkyLoader
 Last edit  : 04.08.2016 [SkyLoader]
--]] ----------------------------------------------------------------------------------------------

--[[
"barter" - "Бартер"
"endurance" - "Выносливость"
"sharpshooter" - "Снайпер"
"survivor" - "Выживание"
--]]
--/ -----------------------------------------------------------------
--/ VARIABLEs
--/ -----------------------------------------------------------------
local skills = { "barter", "endurance", "sharpshooter", "survivor" }
local skills_values = {}
for _,v in ipairs(skills) do
	if not skills_values[v] then
		skills_values[v] = 0
	end
end
local points_amount = 0
local killed_npcs = 0
local killed_mobs = 0
local time_update = time_global() + 30*1000
local ltx_skills = "lost_alpha\\la_skills.ltx"
--/ --------------------------
local sModule = script_name()
--/ -----------------------------------------------------------------
local log = function(fmt, ...) _G.log(sModule..":"..tostring(fmt), ...) end
--/ -----------------------------------------------------------------
--/ Initialization and preset module (from pdaManager.script)
--/ -----------------------------------------------------------------
function PreSets(e)
	event("actor_save"):register(this.save)
	event("actor_spawned"):register(this.spawned)
	event("npc_death"):register(this.add_killed_npcs)
	event("mob_death"):register(this.add_killed_mobs)
	this.prepare()
end
--/ -----------------------------------------------------------------
--/ Process
--/ -----------------------------------------------------------------
function prepare(active)
	local tbl = GetVar("pda_skills_sav", nil)
	if tbl and type(tbl.skills) == 'table' then
		killed_npcs = tbl.npcs or 0
		killed_mobs = tbl.mobs or 0
		points_amount = tbl.points or 0
		skills_values = tbl.skills
		for _,skill in ipairs(skills) do
			if not skills_values[skill] then
				skills_values[skill] = 0
			end
		end
	end
end

function save(e)
	local tbl = {}
	tbl.skills = table.copy(skills_values)
	tbl.points = points_amount
	tbl.npcs = killed_npcs
	tbl.mobs = killed_mobs
	SetVar("pda_skills_sav", tbl)
end

function spawned(e)
	this.prepare()

	--/ SkyLoader: load skills
	db.actor:set_actor_price_factor(get_barter_skill())
	--/ endurance
	db.actor:set_actor_max_weight(get_endurance_max_weight())
	db.actor:set_actor_walk_weight(get_endurance_walk_weight())
	db.actor:set_actor_sprint_factor(get_endurance_sprint())
	--/ sharpshooter
	db.actor:set_actor_dispersion_factor(get_sharpshooter_dispersion())
	db.actor:set_actor_zoom_inert_factor(get_sharpshooter_zoom_inertion())
	--/ survivor
	db.actor:set_actor_hit_immunity_factor(get_survivor_hit_factor())
end

function add_killed_npcs(e)
	if e.who and e.who:id() == idActor then
		killed_npcs = killed_npcs + 1
	end
end
function add_killed_mobs(e)
	if e.who and e.who:id() == idActor then
		killed_mobs = killed_mobs + 1
	end
end

--/ ------------------------------------------------------------------
function get_barter_skill() --/< from xr_conditions.script
	local min_value = Get_Cfg_Number(ltx_skills, "barter_skill_data", "minimum_value", 1)
	local max_value = Get_Cfg_Number(ltx_skills, "barter_skill_data", "maximum_value", 1)

	return min_value - (skills_values["barter"] / 10 * (min_value - max_value)) --/>
end

function get_endurance_max_weight()
	local min_value = Get_Cfg_Number(ltx_skills, "endurance_skill_data", "minimum_max_weight", 35)
	local max_value = Get_Cfg_Number(ltx_skills, "endurance_skill_data", "maximum_max_weight", 70)

	return min_value + (skills_values["endurance"] / 10 * (max_value - min_value)) --/>
end

function get_endurance_walk_weight()
	local min_value = Get_Cfg_Number(ltx_skills, "endurance_skill_data", "minimum_max_walk_weight", 40)
	local max_value = Get_Cfg_Number(ltx_skills, "endurance_skill_data", "maximum_max_walk_weight", 80)

	return min_value + (skills_values["endurance"] / 10 * (max_value - min_value)) --/>
end

function get_endurance_sprint()
	local min_value = Get_Cfg_Number(ltx_skills, "endurance_skill_data", "minimum_sprint_factor", 2.2)
	local max_value = Get_Cfg_Number(ltx_skills, "endurance_skill_data", "maximum_sprint_factor", 2.2)

	return min_value + (skills_values["endurance"] / 10 * (max_value - min_value)) --/>
end

function get_sharpshooter_dispersion()
	local min_value = Get_Cfg_Number(ltx_skills, "sharpshooter_skill_data", "minimum_dispersion", 1)
	local max_value = Get_Cfg_Number(ltx_skills, "sharpshooter_skill_data", "maximum_dispersion", 1)

	return min_value - (skills_values["sharpshooter"] / 10 * (min_value - max_value)) --/>
end

function get_sharpshooter_zoom_inertion()
	local min_value = Get_Cfg_Number(ltx_skills, "sharpshooter_skill_data", "minimum_zoom_inertion", 1)
	local max_value = Get_Cfg_Number(ltx_skills, "sharpshooter_skill_data", "maximum_zoom_inertion", 1)

	return min_value - (skills_values["sharpshooter"] / 10 * (min_value - max_value)) --/>
end

function get_survivor_hit_factor()
	local min_value = Get_Cfg_Number(ltx_skills, "survivor_skill_data", "minimum_immunity_coeff", 1)
	local max_value = Get_Cfg_Number(ltx_skills, "survivor_skill_data", "maximum_immunity_coeff", 1)

	return min_value - (skills_values["survivor"] / 10 * (min_value - max_value)) --/>
end

function get_survivor_stash_skill() --/< from treasure_manager.script
	local min_value = Get_Cfg_Number(ltx_skills, "survivor_skill_data", "minimum_stash_percent", 15)
	local max_value = Get_Cfg_Number(ltx_skills, "survivor_skill_data", "maximum_stash_percent", 40)

	return min_value + (skills_values["survivor"] / 10 * (max_value - min_value)) --/>
end

--/ ------------------------------------------------------------------
function add_points_amount(value) --/< from task_manager.script
	if has_info("pda_skills_enabled") then
		points_amount = points_amount + value
		news_manager.send_tip_hud("points_update", string.format("%s %d", game.translate_string("points_msg"), value))
	end
end

--/ ------------------------------------------------------------------
function get_reputation()
	local r = db.actor:character_reputation()
	if r >= -10000 and r <= -1000 then
		return "terrible"
	elseif r <= -150 then
		return "very_bad"
	elseif r <= -50 then
		return "bad"
	elseif r <= 50 then
		return "neutral"
	elseif r <= 150 then
		return "good"
	elseif r <= 1000 then
		return "very_good"
	end
	return "excellent"
end

function get_completed_quests()
	local sum = 0
	for _,v in pairs(task_manager.get_random_task().task_info) do
		if v.status == "completed" then
			sum = sum + 1
		end
	end
	return tostring(sum)
end

function get_wnd_tags()
	return  {
		["name"]         = db.actor:character_name(),
		["rank"]         = ranks.get_obj_rank_name(db.actor),
		["community"]    = db.actor:character_community(),
		["reputation"]   = this.get_reputation(),
		["quests"]       = this.get_completed_quests(),
		["stalkerkills"] = killed_npcs or 0,
		["monsterkills"] = killed_mobs or 0
	}
end

--/ -------------------------
--/ CLASS CONSTRUCTOR
--/ -----------------------------------------------------------------------------------------------
class "uiPdaSkills" (CUIScriptWnd)
--/ -----------------------------------------------------------------------------------------------
function uiPdaSkills:__init(name,rect,active) super()
	self:SetWindowName("pda_"..name)
	self:SetAutoDelete(false)
	self.rect = rect
	self.active = active
	self.initialized = false
	self:initialize()
	
end

function uiPdaSkills:initialize()
	self:InitControls()
	self:InitCallBacks()
	self.initialized = true
end

function uiPdaSkills:InitControls()
	--self.rect[1] = device():is_widescreen() and self.rect[1] - 15 or self.rect[1]
	self:Init( unpack(self.rect) )
	local xml = CScriptXmlInit()
	xml:ParseFile("ui_wnd\\ui_"..self:WindowName()..".xml")
	
	xml:InitFrame    ("left_frame_window", self)
	xml:InitFrameLine("left_frame_header", self)
	xml:InitFrame    ("right_frame_window", self)
	xml:InitFrameLine("right_frame_header", self)
	xml:InitFrame    ("bottom_frame_window", self)
	xml:InitFrameLine("bottom_frame_header", self)
	xml:InitAnimStatic("anim_static", self)
	self:Register(xml:InitStatic("description", self), "description")
	
	local dlg = nil
	local wnd_tags = this.get_wnd_tags()
	for k,v in pairs(wnd_tags) do
		xml:InitStatic(k.."_caption", self)
		dlg = xml:InitStatic(k.."_static", self)
		dlg:SetWindowName(k.."_static")
		dlg:SetTextST(tostring(v))
	end
	
	dlg = xml:InitButton("points_amount_caption", self)
	dlg:SetTextST("points_amount")
	self:Register(dlg, "points_amount_descr")
	dlg = xml:InitStatic("points_amount_static", self)
	dlg:SetText(tostring(points_amount))
	self:Register(dlg, "points_amount_val")
	
	for _,skill in ipairs(skills) do
		dlg = xml:Init3tButton(skill.."_icon", self)
		self:Register(dlg, skill.."_icon")
		dlg = xml:InitButton(skill.."_caption", self)
		dlg:SetTextST("st_pda_skills_"..skill)
		self:Register(dlg, skill.."_descr")
		dlg = xml:InitStatic(skill.."_static", self)
		dlg:SetText(tostring(skills_values[skill]))
		self:Register(dlg, skill.."_val")
		
		dlg = xml:InitButton(skill.."_title", self)
		self:Register(dlg, skill)
		dlg = xml:InitProgressBar(skill.."_progress", self)
		dlg:SetProgressPos(skills_values[skill])
		self:Register(dlg, skill.."_bar")
		dlg = xml:Init3tButton(skill.."_plus_btn", self)
		dlg:Enable(false)
		dlg:Show(false)
		self:Register(dlg, skill.."_plus_btn")
	end
end

function uiPdaSkills:InitCallBacks()
	self:AddCallback("points_amount_descr", ui_events.BUTTON_CLICKED, self.ShowDescription, self)
	for _,skill in ipairs(skills) do
		self:AddCallback(skill,           ui_events.BUTTON_CLICKED, function() self:ShowDescription("st_pda_skills_"..skill.."_descr") end, self)
		self:AddCallback(skill.."_icon",           ui_events.BUTTON_CLICKED, function() self:ShowDescription("st_pda_skills_"..skill.."_descr") end, self)
		self:AddCallback(skill.."_descr", ui_events.BUTTON_CLICKED, function() self:ShowDescription("st_pda_skills_"..skill.."_descr") end, self)
		self:AddCallback(skill.."_plus_btn",  ui_events.BUTTON_CLICKED, function() self:ChangeSkill(skill, 1) end, self)
	end
end

function uiPdaSkills:ChangeSkill(skill,value)
	skills_values[skill] = skills_values[skill] + value

	if skill == "barter" then
		db.actor:set_actor_price_factor(get_barter_skill())
	elseif skill == "endurance" then
		db.actor:set_actor_max_weight(get_endurance_max_weight())
		db.actor:set_actor_walk_weight(get_endurance_walk_weight())
		db.actor:set_actor_sprint_factor(get_endurance_sprint())
	elseif skill == "sharpshooter" then
		db.actor:set_actor_dispersion_factor(get_sharpshooter_dispersion())
		db.actor:set_actor_zoom_inert_factor(get_sharpshooter_zoom_inertion())
	elseif skill == "survivor" then
		db.actor:set_actor_hit_immunity_factor(get_survivor_hit_factor())
	end

	points_amount = points_amount - value
	
	self:GetStatic(skill.."_val"):SetText(tostring(skills_values[skill]))
	self:GetProgressBar(skill.."_bar"):SetProgressPos(skills_values[skill])
	self:ShowDescription("st_pda_skills_"..skill.."_descr")
	self:UpdateInfo()
end

function uiPdaSkills:UpdateInfo()
	local btn_add = nil
	for _,skill in ipairs(skills) do
		btn_add = self:GetButton(skill.."_plus_btn")
		if points_amount > 0 and skills_values[skill] < 10 then
			btn_add:Enable(true)
			btn_add:Show(true)
		else
			btn_add:Enable(false)
			btn_add:Show(false)
		end
	end

	self:GetStatic("points_amount_val"):SetText(tostring(points_amount))
	local wnd_tags = this.get_wnd_tags()
	for k,v in pairs(wnd_tags) do
		self:GetStatic(k.."_static"):SetTextST(tostring(v))
	end
end

function uiPdaSkills:ShowDescription(str)
	if not str then str = "points_amount_descr" end
	self:GetStatic("description"):SetTextST(str)
end

function uiPdaSkills:Dispatch(cmd, param)
	log("uiPdaSkills:Dispatch(%d, %d)", cmd, param)
	self:OnShow(param == 1)
	return CUIScriptWnd.Dispatch(self, cmd, param)
end

function uiPdaSkills:OnShow(show)
	if show then
		if not self.initialized then
			self:initialize()
		end
		self:UpdateInfo()
		self:ShowDescription("select_skill")
	end
end
--/ ------------------------------------------------------------------------------------------------