--[[
****************************************************
****************************************************
	  ABC - Repair Toll and Suit Control Mod
****************************************************
****************************************************
--]]

local reptime = 0
local callrepair = "no"
local objrep
local objrepid
function chk_repair(obj)
if db.actor ~= nil then
local canomt = time_global()
if reptime == 0 and callrepair == "yes" then reptime = time_global() + 2000 end

  if obj ~= nil and obj ~= "load" then
  -- logf("chk_anom = " .. obj:section())
    if obj:section() == "abc_repair" then
		callrepair = "yes"
		objrep = obj
		objrepid = obj:id()
		-- logf("yes repair dropped - ") -- .. obj:name())
    end	
  end

 if canomt > reptime and callrepair == "yes" and reptime > 0 and level.present() then
	callrepair = "no"
	reptime = 0

		if level.object_by_id(objrepid) == nil then
	    	local gui_win = repair_ui(get_hud())
			level.start_stop_menu(gui_win, true)
			-- logf("calling repair ") -- .. objrep:section())
		end
 end

end
end


class "repair_ui" (CUIScriptWnd)

local showbutton = "no"
local slot1ison = "no"
local slot2ison = "no"
local slot6ison = "no"
local helmetbutton, helmetsuit, slot1name, slot2name, weapnafr, weapnbfr, suitnfr

function repair_ui:__init(owner) super()
	self.owner = owner
	self:Onload()
	self:InitControls()
	self:InitCallBacks()
end

function repair_ui:__finalize() end

function repair_ui:InitControls()
 	local screenwidth = device().width
 	local screenheight = device().height
	local midwscreen = (screenwidth/2) - 10
	local midhscreen = (screenheight/2) - 60
	if (midwscreen/midhscreen) > 1.4 then
	midwscreen = (screenwidth/2) - 10
	midhscreen = (screenheight/2) - 40
	end
	-- width  = 521, height = 438}
	local screen    = {x = 0, y = 0, width = 1024, height = 768}
	local main_rect = {x = 0, y = 0, width  = 260, height = 285}
	main_rect.x = (screen.width  - main_rect.width )/2
	main_rect.y = (screen.height - main_rect.height)/2
    self:Init(main_rect.x,main_rect.y,320,300)

	-- self:Init(midwscreen,midhscreen,300,300)
	-- self:Init(10,10,300,300)
	local xml = CScriptXmlInit()
	xml:ParseFile("ui_abc_repair.xml")

	xml:InitStatic("background", self)
	xml:InitStatic("back_video", self)

	self.msghelmetr_caption = xml:InitStatic("msghelmetr_caption",	self)
	self.msghelmetr_caption:SetText("")

	self:Register(xml:Init3tButton("caption", self),"caption")


  if db.use_suit_hud == "yes" then
	if helmetbutton == "add_helmet_id" and helmetbutton ~= "suitnohemlet" and slot6ison == "yes" then
		self.addhelmet_caption = xml:InitStatic("addhelmet_caption",	self)
		self.removehelmet_caption = xml:InitStatic("removehelmet_caption",	self)
	    self:Register(xml:Init3tButton("btn_1", self),"btn_1")
	    self.removehelmet_caption:Show(false)
	end
	if helmetbutton == "remove_helmet_id" and helmetbutton ~= "suitnohemlet" and slot6ison == "yes" then
		self.removehelmet_caption = xml:InitStatic("removehelmet_caption",	self)
		self.addhelmet_caption = xml:InitStatic("addhelmet_caption",	self)
	    self:Register(xml:Init3tButton("btn_1", self),"btn_1")
	    self.addhelmet_caption:Show(false)
	end

	if helmetsuit ~= nil and slot6ison == "yes" then
	    self:Register(xml:Init3tButton("btn_2", self),"btn_2")
		self.csuit_caption = xml:InitStatic("csuit_caption",	self)
		self.csuit_caption:SetText(helmetsuit)
	end

	if slot6ison == "yes" then 
		self.suit_caption = xml:InitStatic("suit_caption",	self)
		self.helmet_caption = xml:InitStatic("helmet_caption",	self)
	end
  end

	if slot1ison == "yes" then
		self.weapons_caption = xml:InitStatic("weapons_caption",	self) 
		self:Register(xml:Init3tButton("btn_3", self),"btn_3")
		self.slot1_caption = xml:InitStatic("slot1_caption",	self)
		self.slot1_caption:SetText(slot1name)
		
	end
	if slot2ison == "yes" then 
		self.weapons_caption = xml:InitStatic("weapons_caption",	self)
		self:Register(xml:Init3tButton("btn_4", self),"btn_4")
		self.slot2_caption = xml:InitStatic("slot2_caption",	self)
		self.slot2_caption:SetText(slot2name)
	end

	self:Register(xml:Init3tButton("btn_quit", self),"btn_quit")


	self.message_box = CUIMessageBoxEx()
	self:Register(self.message_box,"message_box")
