-- garden/l_system_instructions (default)

------------------------------
-- notes:
-- see documentation on github for instructions on updating existing instructions and creating new ones:
--  https://github.com/jaseknighter/flora/blob/main/README.md
-- make sure the number_of_instructions variable is equal to the number of instructions listed 
--  in the l_system_instructions.get_instruction function
------------------------------

local garden = {}

function garden.init()
  local planted_plants = tab.load(planted_plants_path) or {}
  if #planted_plants > 0 then
    for i=1,#planted_plants,1
    do
      local plant_name = planted_plants[i]
      local plant_path = nursery_path .. plant_name
      local plant = tab.load(plant_path)
      table.insert(garden_catalog, plant)
    end
  end
end

-- local catalog = catalog_default
function garden.get_num_plants()
  return #garden_catalog
end

function garden.add(plant)
  table.insert(garden_catalog, plant)
  -- local planted_plants_path = nursery_path .. "planted_plants"
  local planted_plants = tab.load(planted_plants_path) or {}
  print(planted_plants, plant.name, planted_plants_path)
  table.insert(planted_plants, plant.name)
  tab.save(planted_plants, planted_plants_path)
end

function garden.remove(plant_name, author)
  -- remove from in memory plant catalog
  print("remove", plant_name, author)
  for i=1,#garden_catalog,1
  do
    -- print(garden_catalog[i].name)
    if garden_catalog[i].name and garden_catalog[i].name == plant_name then
      print("found plant_name to remove",plant_name)
      table.remove(garden_catalog, i)
    end
  end

  -- remove from saved file of planted plants
  local planted_plants = tab.load(planted_plants_path) or {}
  for i=1,#planted_plants,1
  do
    if planted_plants[i] == plant_name then
      print("found plant_name to remove in file",plant_name)
      table.remove(planted_plants, i)
      tab.save(planted_plants, planted_plants_path)
    end
  end


  table.insert(planted_plants, plant.name)
  tab.save(planted_plants, planted_plants_path)


end

function garden.get_instruction(instruction_id)
  -- local instruction = {}
  -- for i=1,1, garden.get_num_plants()
  -- do
  --   table.insert(instruction,default_instructions[instruction_id])
  -- end
  -- local instruction = default_instructions[instruction_id]
  return garden_catalog[instruction_id]
end

return garden
