#include "ui_defs.lua"
#include "ui/components/common_components.lua"

-- Usage example
--		Ui.Stepper
--		{
--			w = 800,
--			label = "Sounds",
--			params =
--			{
--				allValues = {0, 1},
--				headers = {'Off', 'On'},
--			},
--		}

function Ui.Stepper(obj)
	local stepper = Ui.Object:new
	{
		w = 600,
		h = 50,
		label = "label",

		params =
		{
			currentIdx = 1,
			allValues = {},
			headers = {},
			descriptions = {},
			isCyclic = false,
			canChange = true,

			onNext = function(self)
				self.currentIdx = self.currentIdx + 1
				if self.currentIdx > #self.allValues then
					self.currentIdx = 1
				end
			end,

			onPrev = function(self)
				self.currentIdx = self.currentIdx - 1
				if self.currentIdx < 1 then
					self.currentIdx = #self.allValues
				end
			end,

			getCurrentValue = function(self)
				return self.allValues[self.currentIdx]
			end,
		},

		draw = function(self)
			UiPush()
			local navId = UiNavGroupBegin()

			local p2 = nil
			UiNavGroupSize(self.w, self.h)
			local isFocused = UiReceivesInput() and (UiIsMouseInRect(self.w, self.h) or UiIsComponentInFocus(navId))
			if isFocused then
				if p2 ~= nil then
					UiPush()
						UiTranslate(p2.x, p2.y)
						drawTooltip(p2)
					UiPop()
				end

				UiPush()
					if self.params.canChange == nil or self.params.canChange then
						UiColorFilter(1.0, 1.0, 1.0, 1)
					else
						UiColorFilter(1.0, 1.0, 1.0, 0.5)
					end
					UiPush()
						UiColor(1, 1, 1, 1)
						UiImageBox("common/rect_c#d9d9d9_o1.00_cr8.png", self.w, self.h, 9, 9)
					UiPop()
					UiPush()
						UiTranslate(12, 15)
						UiColor(1.0, 1.0, 1.0, 0.8)
						UiFont("regular.ttf", 27)
						UiText(self.label)
					UiPop()
					UiTranslate(self.w - 350, 12.0)
					drawFocusedStepper(self.params)
				UiPop()
			else
				UiPush()
					if self.params.canChange == nil or self.params.canChange then
						UiColorFilter(1.0, 1.0, 1.0, 1)
					else
						UiColorFilter(1.0, 1.0, 1.0, 0.5)
					end
					UiPush()
						UiTranslate(12, 15)
						UiColor(1.0, 1.0, 1.0, 0.8)
						UiFont("regular.ttf", 27)
						UiText(self.label)
					UiPop()
					UiTranslate(self.w - 350, 12.0)
					drawStepper(self.params)
				UiPop()
			end
			UiNavGroupEnd()
			UiPop()
		end,

		getSize = function(self)
			return self.w, self.h
		end,

		getCurrentValue = function(self)
			return self.params.allValues[self.params.currentIdx]
		end,
	}

	return stepper:new(obj)
end