end

function repair_ui:InitCallBacks()
	self:AddCallback("btn_1", ui_events.BUTTON_CLICKED, self.repair_ui1, self)
	self:AddCallback("btn_2", ui_events.BUTTON_CLICKED, self.repair_ui2, self)
	self:AddCallback("btn_3", ui_events.BUTTON_CLICKED, self.repair_ui3, self)
	self:AddCallback("btn_4", ui_events.BUTTON_CLICKED, self.repair_ui4, self)
	self:AddCallback("btn_quit", ui_events.BUTTON_CLICKED, self.on_quit, self)

	self:AddCallback("message_box",     ui_events.MESSAGE_BOX_YES_CLICKED,		self.OnMsgYes,						self)
	self:AddCallback("message_box",     ui_events.MESSAGE_BOX_NO_CLICKED,		self.OnMsgNo,						self)
end


function repair_ui:repair_ui1()
CUIScriptWnd.Update(self)

local chanagain = "yes"
if helmetbutton == "remove_helmet_id" then
	 self:Onload("remove") 
	 -- logf("remove")
	 chanagain = "no"
     self.removehelmet_caption:Show(false)
     self.addhelmet_caption:Show(true)
	 self.msghelmetr_caption:SetText(game.translate_string("helmet_removed_id"))

end
if helmetbutton == "add_helmet_id" and chanagain == "yes" then 
	self:Onload("add") 
	-- logf("add")
    self.addhelmet_caption:Show(false)
    self.removehelmet_caption:Show(true)
	self.msghelmetr_caption:SetText(game.translate_string("helmet_added_id"))
end

end
function repair_ui:repair_ui2()
 self:fix("suit")
end
function repair_ui:repair_ui3()
 self:fix("slot1")
-- self.message_box:Init("message_box_change_level")	
-- self:GetHolder():start_stop_menu(self.message_box, true)
end
function repair_ui:repair_ui4()
 self:fix("slot2")
end
function repair_ui:check_game()
 local check = false
 if level.present() and (db.actor ~= nil) and db.actor:alive() then
  check = true
 end
 return check
end
function repair_ui:on_quit()
 showbutton = "no"
 slot1ison = "no"
 slot2ison = "no"
 slot6ison = "no"
 alife():create("abc_repair", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id()) 
 self:GetHolder():start_stop_menu (self,true)
end

-- test
function repair_ui:OnMsgYes()
	-- logf("yes clicked")
end
function repair_ui:OnMsgNo()
	-- logf("no clicked")
end

function repair_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_ESCAPE then
			 alife():create("abc_repair", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id()) 
		end
	 end
end


