let assert = require('assert');
import MIM from '../multipleInstanceManager';
import { MultipleInstanceInterface } from "../multipleInstanceInterface";
import Settings from "../settings";

let calledDestroy = false;


class Temp implements MultipleInstanceInterface {
    private id: string = "default";
    constructor(token:string = "test"){
        let config = Settings.Instance.getCustom(token);
        this.id = config.id;
    }
    public getId(){
        return this.id;
    }
    public destroy(){
        calledDestroy = true;
    }

    public getStatus(){
        return "ok";
    }

    public isFunctional(){
        return true;
    }
}

let conf = {
    id: "global"
}

describe('test multiple instance manager', function () {
    it('complete test', function (done) {
        Settings.Instance.setCustom("global", conf);
        let manager = new MIM<Temp>(Temp);
        let instance = manager.get();
        assert.notEqual(instance, null);
        if(instance)      
            assert.equal(instance.getId(), conf.id);

        assert.equal(manager.get("custom"), null);
        Settings.Instance.setCustom("custom", {id: "custom"});        
        manager.newInstance("custom");

        instance = manager.get("custom");
        assert.notEqual(instance, null); 
        if(instance)               
            assert.equal(instance.getId(), "custom");

        assert.equal(calledDestroy, false);
        manager.delete("custom");
        assert.equal(calledDestroy, true);
        assert.equal(manager.get("custom"), null);
        
        Settings.Instance.setCustom("test", {id: "custom2"});                
        let custom2 = new Temp();
        manager.set(custom2, "custom");
        
        instance = manager.get("custom");
        assert.notEqual(instance, null);
        if(instance)              
            assert.equal(instance.getId(), "custom2");
        
        done();
    });
});