local esc = minetest.formspec_escape

local base =
   'formspec_version[4]'..
   'size[13,10]'..
   'no_prepend[]'..
   'bgcolor[;true;#080808BB]'..
   'background[-.5,-.5;14,11;teleport_fs_bg_1.png]'..
   'style_type[image_button;border=false]'

function teleport.formspec_overview(pos, name)
   local stations = teleport.other_stations
   local key = minetest.pos_to_string(pos)
   local owner = teleport.data[key].owner
   local station_name = teleport.data[key].name
   local desc = teleport.data[key].desc
   if #desc > 137 then
      desc = desc:sub(1, 134) .. '...'
   end
   local formspec =
   base..
   'textarea[.25,.25;2.75,8;;;Station Data\n'..
   'Owner: '..esc(owner)..'\n'..
   'Name: '..esc(station_name)..'\n'..
   'Description: '..esc(desc)..']'..
   'style_type[button;bgimg=teleport_fs_blank.png]'..
   'scroll_container[3,.25;9.75,8.5;stations;horizontal;.1]'
   local voffset = -.5
   local hoffset = 0

   for k, v in pairs(stations) do
      local name, strpos = string.match(v, "(.*)|(.*)")
      voffset = voffset + 1
      if voffset > 8 then
         voffset = .5
         hoffset = hoffset + 3.25
      end
      formspec = formspec..
      'button['.. .25+hoffset..','..voffset..';3,.75;'..strpos..';'..esc(name)..']'
   end

   formspec = formspec.. 'scroll_container_end[]'
   if #stations > 24 then
      local total = (math.ceil(#stations/8)) * 3.25
      local scrollboxsize = (total - 9.5)/.1
      formspec = formspec..
      'scrollbaroptions[max='..scrollboxsize..']'..
      'scrollbar[3.25,9;9.5,.5;horizontal;stations;]'
   end

   if name == owner then
      formspec = formspec..
      'image_button[.25,8.75;2.75,1;teleport_fs_edit.png;station_edit;]'
   end

   return formspec
end

function teleport.formspec_station_edit(pos, name)
   local key = minetest.pos_to_string(pos)
   local owner = teleport.data[key].owner
   local name = teleport.data[key].name
   local desc = teleport.data[key].desc
   local formspec = base..
   'textarea[.25,.25;2.75,7;;;Feel free to spin up a mystic story about your destination.]'..
   'textarea[3.25,.5;9.5,1;name;Station Name;'..esc(name)..']'..
   'textarea[3.25,2;9.5,5.25;desc;Description;'..esc(desc)..']'..
   'image_button[.25,7.5;2.75,1;teleport_fs_back.png;back;]'..
   'image_button[.25,8.75;2.75,1;teleport_fs_save.png;save;]'

   return formspec
end

function teleport.formspec_station_info(pos, name)
   local key = minetest.pos_to_string(pos)
   local owner = teleport.data[key].owner
   local station_name = teleport.data[key].name
   local desc = teleport.data[key].desc
   local pos1 = minetest.string_to_pos(key)
   local pos2 = teleport.local_pos[name]
   local distance = vector.distance(pos1, pos2)

   local formspec = base..
   'background[-.5,-.5;14,11;teleport_fs_bg_2.png]'..
   'hypertext[.25,-.3125;12.5,1.25;;<global valign=middle><center><style color=white size=40>'..esc(station_name)..'</style></center>]'..
   'textarea[.25,2;12.5,5.25;;Description;'..esc(desc)..']'..
   'textarea[.25,7.5;12.5,1;;;Distance is '..math.ceil(distance)..' nodes, estimated time to teleport '..math.ceil(distance/100)..' seconds]'..
   'image_button[.25,8.75;2.75,1;teleport_fs_back.png;back;]'
   if distance < 1000 then
      formspec = formspec..
      'image_button[4.5,8.75;4,1;teleport_fs_teleport.png;teleport;]'
   else
      formspec = formspec..
      'image_button[4.5,8.75;4,1;teleport_fs_no_teleport.png;sorry;]'..
      'tooltip[sorry;You can teleport 1000 nodes max.\nConsider teleporting to a closer location.]'
   end

   if name == owner then
      formspec = formspec ..
      'image_button[10,8.75;2.75,1;teleport_fs_edit.png;edit;]'
   end

   return formspec
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
   if formname == 'teleport:formspec' then
      local name = player:get_player_name()
      if fields.quit then
         minetest.close_formspec(name, 'teleport:formspec')
      elseif fields.station_edit then
         local pos = teleport.local_pos[name]
         teleport.remote_pos[name] = pos
         minetest.show_formspec(name, 'teleport:formspec_station_edit', teleport.formspec_station_edit(pos, name))
      end
      for k,v in pairs(fields) do
         local pos = minetest.string_to_pos(k)
         if pos then
            teleport.remote_pos[name] = pos
            minetest.show_formspec(name, 'teleport:formspec_station_info', teleport.formspec_station_info(pos, name))
         end
      end
   elseif formname == 'teleport:formspec_station_edit' then
      local name = player:get_player_name()
      if fields.save then
         teleport.find_stations()
         local pos = teleport.remote_pos[name]
         local key = minetest.pos_to_string(pos)
         local meta = minetest.get_meta(pos)
         meta:set_string('infotext', fields.name)
         teleport.data[key].name = fields.name
         teleport.data[key].desc = fields.desc
      elseif fields.back then
         local pos = teleport.local_pos[name]
         minetest.show_formspec(name, 'teleport:formspec', teleport.formspec_overview(pos, name))
      end
   elseif formname == 'teleport:formspec_station_info' then
      local name = player:get_player_name()
      if fields.teleport then
         minetest.close_formspec(name, 'teleport:formspec_station_info')
         local pos1 = teleport.remote_pos[name]
         local pos2 = teleport.local_pos[name]
         local distance = vector.distance(pos1, pos2)
         minetest.after(distance/100, function()-- This needs to black the screen, and prevent player from moving or taking damage.
            player:set_pos(pos1)
         end)
      elseif fields.back then
         local pos = teleport.local_pos[name]
         minetest.show_formspec(name, 'teleport:formspec', teleport.formspec_overview(pos, name))
      elseif fields.edit then
         local pos = teleport.remote_pos[name]
         minetest.show_formspec(name, 'teleport:formspec_station_edit', teleport.formspec_station_edit(pos, name))
      end
   end
end)