function repair_ui:Onload(what)
-- tenho que detectar se helmet esta off e alterar porcetagem + 0.2 ao carregar

 local csuithud = db.actor:get_current_outfit()
 local suithudname, suithudtype, isitbino
 local ini_file_name = "abc_items_weapons_suit.ltx"
 if csuithud ~= nil then suithudname = csuithud:section() end
 if suithudname ~= nil then
  slot6ison = "yes"
  if string.find(suithudname, "exo") then showbutton = "yes"  end
  if string.find(suithudname, "gaz") then showbutton = "yes" end
  if string.find(suithudname, "antigas") then showbutton = "yes" end
  if string.find(suithudname, "scientific") then showbutton = "yes" end
  if string.find(suithudname, "ecolog") then showbutton = "yes" end
  if string.find(suithudname, "protection") then showbutton = "yes" end
  if string.find(suithudname, "nano") then showbutton = "yes" end
  if string.find(suithudname, "military") then showbutton = "yes" end
  if string.find(suithudname, "specops") then showbutton = "yes" end
  if string.find(suithudname, "killer_antigas") then showbutton = "yes" end

  if what == "remove" then db.storage[db.actor:id()].pstor[csuithud:id().."_helmet"] = "off" end
  if what == "add" then db.storage[db.actor:id()].pstor[csuithud:id().."_helmet"] = "on" end

  local ishelmon = db.storage[db.actor:id()].pstor[csuithud:id().."_helmet"]

  local	suitsc
  if ishelmon == "off" then suitsc = 0.2 else suitsc = 0 end

  if showbutton == "yes" and (ishelmon == "on" or ishelmon == nil) then
   helmetbutton = "remove_helmet_id"
   helmetsuit = suithudname
  elseif showbutton == "yes" and ishelmon == "off" then
   helmetbutton = "add_helmet_id"
   helmetsuit = suithudname
  else
   helmetbutton = "suitnohemlet"
   helmetsuit = suithudname
  end
  local persuit = (csuithud:condition() + suitsc) * 100
  persuit = math.floor(persuit)

  -- logf("helmetsuit = " .. helmetsuit .. " - " .. game.translate_string("remove_helmet_id"))
  local tcuit = csuithud:id()
  -- if tcsuit ~= nil then logf("suit id = " .. tcsuit) end

   local namefound = "no"	
   local ini_section_name 	  = suithudname
   local suit_settings 		  = ini_file(ini_file_name)
   if suit_settings ~= nil then
	if suit_settings:section_exist(ini_section_name) then
		suithudname = suit_settings:r_string(ini_section_name, "inv_name_short")
		namefound = "yes"
		suitnfr = game.translate_string(suithudname)
	    helmetsuit = "[" .. game.translate_string(suithudname) .. ": " .. persuit .. "%]"
	end  
   end
 end

 local item_in_slot_1 = db.actor:item_in_slot(1)
 local item_in_slot_2 = db.actor:item_in_slot(2)
 if item_in_slot_1 ~= nil then 
    local weapname = item_in_slot_1:section()
	if not (string.find(weapname, "af_") or string.find(weapname, "nife") or string.find(weapname, "knive")) then
		slot1ison = "yes" 
	    local ini_section_name 	= weapname
	    local weap_settings 	= ini_file(ini_file_name)
	   if weap_settings ~= nil then
		if weap_settings:section_exist(ini_section_name) then
			weapname = weap_settings:r_string(ini_section_name, "inv_name_short")		
            weapnafr = game.translate_string(weapname)
		end  
	   end
	end
	local perweap = item_in_slot_1:condition() * 100
	perweap = math.floor(perweap)
	if weapname ~= nil then
		slot1name = "[" .. perweap .. "%] " .. game.translate_string(weapname)
	else
		slot1name = "[" .. perweap .. "%] " .. game.translate_string("repair_slot1")
	end

 end
 if item_in_slot_2 ~= nil then 
      local weapname = item_in_slot_2:section()
	  slot2ison = "yes" 
	  local ini_section_name 	  = weapname
	  local weap_settings 		  = ini_file(ini_file_name)
	  if weap_settings ~= nil then
		if weap_settings:section_exist(ini_section_name) then
		 weapname = weap_settings:r_string(ini_section_name, "inv_name_short")
         weapnbfr = game.translate_string(weapname)
	    end  
	 end
	local perweap = item_in_slot_2:condition() * 100
	-- logf(perweap .. " - " .. item_in_slot_2:condition())
	perweap = math.floor(perweap)
	if weapname ~= nil then
		slot2name = "[" .. perweap .. "%] " .. game.translate_string(weapname)
	else
		slot2name = "[" .. perweap .. "%] " .. game.translate_string("repair_slot2")
	end

 end

end

