-- Misery 2 Class selector
-- Alundaio
--[[
Copyright (C) 2012 Alundaio
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License-]]
--]]
class "class_ui" (CUIScriptWnd)
function class_ui:__init(owner) super()
	self.owner = owner
	self:InitControls()
	self:InitCallBacks()
end

function class_ui:__finalize() end

function class_ui:InitControls()
	self:SetWndRect(Frect():set(0,0,1024,768))
	self:Enable(true)

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

	-- Profile Pictures
	self.class = {}
	self.class.assaulter = xml:InitStatic("uss_assaulter", self)
	self.class.recon = xml:InitStatic("uss_recon", self)
	self.class.sniper = xml:InitStatic("uss_sniper", self)
	self.class.loadout = xml:InitStatic("loadout", self)

	self.class["assaulter"]:Show(false)
	self.class["recon"]:Show(false)
	self.class["sniper"]:Show(false)
	self.class["loadout"]:Show(false)

	self.class[axr_misery.ActorClass]:Show(true)

	self.current_class = axr_misery.ActorClass

	-- Message Box
	self.selector_mb = CUIMessageBoxEx()
	self:Register(self.selector_mb,"msg_box")

	-- Dialog Tab
	self.tab = xml:InitTab("tab", self)
	self:Register(self.tab, "tab")

	self.tab:SetActiveTab(axr_misery.ActorClass)

	-- Accept button
	self.accept = xml:Init3tButton("btn_accept", self)
	self:Register(self.accept,"btn_accept")

	-- Back button
	local button = xml:Init3tButton("btn_back", self)
	self:Register(button,"btn_back")
end

function class_ui:InitCallBacks()
	self:AddCallback("tab", ui_events.TAB_CHANGED, self.OnTabChange, self)
	self:AddCallback("btn_accept", ui_events.BUTTON_CLICKED, self.OnAccept, self)
	self:AddCallback("btn_back", ui_events.BUTTON_CLICKED, self.OnQuit, self)
	self:AddCallback("msg_box", ui_events.MESSAGE_BOX_YES_CLICKED, self.OnMsgYes, self)
end

function class_ui:OnMsgYes()
	if (self.current_class ~= nil or self.current_class ~= "") then
		axr_misery.set_actor_class(self.current_class)
		local console = get_console()
		console:execute("quit")
	end
end

function class_ui:OnAccept()
	if (self.current_class ~= axr_misery.ActorClass) then
		self.selector_mb:InitMessageBox("message_box_yes_no")
		self.selector_mb:SetText("Для смены класса необходимо перезагрузить игру. Желаете продолжить?")
		self.selector_mb:ShowDialog(true)
	else
		self.selector_mb:InitMessageBox("message_box_ok")
		self.selector_mb:SetText("Данный класс уже выбран.")
		self.selector_mb:ShowDialog(true)
	end
end

function class_ui:OnLoadout()
	self.loadout:Show(true)
end

function class_ui:OnQuit()
	self:HideDialog()
	self.owner:ShowDialog(true)
	self.owner:Show(true)
end

function class_ui:OnTabChange()
	local ids = self.tab:GetActiveId()

	if not (self.class[ids]) then
		return
	end

	for k,v in pairs(self.class) do
		v:Show(false)
	end

	if (ids == "loadout") then
		self.accept:Show(false)
	else
		self.current_class = ids
		self.accept:Show(true)
	end

	self.class[ids]:Show(true)
end

function class_ui:OnKeyboard(dik, keyboard_action)
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	if keyboard_action == ui_events.WINDOW_KEY_RELEASED then
		if dik == DIK_keys.DIK_ESCAPE then
			self:OnQuit()
		end
	end
	return true
end
