#include "ui_defs.lua"

function Ui.Colorf(r,g,b,a)
	return {
		r = r,
		g = g,
		b = b,
		a = a or 1,
		set = function(self)
			UiColor(self.r,self.b,self.g,self.a)
		end
	}
end


function Ui.Rect(obj) 
	local rect = Ui.Object:new {
		w = 40,
		h = 40,
        color = {1,1,1},
        cornerRadius = 0,
        fillImagePath = nil,

		draw = function(self)
            UiPush()
                Ui.Color(self.color)

                if self.fillImagePath then
                    UiFillImage(self.fillImagePath)
                end

                if self.cornerRadius == 0 then
                    UiRect(self.w,self.h)
                else
                    UiRoundedRect(self.w, self.h, self.cornerRadius)
                end
            UiPop()
		end
	}

	return rect:new(obj)
end


function Ui.RectOutline(obj) 
    local outline = Ui.Object:new {
        w = 40,
        h = 40,
        color = {1,0,0},
        thickness = 1,
        cornerRadius = 0,

        draw = function(self)
            UiPush()
                Ui.Color(self.color)
                if self.cornerRadius == 0 then
                    UiRectOutline(self.w,self.h, self.thickness)
                else
                    UiRoundedRectOutline(self.w, self.h, self.cornerRadius, self.thickness)
                end
            UiPop()
        end
    }

    return outline:new(obj)
end


function Ui.Circle(obj)
    local cirlce = Ui.Object:new {
        radius = 20,
        color = { 1, 1, 1 },

        draw = function(self)
            self.w = self.radius * 2
            self.h = self.radius * 2

            UiPush()
                Ui.Color(self.color)
                UiCircle(self.radius)
            UiPop()
        end,

        getSize = function(self)
            self:resolveBinds()
            self.w = self.radius * 2
            self.h = self.radius * 2
            return self.w, self.h
        end
    }

    return cirlce:new(obj)
end


function Ui.CircleOutline(obj)
    local outline = Ui.Object:new {
        radius = 20,
        color = { 1, 1, 1 },
        thickness = 1,

        draw = function(self)
             self.w = self.radius * 2
            self.h = self.radius * 2

            UiPush()
                Ui.Color(self.color)
                UiCircleOutline(self.radius, self.thickness)
            UiPop()
        end,

        getSize = function(self)
            self:resolveBinds()
            self.w = self.radius * 2
            self.h = self.radius * 2
            return self.w, self.h
        end
    }

    return outline:new(obj)
end


function Ui.ImageBox(obj)
	local imageBox = Ui.Object:new {
		path = "",
        border = {  width = 0, height = 0 },
        color = 0xFFFFFFFF,

		draw = function(self)
			UiPush()
				Ui.Color(self.color)
				UiImageBox(self.path, self.w, self.h, self.border.width, self.border.height)
			UiPop()
		end,

		onCreate = function(self)
		end
	}

	return imageBox:new(obj)
end


function Ui.Image(obj)
	local img = Ui.Object:new {
		color = 0xFFFFFFFF,
        path = "",
		
		draw = function(self)
			UiPush()
				local iw, ih = UiGetImageSize(self.path)
				local scaleX, scaleY = self.w / iw, self.h / ih
				UiScale(scaleX, scaleY)
                Ui.Color(self.color)
				UiImage(self.path)
			UiPop()
		end,

		getSize = function(self)
			local w, h = UiGetImageSize(self.path)
			if self.w == 0 or self.h == 0 then
				self.w = w
                self.h = h
			end

			return self.w, self.h
		end
	}

	return img:new(obj)
end


function Ui.VSpacer(obj)
	local spacer = Ui.Object:new {
        id = "vspacer",
		draw = function(self)
            UiTranslate(0,self.h)
		end
	}

	return spacer:new(obj)
end


function Ui.TitleBar(obj)
    local btn = function(obj)
        local button = Ui.Rect{
            onClick = function(self)
            end,
            onMouseDown = function(self)
                UiColorFilter(0.5,0.5,0.5)
            end,

        }
        return button:new(obj)
    end
    local titleBar = Ui.Frame{
        color = Ui.Style.Color.White,
        w = 1000,
        h = 40,
        Ui.Frame {
            hAlign = Ui.ContentLayout.Horizontal.Center,
            vAlign = Ui.ContentLayout.Vertical.Top,
            paddingLeft = 20,
            paddingTop = 10,
            color = Ui.Style.Color.White,
            Ui.Text{
                text = obj.title,
                color = Ui.Style.Color.Black,
            }
        },
        Ui.HorizontalLayout{
            hAlign = Ui.ContentLayout.Horizontal.Right,
            vAlign = Ui.ContentLayout.Vertical.Top,
            btn{
                color = Ui.Style.Color.Black,
            },
            btn{
                color = Ui.Style.Color.Yellow,
            },
            btn{
                color = Ui.Style.Color.Red,
                onMouseUp = function(self)
                    self:hide()
                end
            },
        }
    }

    return titleBar
end