function repair_ui:fix(what)
-- logf("what = " .. what)
local current_suit = db.actor:get_current_outfit()
local act_rank = ranks.get_obj_rank_name(db.actor)
local item_in_slot_1 = db.actor:item_in_slot(1)
local item_in_slot_2 = db.actor:item_in_slot(2)
local item_in_slot_6 = db.actor:item_in_slot(6)
local isrepaired, suitsc
if act_rank ~= nil then
	if what == "suit" then
		 if current_suit ~= nil then 
			 if db.storage[db.actor:id()].pstor[current_suit:id().."_helmet"] == "off" then suitsc = 0.2 else suitsc = 0 end

			 if act_rank == "novice" and ((current_suit:condition() + suitsc)*100) < 70 then
				 current_suit:set_condition(0.70) 
				 isrepaired = "yes"
			 end
			 if act_rank == "experienced" and ((current_suit:condition() + suitsc)*100) < 75 then
				 current_suit:set_condition(0.75)
				 isrepaired = "yes"
			 end
			 if act_rank == "veteran" and ((current_suit:condition() + suitsc)*100) < 85 then
				 current_suit:set_condition(0.85) 
				 isrepaired = "yes"
			 end			
			 if act_rank == "master" and ((current_suit:condition() + suitsc)*100) < 95 then
				 current_suit:set_condition(0.95) 
				 isrepaired = "yes"
			 end			
				 local persuit = current_suit:condition() * 100
				 persuit = math.floor(persuit)
			 if isrepaired == "yes" then
				 self.msghelmetr_caption:SetText(game.translate_string("suit_repaired_id") .. persuit .. "%")
				 if suitnfr ~= nil then self.csuit_caption:SetText("[" .. suitnfr .. ": " .. persuit .. "%]") end
				 self.csuit_caption:SetTextColor(255,255,0,0)
				 local csuitname = current_suit:name()
				 if db.storage[db.actor:id()].pstor[current_suit:id().."_helmet"] == "off" then
     				local stncfsuit = current_suit:condition() - 0.2
				    current_suit:set_condition(stncfsuit)
				 end 							
			 else
				if current_suit:condition() == 1 then
				 self.msghelmetr_caption:SetText(game.translate_string("suit_noneedrepair_id"))								 
				else
				 self.msghelmetr_caption:SetText(game.translate_string("suit_cantrepair_id"))				
				end
			 end
		 end
	end
	if what == "slot1" then
		if item_in_slot_1 ~= nil then
    	   if act_rank == "novice" and (item_in_slot_1:condition()*100) < 65 then
			 item_in_slot_1:set_condition(0.65) 
			 isrepaired = "yes"
			end
	       if act_rank == "experienced" and (item_in_slot_1:condition()*100) < 75 then
			 item_in_slot_1:set_condition(0.75)
			 isrepaired = "yes"
		   end
	   	   if act_rank == "veteran" and (item_in_slot_1:condition()*100) < 85 then
			 item_in_slot_1:set_condition(0.85) 
			 isrepaired = "yes"
		   end
	   	   if act_rank == "master" and (item_in_slot_1:condition()*100) < 90 then
			 item_in_slot_1:set_condition(0.90)
			 isrepaired = "yes"
		   end
			 local perweap = item_in_slot_1:condition() * 100
			 perweap = math.floor(perweap)
			 if isrepaired == "yes" then
		         if weapnafr ~= nil then self.slot1_caption:SetText("[" .. perweap .. "%] " .. weapnafr) end
				 self.slot1_caption:SetTextColor(255,255,0,0)
				 self.msghelmetr_caption:SetText(game.translate_string("weapon_repaired_id") .. perweap .. "%")
			 else
				if item_in_slot_1:condition() == 1 then
				 self.msghelmetr_caption:SetText(game.translate_string("weapon_noneedrepair_id"))								 
				else
				 self.msghelmetr_caption:SetText(game.translate_string("weapon_cantrepair_id"))				
				end
			 end
		end
	end
	if what == "slot2" then
		if item_in_slot_2 ~= nil then
    	   if act_rank == "novice" and (item_in_slot_2:condition()*100) < 65 then
			 item_in_slot_2:set_condition(0.65)
			 isrepaired = "yes"
		   end
	       if act_rank == "experienced" and (item_in_slot_2:condition()*100) < 75 then
			 item_in_slot_2:set_condition(0.75)
			 isrepaired = "yes"
		   end
	   	   if act_rank == "veteran" and (item_in_slot_2:condition()*100) < 85 then
			 item_in_slot_2:set_condition(0.85)
			 isrepaired = "yes"
		   end
	   	   if act_rank == "master" and (item_in_slot_2:condition()*100) < 90 then
			 item_in_slot_2:set_condition(0.90)
			 isrepaired = "yes"
		   end
			 local perweap = item_in_slot_2:condition() * 100
			 perweap = math.floor(perweap)
			 if isrepaired == "yes" then
		         if weapnbfr ~= nil then self.slot2_caption:SetText("[" .. perweap .. "%] " .. weapnbfr) end
				 self.slot2_caption:SetTextColor(255,255,0,0)
				 self.msghelmetr_caption:SetText(game.translate_string("weapon_repaired_id") .. perweap .. "%")
			 else
				if item_in_slot_2:condition() == 1 then
				 self.msghelmetr_caption:SetText(game.translate_string("weapon_noneedrepair_id"))								 
				else
				 self.msghelmetr_caption:SetText(game.translate_string("weapon_cantrepair_id"))				
				end
			 end
		end
	end
end
end

