local esc = minetest.formspec_escape

local function plaque_edit(pos)
   local node = minetest.get_node(pos)
   local node_name = node.name
   local def = minetest.registered_nodes[node_name]
   local texture = def.formspec_bg
   local meta = minetest.get_meta(pos, texture)
   local title = meta:get_string('infotext')
   local body = meta:get_string('body')
   local formspec =
      'formspec_version[3]'..
      'size[10,5.5]'..
      'background[0,0;10,5.5;'..texture..']'..
      'field[0.25,.75;9.5,.75;title;Title:;'..esc(title)..']'..
      'textarea[.25,1.75;9.5,3;body;;'..esc(body)..']'..
      'button_exit[1.5,4.75;3,.75;save;Save]'..
      'button_exit[5.5,4.75;3,.75;preview;Preview]'
   return formspec
end

local function plaque_read(pos)
   local node = minetest.get_node(pos)
   local node_name = node.name
   local def = minetest.registered_nodes[node_name]
   local texture = def.formspec_bg
   local meta = minetest.get_meta(pos, texture)
   local meta = minetest.get_meta(pos)
   local title = meta:get_string('infotext')
   local body = meta:get_string('body')
   local formspec =
      'formspec_version[3]'..
      'size[10,5.5]'..
      'background[0,0;10,5.5;'..texture..']'..
      'hypertext[.25,.25;9.5,1;;<center><style color=white size=30>'..esc(title)..'</style></center>]'..
      'textarea[.25,1.125;9.5,4.25;;;'..esc(body)..']'
   return formspec
end

minetest.register_node('signs:1_wood', {
   drawtype = 'mesh',
   mesh = 'signs_wall_1.obj',
   description = 'Wall Sign (Wooden)',
   groups = {breakable=1},
   sunlight_propagates = true,
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'signs_1_wood.png'},
   formspec_bg = 'signs_wood_bg.png',
   selection_box = {
      type = 'fixed',
      fixed = {-.4375, -.3125, .5, .4375, .3125, .4375},
      },
   collision_box = {
      type = 'fixed',
      fixed = {-.4375, -.3125, .5, .4375, .3125, .4375},
      },
   after_place_node = function(pos, placer)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', '')
      meta:set_string('body', '')
   end,
   on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
      local name = clicker:get_player_name()
      if not minetest.is_protected(pos, name) or minetest.check_player_privs(name, { server = true }) then
         signs.player_pos[name] = pos
         minetest.show_formspec(name, 'signs:plaque_edit', plaque_edit(pos))
      else
         minetest.show_formspec(name, 'signs:plaque_read', plaque_read(pos))
      end
   end,
})

minetest.register_node('signs:1_metal', {
   drawtype = 'mesh',
   mesh = 'signs_wall_1.obj',
   description = 'Wall Sign (Steel)',
   groups = {breakable=1},
   sunlight_propagates = true,
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'signs_1_steel.png'},
   formspec_bg = 'signs_steel_bg.png',
   selection_box = {
      type = 'fixed',
      fixed = {-.4375, -.3125, .5, .4375, .3125, .4375},
      },
   collision_box = {
      type = 'fixed',
      fixed = {-.4375, -.3125, .5, .4375, .3125, .4375},
      },
   after_place_node = function(pos, placer)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', '')
      meta:set_string('body', '')
   end,
   on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
      local name = clicker:get_player_name()
      if not minetest.is_protected(pos, name) or minetest.check_player_privs(name, { server = true }) then
         signs.player_pos[name] = pos
         minetest.show_formspec(name, 'signs:plaque_edit', plaque_edit(pos))
      else
         minetest.show_formspec(name, 'signs:plaque_read', plaque_read(pos))
      end
   end,
})

minetest.register_node('signs:1_bronze', {
   drawtype = 'mesh',
   mesh = 'signs_wall_1.obj',
   description = 'Wall Sign (Bronze)',
   groups = {breakable=1},
   sunlight_propagates = true,
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'signs_1_bronze.png'},
   formspec_bg = 'signs_bronze_bg.png',
   selection_box = {
      type = 'fixed',
      fixed = {-.4375, -.3125, .5, .4375, .3125, .4375},
      },
   collision_box = {
      type = 'fixed',
      fixed = {-.4375, -.3125, .5, .4375, .3125, .4375},
      },
   after_place_node = function(pos, placer)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', '')
      meta:set_string('body', '')
   end,
   on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
      local name = clicker:get_player_name()
      if not minetest.is_protected(pos, name) or minetest.check_player_privs(name, { server = true }) then
         signs.player_pos[name] = pos
         minetest.show_formspec(name, 'signs:plaque_edit', plaque_edit(pos))
      else
         minetest.show_formspec(name, 'signs:plaque_read', plaque_read(pos))
      end
   end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   if formname == 'signs:plaque_edit' then
      local name = player:get_player_name()
      local pos = signs.player_pos[name]
      local meta = minetest.get_meta(pos)
      if fields.save then
         meta:set_string('infotext', fields.title)
         meta:set_string('body', fields.body)
         minetest.log('action', (name or '')..' wrote \''..fields.title..'\' to plaque at '..minetest.pos_to_string(pos))
      elseif fields.preview then
         meta:set_string('infotext', fields.title)
         meta:set_string('body', fields.body)
         minetest.log('action', (name or '')..' wrote \''..fields.title..'\' to plaque at '..minetest.pos_to_string(pos))
         minetest.show_formspec(name, 'signs:plaque_read', plaque_read(pos))
      end
   end
end)
