#include "ui_defs.lua"

function Ui.TextInput(obj)
	local textInput = Ui.Frame
	{
		focus = false,
		hideCursor = false,
        keyBoardShortcut = "mmb";

		isGamepad = Ui.bind():call(
            function()
                return LastInputDevice() == UI_DEVICE_GAMEPAD
            end),

		background = Ui.RectOutline
		{
            id = "background",
            thickness = 2,
            cornerRadius = 6,
			color = { 1, 1, 1, 0.5 },

            resizeToParent = true
		},

		placeholder = Ui.Text
		{
			margin = {left = 8},
			vAlign = Ui.ContentLayout.Vertical.Middle,
			text = "Input text",
			color = { 1, 1, 1, 0.5},
            visible = Ui.bind().parent.empty
		},

		data =
		{
			text = ""
		},

		clearButton = Ui.Image
		{
			vAlign = Ui.V.Middle,
			hAlign = Ui.H.Right,
			margin = { right = 10 },
            path = "data/ui/common/cross_ico.png",
            color = { 1, 1, 1, 0.6 },

            visible = Ui.bind():call(
                function() 
                    return LastInputDevice() ~= UI_DEVICE_GAMEPAD 
                end):band(Ui.bind().parent.empty:bnot()),

			onMouseEnter = function(self)
				self.color = 0xFFFF7F
			end,

			onMouseLeave = function(self)
				self.color = { 1, 1, 1, 0.6 }
			end,

			onPressed = function(self)
				self.parent.input:clear()
			end,
		},

		input = Ui.Object:new
		{           
			maxLength = -1,
			color = { 1, 1, 1 },

            w = Ui.bind().parent.w,
            h = Ui.bind().parent.h,

			empty = function(self)
				return UiGetSymbolsCount(self.parent.data.text) == 0
			end,

			clear = function(self)
				self.parent.data.text = ""
			end,

			draw = function(self)
                UiPush()
				Ui.Color(self.color)
				Ui:Font("regular.ttf", 22)
                UiTextInputKeyBoardShortCutKey(self.parent.keyBoardShortcut)
				local txt = UiTextInput(self.parent.data.text, self.w, self.h, self.parent.focus, self.parent.hideCursor)

				if self.maxLength ~= -1 and UiGetSymbolsCount(txt) > self.maxLength then
					txt = UiTextSymbolsSub(txt, 1, self.maxLength)
				end

                local txtWidth = UiGetTextSize(txt)
                local clearBtnWidth = (self.parent.clearButton.visible and (self.parent.clearButton.w + self.parent.clearButton.margin.right) or 0)
                local maxTxtWidth = self.w - 10 - clearBtnWidth

                if txt ~= self.parent.data.text and txtWidth <= maxTxtWidth then
                    self.parent.data.text = txt
                    Ui._ObjectHelpers.call(self.parent, "onTextUpdated", txt)
                end

                UiPop()
			end,
		},

        setText = function(self, newText)
            self.data.text = newText
        end,

        getText = function(self)
            return tostring(self.data.text)
        end,

		empty = function(self)
			return self.input:empty()
		end,

		deleteLastCharacter = function(self)
			local len = UiGetSymbolsCount(self.data.text)
			if len > 0 then
				self.data.text = UiTextSymbolsSub(self.data.text, 1, len - 1)
			end
		end
	}

	return textInput:new(obj)
end
