color = {}
dofile(minetest.get_modpath('color')..'/stair_functions.lua')

local function register_color(name)
   minetest.register_node('color:'..name, {
      description = name,
      tiles = {'color_'..name..'.png'},
      groups = {breakable=1, stainable=1},
   })
end

local function region_add_delta(pos1, pos2, player, delta)
    if minetest.is_protected(pos1, player) or minetest.is_protected(pos2, player) then
        minetest.debug('*** you are trying to paint in a protected region, '
            .. player .. '!')
    else
        worldedit.add_param2(pos1, pos2, delta)
    end
end

register_color('black')
register_color('white')

minetest.register_tool('color:brush', {
   description = 'Paint Brush',
   inventory_image = 'color_brush.png',
   on_drop = lobby.no_drop,
   on_place = function(itemstack, user, pointed_thing)
      local player = user:get_player_name()
      local pos = minetest.get_pointed_thing_position(pointed_thing)
      local node = minetest.get_node(pos)
      if minetest.get_item_group(node.name, 'stainable') > 0 then
         if not minetest.is_protected(pos, player) then
            minetest.swap_node(pos, {name = node.name, param2 = node.param2-32})
         end
      end
   end,
   on_use = function(itemstack, user, pointed_thing)
      local player = user:get_player_name()
      if worldedit.is_marker_region(pointed_thing) then
         if worldedit.is_region_owner(pointed_thing, player) then
             if worldedit.pos1[player] ~= nil and worldedit.pos2[player] ~= nil then
                local pos1 = worldedit.pos1[player]
                local pos2 = worldedit.pos2[player]
                region_add_delta(pos1, pos2, player, 32)
             end
         end
         return nil
      end
      if pointed_thing.type ~= 'node' then return end
      local pos = minetest.get_pointed_thing_position(pointed_thing)
      local node = minetest.get_node(pos)
      if minetest.get_item_group(node.name, 'stainable') > 0 then
         if not minetest.is_protected(pos, player) then
            minetest.swap_node(pos, {name = node.name, param2 = node.param2+32})
         end
      end
   end,
   on_secondary_use = function(itemstack, user, pointed_thing)
      local player = user:get_player_name()
      if worldedit.is_marker_region(pointed_thing) then
         if worldedit.is_region_owner(pointed_thing, player) then
             if worldedit.pos1[player] ~= nil and worldedit.pos2[player] ~= nil then
                local pos1 = worldedit.pos1[player]
                local pos2 = worldedit.pos2[player]
                region_add_delta(pos1, pos2, player, -32)
             end
         end
      end
      return nil
   end,
})

color.make_darker = function(pos, node, puncher, pointed_thing)
   local player = puncher:get_player_name()
   local wield = puncher:get_wielded_item()
   local wield_name = wield:get_name()
   if not minetest.is_protected(pos, player) and wield_name == 'color:brush' then
      minetest.swap_node(pos, {name = node.name, param2 = node.param2+32})
   else
      return
   end
end

color.make_lighter = function(pos, node, clicker)
   local player = clicker:get_player_name()
   local wield = clicker:get_wielded_item()
   local wield_name = wield:get_name()
   if not minetest.is_protected(pos, player) and wield_name == 'color:brush' then
      minetest.swap_node(pos, {name = node.name, param2 = node.param2-32})
   else
      return
   end
end

local colors = { -- description, node name, hex color
   {'Pink', 'pink', '#ff2db1'},
   {'Aqua', 'aqua', '#00ada4'},
   {'Red', 'red', '#ff6666'},
   {'Orange', 'orange', '#ffb366'},
   {'Yellow', 'yellow', '#ffff66'},
   {'Green', 'green', '#66ff66'},
   {'Blue', 'blue', '#6666ff'},
   {'Violet', 'violet', '#bc66ff'},
   {'Grey', 'grey', '#929292'}
}

for i in ipairs(colors) do
   local desc = colors[i][1]
   local name = colors[i][2]
   local hex = colors[i][3]

   minetest.register_node('color:'..name, {
      description = desc,
      tiles = {{name='color_speckled.png', align_style='world', scale=4}},
      paramtype2 = 'color',
      palette = 'color_'..name..'.png',
      color = hex,
      groups = {breakable=1, stainable=1},
   })

   color.register_stair_and_slab(name,
      {{name='color_speckled.png', align_style='world', scale=4}},
      'color_'..name..'_1.png',
      hex,
      true)

   minetest.register_node('color:'..name..'_blocks', {
      description = desc..' Blocks',
      tiles = {{name='color_blocks.png', align_style='world', scale=4}},
      paramtype2 = 'color',
      palette = 'color_'..name..'.png',
      color = hex,
      groups = {breakable=1, stainable=1},
   })

   color.register_stair_and_slab(name..'_blocks',
      {{name='color_blocks.png', align_style='world', scale=4}},
      'color_'..name..'_1.png',
      hex,
      true)

   minetest.register_node('color:'..name..'_bricks', {
      description = desc..' Bricks',
      tiles = {{name='color_bricks.png', align_style='world', scale=4}},
      paramtype2 = 'color',
      palette = 'color_'..name..'.png',
      color = hex,
      groups = {breakable=1, stainable=1},
   })

   color.register_stair_and_slab(name..'_bricks',
      {{name='color_bricks.png', align_style='world', scale=4}},
      'color_'..name..'_1.png',
      hex,
      true)

   minetest.register_node('color:'..name..'_streak', {
      description = desc..' Laminate',
      tiles = {{name='color_streak.png', align_style='world', scale=4}},
      paramtype2 = 'color',
      palette = 'color_'..name..'.png',
      color = hex,
      groups = {breakable=1, stainable=1},
   })

   color.register_stair_and_slab(name..'_streak',
      {{name='color_streak.png', align_style='world', scale=4}},
      'color_'..name..'_1.png',
      hex,
      true)

   minetest.register_node('color:'..name..'_siding', {
      description = desc..' Siding',
      tiles = {{name='color_siding.png', align_style='world', scale=4}},
      paramtype2 = 'color',
      palette = 'color_'..name..'.png',
      color = hex,
      groups = {breakable=1, stainable=1},
   })

   color.register_stair_and_slab(name..'_siding',
      {{name='color_siding.png', align_style='world', scale=4}},
      'color_'..name..'_1.png',
      hex,
      true)

   minetest.register_node('color:'..name..'_shake', {
      description = desc..' Shake',
      tiles = {{name='color_shake.png', align_style='world', scale=4}},
      paramtype2 = 'color',
      palette = 'color_'..name..'.png',
      color = hex,
      groups = {breakable=1, stainable=1},
   })

   color.register_stair_and_slab(name..'_shake',
      {{name='color_shake.png', align_style='world', scale=4}},
      'color_'..name..'_1.png',
      hex,
      true)

end
