describe('DeviceDetector', function(){
    
    var Request = require('../../lib/index.js').Net.Request;
    var DeviceDetector = null;
    
    it('require DeviceDetector', function(){
    	DeviceDetector = require('../../lib/index.js').Util.DeviceDetector;
    	expect(DeviceDetector).toBeTruthy();
        
        beforeEach(function(){
            DeviceDetector = require('../../lib/index.js').Util.DeviceDetector;
        });
	});
    
    it('getUserAgent #1', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('HTTP_USER_AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });
    
    it('getUserAgent #2', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('HTTP_USER_AGENT', 'XXX', true);
        req.setHeader('HTTP_X_OPERAMINI_PHONE_UA', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });
    
    it('getUserAgent #3', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('HTTP_USER_AGENT', 'XXX', true);
        req.setHeader('HTTP_X_SKYFIRE_PHONE', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });
    
    it('getUserAgent #3', function () {
        var req = new Request();
        var ua = 'Mozilla';
        req.setHeader('HTTP_USER_AGENT', 'XXX', true);
        req.setHeader('HTTP_X_SKYFIRE_PHONE', 'XXX', true);
        req.setHeader('HTTP_X_OPERAMINI_PHONE_UA', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
    });
    
    it('getUserAgent #4', function () {
        var req = new Request();
        expect(DeviceDetector.getUserAgent(req)).toBeFalsy();
    });
    
    it('isFromDesktop #1', function () {
        var req = new Request();
        // firefox
        var ua = 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0';
        req.setHeader('HTTP_USER_AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
    });
    
    it('isFromDesktop #2', function () {
        var req = new Request();
        // iphone
        var ua = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16';
        req.setHeader('HTTP_USER_AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(false);
    });
    
    it('isFromDesktop #3', function () {
        var req = new Request();
        // googlebot
        var ua = 'Googlebot/2.1 ( http://www.googlebot.com/bot.html)';
        req.setHeader('HTTP_USER_AGENT', ua, true);
        expect(DeviceDetector.getUserAgent(req)).toBe(ua);
        expect(DeviceDetector.isDesktop(req)).toBe(true);
    });
    
    it('isFromDesktop List', function () {
        var mobileUA = require('fs').readFileSync('./tests/util/devices.txt', 'utf8').split('\n');
        var fails = 0;
        for(var i=0,max=mobileUA.length;i<max;i++) {
            var req = new Request();
            req.setHeader('HTTP_USER_AGENT', mobileUA[i], true);
            expect(DeviceDetector.getUserAgent(req)).toBe(mobileUA[i]);
            if (DeviceDetector.isDesktop(req) == true) {
                //console.log('FALSE POSITIVE: ' + mobileUA[i]);
                fails += 1;
            }
        }
        console.log('\nUA MISC DETECT FAILS: ' + fails + '/' + mobileUA.length, ((fails/mobileUA.length)*100) + '%');        
        
    });
    
    
    it('isFromDesktop WURFL', function () {
        var mobileUA = require('fs').readFileSync('./tests/util/wurfl-2.3.1.xml', 'utf8');
        var regexp = /user_agent="(.*?)"/gi
        var result = mobileUA.match(regexp);
        var fails = 0;
        var count = 0;
        
        for(var i=0,max=result.length;i<max;i++) {
            // nie matchujemy genericow
            if (result[i].indexOf('DO_NOT_MATCH') > -1) {
                continue;
            }
            count += 1;
            var req = new Request();
            req.setHeader('HTTP_USER_AGENT', result[i], true);
            expect(DeviceDetector.getUserAgent(req)).toBe(result[i]);
            if (DeviceDetector.isDesktop(req) == true) {
                //console.log('FALSE POSITIVE: ' + result[i]);
                fails += 1;
            }
        }
        console.log('\nUA MOBILE DETECT FAILS: ' + fails + '/' + count, Math.round((fails/count)*10000)/100 + '%');
    });
    
    
    it('isFromDesktop Browser Detect', function () {
        var mobileUA = require('fs').readFileSync('./tests/util/useragentstring.browserlist.txt', 'utf8');
        var regexp = /<div id='liste'>([\s\S]+)<\/div>/gi;
        var uaRegexp = /<a.*?>(.*?)<\/a>/gi;
        var uaRegexpSingle = /<a.*?>(.*?)<\/a>/i;
        var result = mobileUA.match(regexp)
        result = result[0].match(uaRegexp);
        
        var fails = 0;
        var count = 0;
        
        for(var i=0,max=result.length;i<max;i++) {
            result[i] = result[i].match(uaRegexpSingle)[1];
            
            // nie matchujemy genericow
            if (result[i].indexOf('More ') == 0) {
                continue;
            }
            
            count += 1;
            var req = new Request();
            
            req.setHeader('HTTP_USER_AGENT', result[i], true);
            expect(DeviceDetector.getUserAgent(req)).toBe(result[i]);
            if (DeviceDetector.isDesktop(req) == false) {
                //console.log('FALSE POSITIVE: ' + result[i]);
                fails += 1;
            }
        }
        console.log('\nUA BIG BROWSERS FAILS: ' + fails + '/' + count, Math.round((fails/count)*10000)/100 + '%');
    });
    
    
});
