#include "ui_defs.lua"
#include "ui_layout.lua"

local Events =
{
    onSelectionChanged = "onSelectionChanged"
}

local Helpers = Ui._ObjectHelpers

function Ui.VerticalList(obj)
	local list = Ui._.ListBase
	{
		draw = function(self)
			self:_update()

			self.state.background.h = self.h
			self.navId = UiNavGroupBegin()

			UiFrameOccupy(self.w, self.h)

			UiPush()
				UiFrameSkipItem(true)
				UiList(self._listW, self.h, self.children, self.state)
			UiPop()

			UiNavGroupEnd()

			if self.state.selectedIdx ~= self.selectedIdx then
				self.selectedIdx = self.state.selectedIdx
				self.selectedItem = self.children[self.state.selectedIdx]

                if self.selectedItem then
                    Helpers.call(self, Events.onSelectionChanged, self.selectedItem)
                end
			end
		end,

		state = UiCreateListState(false),

        _onReadAttempt = function(self, k)
            if k ~= "w" then
                return self:_rGet(k)
            end

            return self._listW + (self.state.isShown and (self.state.scrollbarOffset + self.state.background.w) or 0)
        end,

        _onModifyAttempt = function(self, k, v)
            if k ~= "w" then
                self:_rSet(k, v)
                return
            end

            self._listW = v
        end
	}

	return list:new(obj)
end


function Ui.HorizontalList(obj)
	local list = Ui._.ListBase
	{
		draw = function(self)
			self:_update()

			self.state.background.w = self.w
			self.navId = UiNavGroupBegin()

			UiFrameOccupy(self.w, self.h)

			UiPush()
				UiFrameSkipItem(true)
				UiList(self.w, self._listH, self.children, self.state)
			UiPop()

			UiNavGroupEnd()

			if self.state.selectedIdx ~= self.selectedIdx then
				self.selectedIdx = self.state.selectedIdx
				self.selectedItem = self.children[self.state.selectedIdx]

                if self.selectedItem then
				    Helpers.call(self, Events.onSelectionChanged, self.selectedItem)
                end
			end
		end,

		state = UiCreateListState(true),

        _onReadAttempt = function(self, k)
            if k ~= "h" then
                return self:_rGet(k)
            end

            return self._listH + (self.state.isShown and (self.state.scrollbarOffset + self.state.background.h) or 0)
        end,

        _onModifyAttempt = function(self, k, v)
            if k ~= "h" then
                self:_rSet(k, v)
                return
            end

           self._listH = v
        end
	}

	return list:new(obj)
end


Ui._ = {}
function Ui._.ListBase(obj)
	local ListUiWindow = CreateUiWindow()
	local list = Ui.Object:new{
		selectedItem = nil,
		selectedIdx = 0,
		children = {},
		state = {},
		navId = "",

		showScrollbar = true,

		onSelectionChanged = function(self, it)
		end,

		scroll = function(self, s)
			local offset = -self.state.contentSpan * s
			self.state:addOffset(offset)
			self.state.navIdx = 0
			self.state.forceIdx = 0
		end,

        scrollToItem = function(self, itemIdx, doLerp)
            self.state:scrollToItem(itemIdx, doLerp)
        end, 

		appendMany = function(self, items)
			for i=1,#items do
				self:append(items[i])
			end
		end,

		clear = function(self)
			self.children = {}
			self.selectedItem = nil
			self.selectedIdx = -1
		end,

		_update = function(self)
			self.state.forceHideScrollbar = not self.showScrollbar
			self.window = ListUiWindow(self.w, self.h)
		end
	}

	return list:new(obj)
end