local tw = {}
_tees = {}

function tw.load_all_skins()
	_allskins = {}
	for file in lfs.dir("D:/teeclone/data/skins") do
		local g = "D:/teeclone/data/skins/"..file
		local attr = lfs.attributes(g)
		assert(type(attr) == "table")
		if attr.mode ~= "directory" then
			table.insert(_allskins, #_allskins+1, file)
		end
	end
	return _allskins
end

function tw.create(Skin, X, Y)
	Skin = Skin or "default.png"
	X = X or 0
	Y = Y or 0
	local base = love.graphics.newImage("data/skins/"..Skin)
	local body = love.graphics.newQuad(0, 0, 96, 96, base:getWidth(), base:getHeight())
	local bodyshadow = love.graphics.newQuad(96, 0, 96, 96, base:getWidth(), base:getHeight())
	local feetshadow = love.graphics.newQuad(192, 64, 64, 32, base:getWidth(), base:getHeight())
	local feet = love.graphics.newQuad(192, 32, 64, 32, base:getWidth(), base:getHeight())
	local eyes = love.graphics.newQuad(64+(32*0), 96, 32, 32, base:getWidth(), base:getHeight())
	table.insert(_tees, #_tees+1,
	{
	id = #_tees+1,
	skin = Skin,
	x = X,
	y = Y,
	hspeed = 0,
	vspeed = 0,
	gravity = 0,
	m_base = base,
	m_body = body,
	m_feet = feet,
	m_bodyshadow = bodyshadow,
	m_feetshadow = feetshadow,
	m_eyesimg = eyes,
	m_eyes = 0,
	m_direction = 0,
	m_angle = 0,
	lendir_x = 0,
	lendir_y = 0,
	update = function(this, dt, mouseaim)
		mouseaim = mouseaim or false
		local mouse_x, mouse_y = love.mouse.getPosition()
			
		if mouseaim then
			if mouse_x < this.x+(96/2) then this.direction = 1
			else this.direction = 0 end
			
				this.m_angle = point_direction(this.x+(96/2), this.y+(96/2), mouse_x, mouse_y)
				this.lendir_x = lengthdir_x(12, this.m_angle)
				this.lendir_y = lengthdir_y(12, this.m_angle)
				
			else
				x_lendir = x_lendir or lengthdir_x(12, this.m_angle)
				y_lendir = y_lendir or lengthdir_y(12, this.m_angle)
				
			end
			
			this.vspeed = this.vspeed + this.gravity *dt
			this.x = this.x + this.hspeed * dt
			this.y = this.y + this.vspeed * dt
		end
	})
	return _tees[#_tees]
end

function tw.delete(id)
	table.remove(_tees, id)
	for i=1, #_tees do
		_tees[i].id = i
	end
end

function tw.update(dt, mouseaim, x_lendir, y_lendir)
	
end

function tw.draw()
	for i=1,#_tees do
		love.graphics.setColor(255, 255, 255)
		if _tees[i].direction == 0 then
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feetshadow, _tees[i].x+31, _tees[i].y+64-6)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_bodyshadow, _tees[i].x, _tees[i].y)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feetshadow, _tees[i].x-1, _tees[i].y+64-6)
			
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feet, _tees[i].x+31, _tees[i].y+64-6)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_body, _tees[i].x, _tees[i].y)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feet, _tees[i].x-1, _tees[i].y+64-6)
			
		elseif _tees[i].direction == 1 then
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feetshadow, _tees[i].x-1, _tees[i].y+64-6)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_bodyshadow, _tees[i].x, _tees[i].y)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feetshadow, _tees[i].x+31, _tees[i].y+64-6)
			
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feet, _tees[i].x-1, _tees[i].y+64-6)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_body, _tees[i].x, _tees[i].y)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feet, _tees[i].x+31, _tees[i].y+64-6)
		
		else -- default direction...
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feetshadow, _tees[i].x+31, _tees[i].y+64-6)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_bodyshadow, _tees[i].x, _tees[i].y)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feetshadow, _tees[i].x-1, _tees[i].y+64-6)
			
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feet, _tees[i].x+31, _tees[i].y+64-6)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_body, _tees[i].x, _tees[i].y)
			love.graphics.draw(_tees[i].m_base, _tees[i].m_feet, _tees[i].x-1, _tees[i].y+64-6)

		end
		love.graphics.draw(_tees[i].m_base, _tees[i].m_eyesimg, _tees[i].x+48+32-11+_tees[i].lendir_x, _tees[i].y+32+_tees[i].lendir_y, 0, -1, 1)
		love.graphics.draw(_tees[i].m_base, _tees[i].m_eyesimg, _tees[i].x+48-22+_tees[i].lendir_x, _tees[i].y+32+_tees[i].lendir_y, 0, 1, 1)
	end
end

return tw