local quad = require("love_src.wrapper.lappy.new.quad").obj
local image = require("love_src.wrapper.lappy.new.image").obj

local components = {}

components.dict = {
  quad = "quad",
  texture = "texture",
}

local function load_image(path)
  image:put_to(love.graphics.newImage(path), path)
end

local function load_quad(x, y, w, h, path, key)
  load_image(path)
  local i = image:load_from(path)
  local iw, ih = i:getDimensions()
  quad:put_to(love.graphics.newQuad(x or 0, y or 0, w or iw, h or ih, i), key)
end

function components.quad (c, quads)
  c.data = {}
  for _, q_data in pairs(quads) do
    for k_q, q in pairs(q_data.quad) do
      load_quad(q[1], q[2], q[3], q[4], q_data.image, k_q)
      c.data[k_q] = {}
      c.data[k_q].quad = quad:load_from(k_q)
      c.data[k_q].image = image:load_from(q_data.image)
    end
  end
end

function components.texture (c, images)
  c[components.dict.texture] = {}
  for k, i  in pairs(images) do
    load_image(i)
    c[components.dict.texture][k] = image:load_from(i)
  end
end

return components
