local letters = {
   {'A', 'a'},
   {'B', 'b'},
   {'C', 'c'},
   {'D', 'd'},
   {'E', 'e'},
   {'F', 'f'},
   {'G', 'g'},
   {'H', 'h'},
   {'I', 'i'},
   {'J', 'j'},
   {'K', 'k'},
   {'L', 'l'},
   {'M', 'm'},
   {'N', 'n'},
   {'O', 'o'},
   {'P', 'p'},
   {'Q', 'q'},
   {'R', 'r'},
   {'S', 's'},
   {'T', 't'},
   {'U', 'u'},
   {'V', 'v'},
   {'W', 'w'},
   {'X', 'x'},
   {'Y', 'y'},
   {'Z', 'z'}
}

local numbers = {
   {'0', '0'},
   {'1', '1'},
   {'2', '2'},
   {'3', '3'},
   {'4', '4'},
   {'5', '5'},
   {'6', '6'},
   {'7', '7'},
   {'8', '8'},
   {'9', '9'}
}

for i in ipairs(letters) do
   local desc = letters[i][1]
   local name = letters[i][2]

   minetest.register_node('letters:'..name, {
      description = 'Letter '..desc,
      drawtype = 'signlike',
      tiles = {'letters_'..name..'.png'},
      inventory_image = 'letters_'..name..'.png',
      wield_image = 'letters_'..name..'.png',
      paramtype = 'light',
      paramtype2 = 'colorwallmounted',
      palette = 'letters_color_palette.png',
      sunlight_propagates = true,
      selection_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      collision_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      groups = {dig_immediate = 2, attached_node = 1},
      on_rightclick = function(pos, node, clicker)
         local player_name = clicker:get_player_name()
         if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(clicker, 'protection_bypass') then
            return
         end
         minetest.swap_node(pos, {name = node.name, param2 = node.param2+8})
      end,
      on_punch = function(pos, node, puncher, pointed_thing)
         local player_name = puncher:get_player_name()
         if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(puncher, 'protection_bypass') then
            return
         end
         minetest.swap_node(pos, {name = node.name, param2 = node.param2-8})
      end
   })
end

for i in ipairs(numbers) do
   local desc = numbers[i][1]
   local name = numbers[i][2]

   minetest.register_node('letters:'..name, {
      description = 'Number '..desc,
      drawtype = 'signlike',
      tiles = {'letters_'..name..'.png'},
      inventory_image = 'letters_'..name..'.png',
      wield_image = 'letters_'..name..'.png',
      paramtype = 'light',
      paramtype2 = 'colorwallmounted',
      palette = 'letters_color_palette.png',
      sunlight_propagates = true,
      selection_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      collision_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      groups = {dig_immediate = 2, attached_node = 1},
      on_rightclick = function(pos, node, clicker)
         local player_name = clicker:get_player_name()
         if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(clicker, 'protection_bypass') then
            return
         end
         minetest.swap_node(pos, {name = node.name, param2 = node.param2+8})
      end,
      on_punch = function(pos, node, puncher, pointed_thing)
         local player_name = puncher:get_player_name()
         if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(puncher, 'protection_bypass') then
            return
         end
         minetest.swap_node(pos, {name = node.name, param2 = node.param2-8})
      end
   })
end

letters_shop =
'size[9,9.5]'..
'image_button[0,0;1,1;letters_a.png;a;]'..
'image_button[1,0;1,1;letters_b.png;b;]'..
'image_button[2,0;1,1;letters_c.png;c;]'..
'image_button[3,0;1,1;letters_d.png;d;]'..
'image_button[4,0;1,1;letters_e.png;e;]'..
'image_button[5,0;1,1;letters_f.png;f;]'..
'image_button[6,0;1,1;letters_g.png;g;]'..
'image_button[7,0;1,1;letters_h.png;h;]'..
'image_button[8,0;1,1;letters_i.png;i;]'..
'image_button[0,1;1,1;letters_j.png;j;]'..
'image_button[1,1;1,1;letters_k.png;k;]'..
'image_button[2,1;1,1;letters_l.png;l;]'..
'image_button[3,1;1,1;letters_m.png;m;]'..
'image_button[4,1;1,1;letters_n.png;n;]'..
'image_button[5,1;1,1;letters_o.png;o;]'..
'image_button[6,1;1,1;letters_p.png;p;]'..
'image_button[7,1;1,1;letters_q.png;q;]'..
'image_button[8,1;1,1;letters_r.png;r;]'..
'image_button[0,2;1,1;letters_s.png;s;]'..
'image_button[1,2;1,1;letters_t.png;t;]'..
'image_button[2,2;1,1;letters_u.png;u;]'..
'image_button[3,2;1,1;letters_v.png;v;]'..
'image_button[4,2;1,1;letters_w.png;w;]'..
'image_button[5,2;1,1;letters_x.png;x;]'..
'image_button[6,2;1,1;letters_y.png;y;]'..
'image_button[7,2;1,1;letters_z.png;z;]'..
'image_button[8,2;1,1;letters_0.png;0;]'..
'image_button[0,3;1,1;letters_1.png;1;]'..
'image_button[1,3;1,1;letters_2.png;2;]'..
'image_button[2,3;1,1;letters_3.png;3;]'..
'image_button[3,3;1,1;letters_4.png;4;]'..
'image_button[4,3;1,1;letters_5.png;5;]'..
'image_button[5,3;1,1;letters_6.png;6;]'..
'image_button[6,3;1,1;letters_7.png;7;]'..
'image_button[7,3;1,1;letters_8.png;8;]'..
'image_button[8,3;1,1;letters_9.png;9;]'..
'list[current_name;input;0,4;1,1;]'..
'label[1,4.25;One coin per letter. Click the letters you want, they\'ll be added to inventory.]'..
'list[current_player;main;.5,5.5;8,4;]'

minetest.register_node('letters:station', {
   description = 'Sign Shop',
   --drawtype = 'mesh',
   --mesh = 'letters_station.obj',
   tiles = {'letters_station_top.png', 'letters_station.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   groups = {oddly_breakable_by_hand=2, choppy=3, stainable=1},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('input', 1)
      meta:set_string('infotext', 'Sign Shop')
      meta:set_string('formspec', letters_shop)
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('input') then
         return true
      else
         return false
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local input = stack:get_name()
      if listname == 'input' then
         if input == 'commoditymarket_fantasy:gold_coins' then
            return 1000
         else
            return 0
         end
      end
   end,
   on_receive_fields = function(pos, formname, fields, sender)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory(pos)
      local coins = inv:get_stack('input', 1)
      local coin_count = coins:get_count()
      if coin_count >= 1 then
         for i=0, 26 do
            local letter = string.char(i + 96)
            if fields [letter] then
               local player_inv = sender:get_inventory()
               if player_inv:room_for_item('main', 'letters:'..letter) then
                  player_inv:add_item('main', 'letters:'..letter)
                  coins:take_item()
                  inv:set_stack('input',1,coins)
               end
            end
         end
         for i=0, 9 do
            local letter = tostring(i)
            if fields [letter] then
               local player_inv = sender:get_inventory()
               if player_inv:room_for_item('main', 'letters:'..letter) then
                  player_inv:add_item('main', 'letters:'..letter)
                  coins:take_item()
                  inv:set_stack('input',1,coins)
               end
            end
         end
      end
   end,
})

minetest.register_craft({
   output = 'letters:station',
   recipe = {
      {'furniture:brush', 'furniture:brush', 'furniture:brush'},
      {'default:paper', 'default:paper', 'default:paper'},
      {'dye:black', 'group:dye', 'dye:white'}
   }
})
