describe('OpalLoader', function(){
    var OpalLoader, OpalRequest, ol, requestParams = {}, globalTimeout = 1000;
    
    it('requirements', function(){
        OpalLoader = require('../../lib/opal/OpalLoader.js').OpalLoader;
    	OpalRequest = require('../../lib/opal/OpalRequest.js').OpalRequest;
	    
		beforeEach(function(){
	        ol = new OpalLoader();
	        ol.setTimeout(globalTimeout);
	        requestParams = {
	            url: 'zs.jlekstan_cmszs.test.onetapi.pl',
	            method: null,
	            params: {}
	        };
	    });
    });
    
    it('setRequest', function(){
        expect(function(){
            ol.setRequest({});
        }).toThrow();
        
        expect(function(){
            ol.load();
        }).toThrow();
    });
    
    it('set/get Timeout', function(){
        var timeout = 1337;

        expect(ol.setTimeout(timeout)).toBe(ol);
        expect(ol.getTimeout()).toEqual(timeout);
    });
	
	it('request opal load with diffrent app name than original', function(){
	
		var spy = spyOn(console, 'warn').andCallThrough();
	    process.env['OPAL_IDENTITY'] = 'test.test.onetapi.pl';
	    requestParams.method = 'getConfig',
		requestParams.params.url = '/';
		requestParams.params.agent = 'iPhone';
		requestParams.application = 'inny_app.onetapi.pl'
		var request = new OpalRequest(requestParams);
		
		var loader = new OpalLoader(request);
		loader.setTimeout(0);
		loader.load();
		
		expect(spy).toHaveBeenCalled();
		delete process.env['OPAL_IDENTITY'];
	});
	
	it('request opal load with app name like original', function(){
	
		var spy = spyOn(console, 'warn').andCallThrough();
	    process.env['OPAL_IDENTITY'] = 'test.test.onetapi.pl';
	    requestParams.method = 'getConfig',
		requestParams.params.url = '/';
		requestParams.params.agent = 'iPhone';
		var request = new OpalRequest(requestParams);
		
		var loader = new OpalLoader(request);
		loader.setTimeout(0);
		loader.load();
		
		expect(spy).not.toHaveBeenCalled();
		delete process.env['OPAL_IDENTITY'];
	});
	
	
	it('request opal load with app name when there is no appName in ENV', function(){
	
		var spy = spyOn(console, 'warn').andCallThrough();

	    requestParams.method = 'getConfig',
		requestParams.params.url = '/';
		requestParams.params.agent = 'iPhone';
		requestParams.application = 'inny_app.onetapi.pl'
		var request = new OpalRequest(requestParams);
		
		var loader = new OpalLoader(request);
		loader.setTimeout(0);
		loader.load();
		
		expect(spy).not.toHaveBeenCalled();
		delete process.env['OPAL_IDENTITY'];
	});
    
    xit('ok', function(){
        var response = null;

        runs(function(){
            requestParams.method = 'getConfig',
            requestParams.params.url = '/';
            requestParams.params.agent = 'iPhone';
            
            var request = new OpalRequest(requestParams);
            request.setGatewayHost('rtx0.int.dev.onet');
            
            ol.setRequest(request);
            ol.addEventListener(OpalLoader.LOADED, function(e){
                response = e;
            }).addEventListener(OpalLoader.ERROR, function(e){
                response = e;
            });
            ol.load();
        });
        
        waitsFor(function(){
            return response !== null;
        }, globalTimeout);
        
        runs(function(){
            expect(response.type).toEqual(OpalLoader.LOADED);
            expect(response.data).toBeDefined();
        });
    });
    
    xit('http error', function(){
        var response = null;

        runs(function(){
            requestParams.method = 'getConfig',
            requestParams.params.url = '/';
            requestParams.params.agent = 'iPhone';
            
            var request = new OpalRequest(requestParams);
            request.setGatewayHost('rtx0.int.dev.onet123123');
            
            ol.setRequest(request);
            ol.addEventListener(OpalLoader.LOADED, function(e){
                response = e;
            }).addEventListener(OpalLoader.ERROR, function(e){
                response = e;
            });
            ol.load();
        });
        
        waitsFor(function(){
            return response !== null;
        }, globalTimeout);
        
        runs(function(){
            expect(response.type).toEqual(OpalLoader.ERROR);
            expect(response.code).toEqual(-1);
            expect(response.data).toBeDefined();
            expect(response.message).toEqual('Http request error');
        });
    });
    
    xit('http status code error', function(){
        var response = null;

        runs(function(){
            requestParams.url = 'zszszs.onetapi.pl';
            requestParams.method = 'getConfig',
            requestParams.params.url = '/';
            requestParams.params.agent = 'iPhone';
            
            var request = new OpalRequest(requestParams);
            request.setGatewayHost('rtx0.int.dev.onet');
            
            ol.setRequest(request);
            ol.addEventListener(OpalLoader.LOADED, function(e){
                response = e;
            }).addEventListener(OpalLoader.ERROR, function(e){
                response = e;
            });
            ol.load();
        });
        
        waitsFor(function(){
            return response !== null;
        }, globalTimeout);
        
        runs(function(){
            expect(response.type).toEqual(OpalLoader.ERROR);
            expect(response.code).toEqual(-2);
            expect(response.data).toEqual(404);
            expect(response.message).toEqual('Wrong HTTP status code');
        });
    });
    
    xit('json parse error', function(){
        var response = null;

        runs(function(){
            requestParams.method = 'badJsonix';
            
            var request = new OpalRequest(requestParams);
            request.setGatewayHost('rtx0.int.dev.onet');
            
            ol.setRequest(request);
            ol.addEventListener(OpalLoader.LOADED, function(e){
                response = e;
            }).addEventListener(OpalLoader.ERROR, function(e){
                response = e;
            });
            ol.load();
        });
        
        waitsFor(function(){
            return response !== null;
        }, globalTimeout);
        
        runs(function(){
            expect(response.type).toEqual(OpalLoader.ERROR);
            expect(response.code).toEqual(-3);
            expect(response.data).toBeDefined();
            expect(response.data.json).toBeDefined();
            expect(response.data.error).toBeDefined();
            expect(response.message).toEqual('JSON parse error');
        });
    });
    
    xit('timeout', function(){
        var response = null;

        runs(function(){
            requestParams.method = 'timeouter';
            
            var request = new OpalRequest(requestParams);
            request.setGatewayHost('rtx0.int.dev.onet');
            
            ol.setRequest(request);
            ol.addEventListener(OpalLoader.LOADED, function(e){
                response = e;
            }).addEventListener(OpalLoader.ERROR, function(e){
                response = e;
            });
            ol.load();
        });
        
        waitsFor(function(){
            return response !== null;
        }, globalTimeout);
        
        runs(function(){
            expect(response.type).toEqual(OpalLoader.ERROR);
            expect(response.code).toEqual(-4);
            expect(response.data).toBeDefined();
            expect(response.data).toBeNull();
            expect(response.message).toEqual('Http response timeout');
        });
    });
    
    xit('remote error', function(){
        var response = null;

        runs(function(){
            requestParams.method = 'error';
            
            var request = new OpalRequest(requestParams);
            request.setGatewayHost('rtx0.int.dev.onet');
            
            ol.setRequest(request);
            ol.addEventListener(OpalLoader.LOADED, function(e){
                response = e;
            }).addEventListener(OpalLoader.ERROR, function(e){
                response = e;
            });
            ol.load();
        });
        
        waitsFor(function(){
            return response !== null;
        }, globalTimeout);
        
        runs(function(){
            expect(response.type).toEqual(OpalLoader.ERROR);
            expect(response.code).toEqual(-123);
            expect(response.data).toBeDefined();
            expect(response.data).toBeNull();
            expect(response.message).toEqual('jestem fajnym bledem');
        });
    });
    
    
});
