"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var route_1 = require("./route");
var redis = require("redis");
var FilterRoute = (function (_super) {
    __extends(FilterRoute, _super);
    function FilterRoute() {
        return _super.call(this) || this;
    }
    FilterRoute.create = function (router) {
        var _this = this;
        console.log("[FilterRoute::create] Creating filter route.");
        this.client.lrange('imena', 0, -1, function (err, result) {
            _this.seznamImen = result;
        });
        this.client.lrange('priimki', 0, -1, function (err, result) {
            _this.seznamPriimkov = result;
        });
        this.client.lrange('ulice', 0, -1, function (err, result) {
            _this.seznamUlic = result;
        });
        this.client.lrange('filtri', 0, -1, function (err, result) {
            _this.seznamFiltrov = result;
        });
        this.seznamOblik = ['\\bdoo\\b', '\\bsp\\b', '\\bgiz\\b', '\\bdno\\b', '\\bdd\\b',
            '\\bd\\.o\\.o\\.\\b', '\\bs\\.p\\.\\b', '\\bg\\.i\\.z\\.\\b',
            '\\bd\\.n\\.o\\.\\b', '\\bd\\.d\\.\\b'];
        router.get("/filter", function (req, res, next) {
            new FilterRoute().filter(req, res, next);
        });
        router.post("/filter", function (req, res, next) {
            new FilterRoute().filterPost(req, res, next);
        });
        router.post("/filter/bulk", function (req, res, next) {
            new FilterRoute().filterBulk(req, res, next);
        });
    };
    FilterRoute.prototype.filter = function (req, res, next) {
        this.title = "Filter";
        res.redirect('/');
    };
    FilterRoute.prototype.filterPost = function (req, res, next) {
        var _this = this;
        this.title = "Rezultati testa";
        var odgovor = {
            'vsebujeIme': 0,
            'vsebujePriimek': 0,
            'vsebujeUlico': 0,
            'vsebujeObliko': 0,
            'vsebujeDavcno': 0,
            'vsebujeMaticno': 0,
            'vsebujeFilter': 0,
            'najdeniFiltri': [],
            'niz': '<prazen niz>',
            'id': 0,
        };
        FilterRoute.client.lrange('filtri', 0, -1, function (err, result) {
            if (err) {
                console.log(err);
            }
            else {
                FilterRoute.seznamFiltrov = result;
                _this.preveriNiz(req.body.niz, '1234', function (err, odgovor1) {
                    if (err) {
                        console.log(err);
                    }
                    _this.render(req, res, 'index', odgovor1);
                });
            }
        });
    };
    FilterRoute.prototype.filterBulk = function (req, res, next) {
        var _this = this;
        FilterRoute.client.lrange('filter', 0, -1, function (err, result) {
            if (err) {
                console.log(err);
            }
            else {
                var seznamFiltrov = result;
                FilterRoute.seznamFiltrov = result;
                var odgovor = {
                    rezultat: []
                };
                for (var index = 0; index < req.body.nizi.length; index++) {
                    _this.preveriNiz(req.body.nizi[index], req.body.id[index], function (err, result) {
                        odgovor.rezultat.push(result);
                    });
                }
                res.json(odgovor);
            }
        });
    };
    FilterRoute.prototype.preveriNiz = function (niz, id, callback) {
        var mojNiz;
        var napaka = '';
        var odgovor = {
            'vsebujeIme': 0,
            'vsebujePriimek': 0,
            'vsebujeUlico': 0,
            'vsebujeObliko': 0,
            'vsebujeDavcno': 0,
            'vsebujeMaticno': 0,
            'vsebujeFilter': 0,
            'najdeniFiltri': [],
            'niz': '<prazen niz>',
            'id': 0,
        };
        var vse = 7;
        var koncano = 0;
        var opNajdeni = 0;
        odgovor.id = id;
        odgovor.niz = niz;
        var err = '';
        FilterRoute.client.incr('stats:obdelanih', function (err, result) {
            if (err) {
                console.log('error: ' + err.message);
            }
        });
        FilterRoute.seznamImen.forEach(function (ime) {
            var re = new RegExp('\\b' + ime
                .replace(/č/g, 'c')
                .replace(/š/g, 's')
                .replace(/ž/g, 'z')
                .replace(/đ/g, 'd')
                .replace(/ć/g, 'c')
                + '\\b', 'gi');
            if (niz.toLowerCase().replace(/č/g, 'c')
                .replace(/š/g, 's')
                .replace(/ž/g, 'z')
                .replace(/đ/g, 'd')
                .replace(/ć/g, 'c')
                .search(re) != -1) {
                odgovor.vsebujeIme++;
                opNajdeni++;
                FilterRoute.client.incr('stats:vsebujeIme', function (err, result) {
                    if (err) {
                        console.log('error: ' + err.message);
                    }
                });
            }
        });
        FilterRoute.seznamPriimkov.forEach(function (priimek) {
            var re = new RegExp('\\b' + priimek
                .replace(/č/g, 'c')
                .replace(/š/g, 's')
                .replace(/ž/g, 'z')
                .replace(/đ/g, 'd')
                .replace(/ć/g, 'c')
                + '\\b', 'gi');
            if (niz.toLowerCase().replace(/č/g, 'c')
                .replace(/š/g, 's')
                .replace(/ž/g, 'z')
                .replace(/đ/g, 'd')
                .replace(/ć/g, 'c')
                .search(re) != -1) {
                odgovor.vsebujePriimek++;
                opNajdeni++;
                FilterRoute.client.incr('stats:vsebujePriimek', function (err, result) {
                    if (err) {
                        console.log('error: ' + err.message);
                    }
                });
            }
        });
        FilterRoute.seznamUlic.forEach(function (ulica) {
            var re = new RegExp('\\b' + ulica
                .replace(/č/g, 'c')
                .replace(/š/g, 's')
                .replace(/ž/g, 'z')
                .replace(/đ/g, 'd')
                .replace(/ć/g, 'c')
                + '\\b', 'gi');
            if (niz.toLowerCase().replace(/č/g, 'c')
                .replace(/š/g, 's')
                .replace(/ž/g, 'z')
                .replace(/đ/g, 'd')
                .replace(/ć/g, 'c')
                .search(re) != -1) {
                odgovor.vsebujeUlico++;
                opNajdeni++;
                FilterRoute.client.incr('stats:vsebujeUlico', function (err, result) {
                    if (err) {
                        console.log('error: ' + err.message);
                    }
                });
            }
        });
        FilterRoute.seznamOblik.forEach(function (oblika) {
            var re = new RegExp(oblika, "gi");
            if (niz.toLowerCase().search(re) != -1) {
                odgovor.vsebujeObliko++;
                opNajdeni++;
                FilterRoute.client.incr('stats:vsebujeObliko', function (err, result) {
                    if (err) {
                        console.log('error: ' + err.message);
                    }
                });
            }
        });
        if (niz.search(/\b[1-9]\d\d\d\d\d\d\d\b/) != -1) {
            var idavcna;
            var kontrolna;
            var index = niz.search(/\b[1-9]\d\d\d\d\d\d\d\b/);
            var davcna = niz.substring(index, index + 8);
            idavcna = 8 * Number(davcna[0]) +
                7 * Number(davcna[1]) +
                6 * Number(davcna[2]) +
                5 * Number(davcna[3]) +
                4 * Number(davcna[4]) +
                3 * Number(davcna[5]) +
                2 * Number(davcna[6]);
            kontrolna = 11 - (idavcna % 11);
            if ((kontrolna == 10) || (kontrolna == 11)) {
                kontrolna = 0;
            }
            if (kontrolna == Number(davcna[7])) {
                odgovor.vsebujeDavcno++;
                opNajdeni++;
                FilterRoute.client.incr('stats:vsebujeDavcno', function (err, result) {
                    if (err) {
                        console.log('error: ' + err.message);
                    }
                });
            }
        }
        if (niz.search(/\b[0-3]\d[0-1]\d[9|0]\d\d50\d\d\d\d\b/) != -1) {
            var imaticna;
            var kontrolna;
            var index = niz.search(/\b[0-3]\d[0-1]\d[9|0]\d\d50\d\d\d\d\b/);
            var maticna = niz.substring(index, index + 13);
            imaticna = 7 * Number(maticna[0]) +
                6 * Number(maticna[1]) +
                5 * Number(maticna[2]) +
                4 * Number(maticna[3]) +
                3 * Number(maticna[4]) +
                2 * Number(maticna[5]) +
                7 * Number(maticna[6]) +
                6 * Number(maticna[7]) +
                5 * Number(maticna[8]) +
                4 * Number(maticna[9]) +
                3 * Number(maticna[10]) +
                2 * Number(maticna[11]);
            kontrolna = imaticna % 11;
            if (kontrolna != 0) {
                kontrolna = 11 - kontrolna;
            }
            if ((kontrolna == Number(maticna[12])) && (kontrolna != 10)) {
                odgovor.vsebujeMaticno++;
                opNajdeni++;
                FilterRoute.client.incr('stats:vsebujeMaticno', function (err, result) {
                    if (err) {
                        console.log('error: ' + err.message);
                    }
                });
            }
        }
        FilterRoute.seznamFiltrov.forEach(function (filter) {
            var re = RegExp(filter.replace(/\/\//g, '/'), 'gi');
            if (niz.toLowerCase().search(re) != -1) {
                odgovor.vsebujeFilter++;
                opNajdeni++;
                FilterRoute.client.incr('stats:vsebujeFilter', function (err, result) {
                    if (err) {
                        console.log('error:' + err.message);
                    }
                });
                odgovor.najdeniFiltri.push(filter);
            }
        });
        if (opNajdeni == 0) {
            FilterRoute.client.incr('stats:niziBrezOP');
        }
        if (typeof callback === 'function') {
            callback(err, odgovor);
        }
    };
    return FilterRoute;
}(route_1.BaseRoute));
FilterRoute.client = redis.createClient();
FilterRoute.seznamImen = [];
FilterRoute.seznamPriimkov = [];
FilterRoute.seznamUlic = [];
FilterRoute.seznamOblik = [];
FilterRoute.seznamFiltrov = [];
exports.FilterRoute = FilterRoute;
