local GdiBase = require('script.lib.ui.layout.linear')
local Text = require('script.lib.ui.text')
local Radius = require('script.lib.ui.radius')

local modname = 'GdiButton'
local M = GdiBase:new()
_G[modname] = M
package.loaded[modname] = M

function M:new(o)
	o = o or GdiBase:new(o)
	o.children = {}
	o.bgcolor = o.bgcolor or '#CCCCCC'
	o.bgcolor_focus = o.bgcolor_focus or '#FFFFFF'
	o.fgcolor = o.fgcolor or '#333333'
	o.fgcolor_focus = o.fgcolor_focus or '#000000'
	o.fgcolor_disable = o.fgcolor_disable or '#888888'
	o.text = o.text or 'Button'
	o.font_size = o.font_size or 24
	o.family = o.font_family or ''
	o.padleft = o.padleft or 10
	o.padtop = o.padtop or 10
	o.padright = o.padright or 10
	o.padbottom = o.padbottom or 10
	o.layers = {}
	o.track_display = o.track_display or 1
	o.cur = SysCursor.hand
	o.radius = o.radius or 10
	o.state = {focused=false, enter=false}
	o.enable = o.enable or 1
	o.hit = function(this, evt)
		if this.enable ~= 1 then
			this.layers.bg.color = this.bgcolor
			this.layers.bg:update()
			this.layers.fg.color = this.fgcolor_disable
			this.layers.fg:update_and_paint()
			return
		end
		if evt == WinEvent.leftbuttonup then
			this.layers.bg.color = this.bgcolor
			this.layers.bg:update_and_paint()
			if this.click then this.click() end
		elseif evt == WinEvent.gotfocus then
			this.state.focused = true
			this.layers.bg.color = this.bgcolor_focus
			this.layers.bg:update_and_paint()
		elseif evt == WinEvent.lostfocus then
			this.state.focused = false
			this.layers.bg.color = this.bgcolor
			this.layers.bg:update_and_paint()
		elseif evt == WinEvent.mouseenter then
			this.state.enter = true
			if this.track_display ~= 0 then
				this.layers.fg.text = '[ ' .. this.text .. ' ]'
			end
			this.layers.fg.color = this.fgcolor_focus
			this.layers.fg:update_and_paint()
		elseif evt == WinEvent.mouseleave then
			this.state.enter = false
			this.layers.fg.text = this.text
			this.layers.fg.color = this.fgcolor
			this.layers.fg:update_and_paint()
		end
	end
	setmetatable(o, self)
	self.__index = self
	return o;
end

function M:attach(parent)
	parent:add(self)
	self:add(Radius:new({
		color = self.bgcolor,
		radius = self.radius
	}))
	self:add(Text:new({
		color = self.fgcolor,
		text = self.text,
		size = self.font_size,
		family = self.font_family
	}))
	self.layers.bg = self.children[1]
	self.layers.fg = self.children[2]
	self:reset(self.text)
end

function M:reset(text)
	self.text = text
	self.layers.fg.text = text
	if self.enable ~= 1 then
		self.layers.bg.color = self.bgcolor
		self.layers.fg.color = self.fgcolor_disable
	end
	self.layers.bg:update()
	self.layers.fg:update()
end

function M:disabled()
	self.enable = 2
	self.layers.bg.color = self.bgcolor
	self.layers.fg.color = self.fgcolor_disable
	self.layers.bg:update()
	self.layers.fg:update()
end

function M:enabled()
	self.enable = 1
	self.layers.bg.color = self.bgcolor
	self.layers.fg.color = self.fgcolor
	self.layers.bg:update()
	self.layers.fg:update()
end

return M