push_all_buttons = {}

push_all_buttons.params = 
{
	buttons = 
	{
		{	-- 1
			pos = sf.misc.FloatVector(100.0, 100.0),
			on = true,
			buttons = {3,4}
		},
		{	-- 2
			pos = sf.misc.FloatVector(200.0, 100.0),
			on = false,
			buttons = {1,3}
		},
		{	-- 3
			pos = sf.misc.FloatVector(300.0, 100.0),
			on = true,
			buttons = {1,2}
		},
		{	-- 4
			pos = sf.misc.FloatVector(400.0, 100.0),
			on = false
		}
	},
	
	sound_click = "take_put_item"
}

push_all_buttons.CButton = function (_owner, _info, _state1, _state2)
	
	local image1 = sf.gui.CImageWidget(_state1, _T(""), -1, 0)
	image1.SetOffset(_info.pos.X, _info.pos.Y)
	_owner.AddWidget(sf.gui.CImageWidgetPtrT(image1))
	
	local image2 = sf.gui.CImageWidget(_state2, _T(""), -1, 0)
	image2.SetOffset(_info.pos.X, _info.pos.Y)
	_owner.AddWidget(sf.gui.CImageWidgetPtrT(image2))
	
	return
	{
		states = {image1, image2},
		state = 1,
		on = _info.on,
		buttons = _info.buttons,
		
		SetState = function(_self, _state)
			_self.state = _state
			_self:_UpdateImage()
		end,
				
		IsContains = function(_self, _pos)
			local widget = _self.states[_self.state]
			local pos = _pos - widget.GetOffset()
			return sminigames.IsVisiblePoint(widget.GetImage(), pos.X, pos.Y)
		end,	
		
		_UpdateImage = function(_self)
			_self.states[_self.state].RemFlags(sf.gui.CWidget.FlagHidden)
			_self.states[3-_self.state].AddFlags(sf.gui.CWidget.FlagHidden)
		end
	}
end

function push_all_buttons.CheckEnd(_owner)
	for _, b in pairs(push_all_buttons.buttons) do	
		if b.state == 1 then
			return false
		end
	end
	return true
end

function push_all_buttons.OnMouseDown(_owner, _pos, _button, _state, _broadcast)
	
	for _, b in pairs(push_all_buttons.buttons) do
		if b:IsContains(_pos) then
			if b.state == 1 then
				sf.core.g_Application.GetAudioManager().Play(push_all_buttons.params.sound_click, -2, -2, -2, -2, -2)
			end
			if not(b.on == false and b.state == 2) then
				local buttons = b.buttons
				if not buttons then
					buttons = {}
					for i = 1, #push_all_buttons.buttons do
						if push_all_buttons.buttons[i] ~= b then
							table.insert(buttons, i)
						end
					end
				end	
				local state = nil
				if b.on == true then 
					state = 3 - b.state
				else 
					state = 1 
				end
				for _, i in ipairs(buttons) do
					push_all_buttons.buttons[i]:SetState(state)
				end
			end
			b:SetState(3-b.state)
			push_all_buttons.pushed = true
			return true
		end
	end
	
	return false
end

function push_all_buttons.OnMouseMove(_owner, _pos, _state, _broadcast)

	for _, b in pairs(push_all_buttons.buttons) do
		if b:IsContains(_pos) then
			sf.gui.g_Cursor.Instance().SetCursor(sf.gui.CCursor.CursorHand)
			return true
		end
	end

	return false
end

function push_all_buttons.OnUpdate(_owner)
	if push_all_buttons.pushed == true and push_all_buttons.resolved == false then
		push_all_buttons.pushed = false
		push_all_buttons.resolved = push_all_buttons.CheckEnd(_owner)
		if push_all_buttons.resolved == true then
			if push_all_buttons.widgets then
				for _, w in pairs(push_all_buttons.widgets) do
					sminigames.ImageDropBuffer(w.GetImage())
				end
				push_all_buttons.widgets = nil
			end
			_owner.SetGameResult(1)
			_owner.OnEndGame()
		end
	end
end

function push_all_buttons.OnPreferredSize(_owner)
	return sminigames.OnPreferredSize(_owner)
end

function push_all_buttons.Init(_owner)
	sminigames.SetStandartScriptName(_owner, push_all_buttons)
	
	push_all_buttons.back = __cast(_owner.GetWidget("back", false).get(), sf.gui.CImageWidget)
	assert(push_all_buttons.back)
	
	push_all_buttons.button_state1 = __cast(_owner.GetWidget("button_state1", false).get(), sf.gui.CImageWidget)
	assert(push_all_buttons.button_state1)	
	push_all_buttons.button_state1.AddFlags(sf.gui.CWidget.FlagHidden)
	
	push_all_buttons.button_state2 = __cast(_owner.GetWidget("button_state2", false).get(), sf.gui.CImageWidget)
	assert(push_all_buttons.button_state2)	
	push_all_buttons.button_state2.AddFlags(sf.gui.CWidget.FlagHidden)
	
	push_all_buttons.buttons = {}
	push_all_buttons.widgets = {}
	
	for _, b in pairs(push_all_buttons.params.buttons) do
		table.insert(push_all_buttons.widgets, push_all_buttons.button_state1)
		table.insert(push_all_buttons.widgets, push_all_buttons.button_state2)
		local button = push_all_buttons.CButton(_owner, b, push_all_buttons.button_state1.GetImage(), push_all_buttons.button_state2.GetImage())
		button:SetState(1)
		table.insert(push_all_buttons.buttons, button)
	end	
	
	push_all_buttons.pushed = false
	push_all_buttons.resolved = false
end
