xdescribe('OpalLoader', function(){
    var OpalLoader, OpalRequest, ol, requestParams = {}, globalTimeout = 5000;
    
    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('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();
        });
    });
    
    it('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');
        });
    });
    
    it('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');
        });
    });
    
    it('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');
        });
    });
    
    it('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');
        });
    });
    
    it('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');
        });
    });
    
    
});
