const SSDPServer = class SSDPServer {
    constructor({utils, config, db, logger}) {
        this.utils = utils;
        this.config = config;
        this.db = db;
        this.ip = this.utils.getIPs()[0].ip;
        this.logger = logger;
    }

    createServer() {
        try {
            if(!this.ip) {
                this.logger.System('Cant get local ip from system, skipping ssdp server init');
                return;
            }
            let Server = require('node-ssdp').Server;
            let server = new Server({
                description: 'SMG Player Application',
                location: "http://" + this.ip + ":" + this.config.Settings.ServicePort,
                ssdpSig: 'SMG UPnP/1.1'
            });

            server.addUSN('upnp:SMGPlayer');

            server.on('advertise-alive', function (headers) {
                //console.log(headers)
                // Expire old devices from your cache.
                // Register advertising device somewhere (as designated in http headers heads)
                //console.log('alive:' + JSON.stringify( headers))
            });

            server.on('advertise-bye', function (headers) {
                //console.log(headers)
                // Remove specified device from cache.
                //console.log('bye:' + JSON.stringify( headers) )
            });

            // start the server
            server.start();

            process.on('exit', function () {
                server.stop() // advertise shutting down and stop listening
            })
        } catch (e) {
            this.logger.System(`Error initializing ssdp server, err: ${e.message}`);
        }

    }
};


const util = require('util'), EventEmitter = require('events').EventEmitter;
util.inherits(SSDPServer, EventEmitter);
module.exports = SSDPServer;