-- CONTENTS
-- TARDIS exterior functions
-- drwho_tardis:register_tardis_exterior() API
-- TARDIS Node Definitions (using API)

local S = minetest.get_translator("drwho_tardis")
local data = minetest.get_mod_storage()

-- IMPORTANT:
-- Old Tardises will not be affected by the changes in values.
-- They will need to be manually moved by the player, after they have removed valuable
-- things from it. They can choose what interior design to place in it as well. Use /rebuild_tardis

-- TARDIS EXTERIOR FUNCTIONS
local function tardis_on_place(pos, placer, itemstack, pointed_thing)
	if placer:get_player_name() == "" then return else
		local meta = minetest.get_meta(pos)
		local name = placer:get_player_name()
		local timer = minetest.get_node_timer(pos)
		if data:get_string(name.."in_pos") == "" then
			data:set_string(name.."out_pos", minetest.serialize(pos)) -- set exterior position
			local room = minetest.settings:get('drwho_tardis.default_interior')
			if _drwho_tardis.rooms[room] then room = room else room = "default" end
			local _, msg, door_pos_table = _drwho_tardis:place_tardis_interior(name, room, pos) 
			minetest.chat_send_player(name, S("After you walk inside your Tardis, if the cave generation has broken through it, you will have to run the chat command /rebuild_tardis. Make sure you are somewhere where you won't get blocked out of it though."))

			-- Interior door stuff
			_drwho_tardis:init_interior_doors(name, door_pos_table)

			meta:set_string("id", name) -- set exterior id
			-- "in_pos" is set by the place_tardis_interior() function
			data:set_int(name.."power", 3) --set power
			data:set_int(name.."factor", 1) -- set travel factor
			data:set_string(name.."look", "drwho_tardis:tardis_old") --set skin
			data:set_string(name.."respect_mavity", "yes")
			data:set_int(name.."y_dest", 0)
			data:set_int(name.."x_dest", 0)
			data:set_int(name.."z_dest", 0)
			data:set_string(name.."way1", minetest.serialize({x=0, y=0, z=0}) )
			data:set_string(name.."way2", minetest.serialize({x=0, y=0, z=0}) )
			data:set_string(name.."way3",minetest.serialize({x=0, y=0, z=0}) )
			data:set_string(name.."r_pos", "") 
			timer:start(0.2)
		else minetest.set_node(pos, {name = "air"}) minetest.add_item(pos, "drwho_tardis:tardis_old") end
	end
end

-- Check if there are any players or mobs entering a TARDIS and teleport them
local function tardis_timer(pos)
	local objs = minetest.get_objects_inside_radius(pos, 0.9)
	--for idx,obj in pairs(objs) do -- loop incase there are multiple objs there at once
	local obj = objs[1]
		if obj == nil then return true else --only do code if there is something there
			if obj:is_player() then -- if obj is a player
				local meta = minetest.get_meta(pos)
				local pmeta = obj:get_meta()
				local id = meta:get_string("id")
				local go_pos = minetest.deserialize(data:get_string(id.."in_pos"))
				local name = obj:get_player_name()
				local been_inside = data:get_string(name.."been_inside_tardis")
				go_pos.z = go_pos.z-1
				obj:set_look_horizontal( math.rad( 180 ))
				obj:set_look_vertical( math.rad( 0 ))
				obj:set_pos(go_pos)
				if minetest.get_modpath("awards") and _drwho_tardis.GAMETYPE=="mtg" then
					awards.unlock(name, "drwho_tardis:go_inside_tardis") -- unlock award
				end
				if not been_inside then 
					_drwho_tardis:rebuild_tardis_interior(name) -- Fix caves after gen
					data:set_string(name.."been_inside_tardis", true)
				end
				pmeta:set_string("id", id)
			else -- if obj is not a player (probably a mob)
				local meta = minetest.get_meta(pos)
				local id = meta:get_string("id")
				local go_pos = minetest.deserialize(data:get_string(id.."in_pos"))
				go_pos.z = go_pos.z-2
				obj:set_pos(go_pos)
			end
		end
		return true
	--end
end

-- See API.md for more information about this function
function drwho_tardis:register_tardis_exterior(def) 
	-- REQUIRED: {name, texture}
	-- name MUST be unique
	local name = "_"..def.name -- e.g. "_blue" if def.name == "blue"
	
	if def.name == "" then 
		name = ""  -- ONLY FOR THE DEFAULT BLUE TARDIS
		def.name = "blue"
	end 
	local texture = def.texture
	-- OPTIONAL: {light, description, drawtype, title, groups}
	local light = def.light or 10 -- In case one doesn't want it to glow
	local description = def.description or S("Tardis") -- allow for custom description titles
	local drawtype = def.drawtype or "mesh" -- default to mesh
	local title = def.title or def.name -- Title to be shown on exterior selector
	local groups = def.groups or {not_in_creative_inventory = 1, tardis = 1}

	-- Register normal TARDIS
	minetest.register_node("drwho_tardis:tardis"..name, {
		description = description,
		tiles = {texture},
		use_texture_alpha = "clip",
		drawtype = drawtype,
		mesh = "tardis.obj", -- Only used when drawtype == "mesh"
		node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }}, -- Only used when drawtype == "nodebox"
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		light_source = light,
		groups = groups,
		after_place_node = tardis_on_place,
		diggable = false,
		on_timer = tardis_timer
	})
	-- Register locked TARDIS
	minetest.register_node("drwho_tardis:tardis"..name.."_locked", {
			description = description.." (locked)",
			tiles = {texture},
			use_texture_alpha = "clip",
			drawtype = drawtype,
			mesh = "tardis.obj", -- Only used when drawtype == "mesh"
			node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }}, -- Only used when drawtype == "nodebox"
			selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
			collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
			light_source = 10,
			groups = {not_in_creative_inventory = 1, tardis_locked = 1},
			diggable = false
	})

	-- Add it to the exterior list
	ext_list = minetest.deserialize(data:get_string("ext_list")) or {}
	ext_list[#ext_list+1] = {
		itemstring = "drwho_tardis:tardis"..name,
		name = def.name,
		title = title,
		author = def.author,
		license = def.license or "CC BY-SA 4.0", -- defaults so we don't need to specify each time
		texture = texture
	}
	data:set_string("ext_list", minetest.serialize(ext_list))
end