--[[
File: lwc_pda.script
Copyright: © Ирбиs , Real Wolf
Основная часть кода меток была взята из АМК 1.4.1 с переделкой для использования без GPS.
]]

local pda = nil
local rec_wnd = nil
local btn = nil

class "fake_class" (CUIScriptWnd)

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

function fake_class:__finalize() 
end


function fake_class:on_click()
	level.start_stop_menu(pda,true)
	db.actor:give_info_portion("ui_pda_hide")
end

function fake_class:on_click_metka()
	local dlg = dots(get_hud())
	level.start_stop_menu(dlg,true)
end

function on_pda_open()
	rec_wnd = fake_class()	
	pda = level.main_input_receiver()

	btn = CUI3tButton()
	btn:SetAutoDelete(true)
	btn:Init("ui\\lwr_pda\\lwr_pda_quit", 827, 0, 20, 25)
	btn:SetWidth(49)
	btn:SetHeight(25)
	pda:AttachChild(btn)
	rec_wnd:Register(btn,"btn_quit")
	rec_wnd:AddCallback("btn_quit", ui_events.BUTTON_CLICKED, rec_wnd.on_click, rec_wnd)

	btn_metka = CUI3tButton()
	btn_metka:SetAutoDelete(true)
	btn_metka:Init("pda_metka", 55, 692, 172, 27)
	btn_metka:SetWidth(172)
	btn_metka:SetHeight(27)
	btn_metka:SetFont(GetFontGraffiti22Russian())
  	btn_metka:SetText("Отметка")
   	btn_metka:SetTextAlign(CGameFont.alCenter)
   	btn_metka:SetTextY(-1)
	pda:AttachChild(btn_metka)
	rec_wnd:Register(btn_metka,"btn_metka")
	rec_wnd:AddCallback("btn_metka", ui_events.BUTTON_CLICKED, rec_wnd.on_click_metka, rec_wnd)
end

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

function on_pda_hide()
	local drop, err_msg = pcall(protected_detach, pda, btn, btn_metka)
	if (not drop) and (err_msg) then
		qqq("ERROR: lwc_pda, fail to detach buttons, reason: "..tostring(err_msg))
	end
end



class "dots" (CUIScriptWnd)

function dots:__init(owner) super()
	self.targt = objid
	self.owner = owner
	self:InitControls()
	self:InitCallBacks()
end

function dots:__finalize() end

function dots:InitControls()

	self:Init(284,300,428,165)

	local xml = CScriptXmlInit()
	xml:ParseFile("ui_dots_wnd.xml")

	xml:InitStatic("background", self)

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

	self:Register(xml:Init3tButton("btn_quit", self),"btn_quit")
	self:Register(xml:InitEditBox("edit_box",  self), "edit_box")
end

function dots:InitCallBacks()
	self:AddCallback("btn_1", ui_events.BUTTON_CLICKED, self.dots1, self)
	self:AddCallback("btn_quit", ui_events.BUTTON_CLICKED, self.on_quit, self)
end

function dots: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
			self:on_quit()
		end
		if dik == DIK_keys.DIK_RETURN then
			self:dots1()
		end
	end
	return true
end

local met = nil
local text = nil
local update = false

function dots:dots1()
	local edit_box = self:GetEditBox("edit_box")
	local a = db.actor:position()
	met = alife():create("space_restrictor", db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id())
	text = edit_box:GetText()
	update = true
	self:on_quit()
end

function dots:on_quit()
	self:GetHolder():start_stop_menu(self,true)
end


function check_metka()
	if update == true then
		if met ~= nil then
			local obj=level.object_by_id(met.id)
			if obj then
				if obj:parent()==nil then
					amk.add_spot_on_map(met.id,"metka",text)
					update = false
					met = nil
					text = nil
				end
			end
		end
	end  
end