local calendar_style_list = function()
	local k = sorcery.lib.tbl.keys(sorcery.data.calendar.styles)
	table.sort(k)
	local r = {}
	for i,v in ipairs(k) do
		r[i] = {
			id = k[i];
			name = sorcery.data.calendar.styles[v].name;
		}
	end
	return r
end
local astrolabe_formspec = function(pos)
	local m = minetest.get_meta(pos)
	local i = m:get_inventory()
	local datestamp = minetest.get_day_count()
	local date = sorcery.calendar.date(datestamp)
	local strd = sorcery.calendar.stars(datestamp)
	local style = m:get_string('datestyle')
	if style == '' then style = sorcery.data.calendar.default end
	local fdate = string.format('<global halign=center valign=middle size=20><b>%s</b>\n%s',
		sorcery.calendar.shortdate(datestamp,style),
		sorcery.calendar.longdate(datestamp,style))
	sstr=string.format('<global halign=center valign=middle><style size=20>Sign of the %s</style>\n<i>Stellar cycle %u</i>',strd.sign.patron,strd.cycle)
	local lphase = sorcery.calendar.contmatch(sorcery.calendar.moon_phases,date.lunar_phase)
	local menu
	local mi = calendar_style_list()
	local activestyle = 1
	local getphaseimg = function(body,fac)
		if fac >= 1 then fac = 0 end
		return 'sorcery_'..body..'_phases.png^[verticalframe:19:' .. tostring(math.floor(fac * 20))
	end
	local planetphase = getphaseimg('planet',minetest.get_timeofday())
	local ltime = '<global halign=center size=15>' .. sorcery.calendar.contmatch(sorcery.calendar.timesofday,minetest.get_timeofday()+0.1)
	local moonphase = getphaseimg('moon',date.lunar_phase)
	local moonlabel = string.format('<global halign=center size=15>%s Moon', lphase)
	for i,s in ipairs(mi) do
		if s.id == style then activestyle = i end
		local esc = minetest.formspec_escape(s.name)
		if menu
			then menu = menu .. ',' .. esc
			else menu = esc
		end
	end
	m:set_string('formspec', string.format([[
		formspec_version[3] size[14,4.25]
		box[0.25,0.25;3.75,3.75;#000000FF]
		hypertext[0.25,2.75;3.75,1;;%s]
		dropdown[4.1,0.25;5.1;style;%s;%u]

		box[4.1,1.5;2.50,2.50;#000000FF]
		image[4.1,1.5;2.50,2.50;%s]
		hypertext[4.1,3.50;2.50,1;;%s]

		box[6.7,1.5;2.50,2.50;#000000FF]
		image[6.7,1.5;2.50,2.50;%s]
		hypertext[6.7,3.50;2.50,1;;%s]

		hypertext[9.60,1.5;4.15,2.5;;%s]

	]],
		minetest.formspec_escape(sstr),
		menu, activestyle,
		moonphase, minetest.formspec_escape(moonlabel),
		planetphase, minetest.formspec_escape(ltime),
		minetest.formspec_escape(fdate)))
end
local albox = {
	type = 'fixed';
	fixed = {
		-0.43,-0.5,-0.43;
		 0.43, 0.35, 0.43;
	};
}
minetest.register_node('sorcery:astrolabe',{
	description = 'Astrolabe';
	drawtype = 'mesh';
	mesh = 'sorcery-astrolabe.obj';
	groups = {
		cracky = 2, choppy = 2;
		dig_immediate = 2;
		sorcery_tech = 1;
		flammable = 1;
	};
	sunlight_propagates = true;
	paramtype = 'light';
	selection_box = albox, collision_box = albox;
	after_dig_node = sorcery.lib.node.purge_containers;
	tiles = {
		'default_steel_block.png';
		'default_bronze_block.png';
		'default_copper_block.png';
		'default_aspen_wood.png';
	};
	on_construct = function(pos)
		local m = minetest.get_meta(pos)
		local i = m:get_inventory()
		m:set_string('infotext','Astrolabe')
		astrolabe_formspec(pos)
	end;
	on_rightclick = function(pos)
		astrolabe_formspec(pos)
	end;
	on_receive_fields = function(pos,name,fields)
		local m = minetest.get_meta(pos)
		local i = m:get_inventory()

		local csl = calendar_style_list()
		if fields.style then
			for _,s in pairs(csl) do
				if s.name == fields.style then
					m:set_string('datestyle',s.id)
					astrolabe_formspec(pos)
					break
				end
			end
		end
	end;
	_sorcery = {
		recipe = {
			note = 'Unravel the secrets of the stars';
		};
	};
})

minetest.register_craft {
	output = 'sorcery:astrolabe';
	recipe = {
		{'sorcery:steel_screw','default:steel_ingot','sorcery:steel_screw'};
		{'default:copper_ingot',sorcery.data.metals.brass.parts.block,'default:copper_ingot'};
		{'default:stick','sorcery:screw_steel','default:stick'};
	};
};
