// We need to find services for _coap._udp

// name = _mdns.name 'gw-a0cc2b6f6b31'
// [?] fqdn = _mdns.SRV.name 'gw-a0cc2b6f6b31._coap._udp.local',
// host = _mdns.SRV.data.target 'TRADFRI-Gateway-a0cc2b6f6b31.local',
// port = _mdns.SRV.data.port 5684

// Excerpt from a mDNS request
// Answers
// _services._dns-sd._udp.local: type PTR, class IN, _coap._udp.local
// => _coap._udp.local: type PTR, class IN, gw-b072bf257a41._coap._udp.local
// gw-b072bf257a41._coap._udp.local: type TXT, class IN, cache flush
// => gw-b072bf257a41._coap._udp.local: type SRV, class IN, cache flush, priority 0, weight 0, port 5684, target TRADFRI-Gateway-b072bf257a41.local
// _services._dns-sd._udp.local: type PTR, class IN, _hap._tcp.local
// _hap._tcp.local: type PTR, class IN, TRADFRI gateway._hap._tcp.local
// TRADFRI gateway._hap._tcp.local: type TXT, class IN, cache flush
// TRADFRI gateway._hap._tcp.local: type SRV, class IN, cache flush, priority 0, weight 0, port 80, target TRADFRI-Gateway-b072bf257a41.local
// Additional records
// TRADFRI-Gateway-b072bf257a41.local: type A, class IN, cache flush, addr 192.168.2.102
// TRADFRI-Gateway-b072bf257a41.local: type AAAA, class IN, cache flush, addr fe80::b272:bfff:fe25:7a41

/**
 * Check if the target of the PTR record is a tradfri gateway
 * @param {string} ptrData The target of the PTR record to check
 */
function PTRIsTradfri(ptrData) {
	return /gw\-[a-f0-9]{12}\._coap\._udp\.local/.test(ptrData);
}

function detect(ip, device, options, callback) {

	function fail() {
		callback(null, false, ip);
	}

	console.log("tradfri => device=" + JSON.stringify(device));

	// We need to have mdns data with a PTR and SRV record
	if (!device._mdns || !device._mdns.PTR || !device._mdns.SRV) return fail();
	// The PTR must point to a tradfri gateway
	if (!PTRIsTradfri(device._mdns.PTR)) return fail();
	// The SRV must have a port of 5684
	if (device._mdns.SRV.port != 5684) return fail(); // != because we don't know if string or number

	//addInstance(ip, device, options, device._mdns, callback);
	fail();
}

exports.detect = detect;
exports.type = ['ip'];
exports.timeout = 1500;
const tools = require("../tools.js");
const adapterName = "tradfri";

const hostnameRegExp = /^gw\-[a-f0-9]{12}/;
/**
 * Check if a hostname references a tradfri gateway
 * @param {string} hostname The hostname to check
 */
function hostnameIsTradfri(hostname) {
	return hostnameRegExp.test(hostname);
}

function startsWithTRADFRI(str) {
	return /^TRADFRI/.test(str);
}

/**
 * Tests if a packet references a CoAP resource
 * @param {string} packetName The name of the packet to check
 */
function isCoAP(packetName) {
	return /_coap\._udp\.local$/.test(packetName);
}

function addInstance(ip, hostname, device, options, callback) {
	// Try to find an existing instance for this IP
	const instance = tools.findInstance(options, adapterName, (obj) => {
		return obj && obj.native && (obj.native.host === ip || obj.native.host === hostname);
	});

	if (!instance) {
		const id = tools.getNextInstanceID(adapterName, options);
		options.newInstances.push({
			_id: id,
			common: {
				name: adapterName
			},
			native: {
				host: ip
			},
			comment: {
				add: [tools.translate(options.language, 'Required for %s', 'Trådfri gateway ' + hostname)]
			}
		});
		callback(true);
	} else {
		callback(false);
	}
}

function detect(ip, device, options, callback) {

	function fail() {
		callback(null, false, ip);
	}

	// console.log("tradfri => device=" + JSON.stringify(device));

	// We need to have mdns data with a PTR and SRV record, both referencing a CoAP resource
	if (!device._mdns
		|| !(device._mdns.PTR && isCoAP(device._mdns.PTR.name))
		|| !(device._mdns.SRV && isCoAP(device._mdns.SRV.name))
	) return fail();

	// The PTR must point to a tradfri gateway with a corresponding SRV entry
	const fullHostname = device._mdns.PTR.data;
	const srv = device._mdns.SRV;
	if (srv.name !== fullHostname || !hostnameIsTradfri(fullHostname)) return fail();

	// The SRV must have a port of 5684
	if (srv.data.port != 5684) return fail(); // != because we don't know if string or number

	// And the A(AAA) record must contain "TRADFRI"
	const ARecord = device._mdns.A || device._mdns.AAAA;
	if (!startsWithTRADFRI(ARecord.name)) return fail();

	const shortHostname = hostnameRegExp.exec(fullHostname)[0];
	addInstance(ip, shortHostname, device, options, callback);
}

module.exports = {
	detect,
	type: ['ip'],
	timeout: 1500
};

Implement tradfri auto-discovery