/* global AJS:false */
AJS.test.require("com.atlassian.bitbucket.server.bitbucket-web:filefinder", function() {

    var files = [
        // Camel Casing
        "fruits/citrus/Apple.java",
        "fruits/juices/PeachJuice.java",
        "fruits/juices/PearJuice.java",
        // Different breaks
        "desserts/creme-brulee.js",
        "desserts/creme_brulee.js",
        "desserts/creme.brulee.js",
        // Long path
        "beverage/alcohol/beer/heineken.pint",
        // Regex escaped
        "beverage/a][\\^$.|?*+)(.js",
        // Ranking filename matches
        "icecream/vanilla.soy",
        "icecream/chocolate.soy",
        "desserts/icecream.soy",
        // Ranking exact matches
        "test/StashRestPlugin.xml",
        "Stash/Plugin/pom.xml",
        "plugin/StashPlugin.xml",
        "'\"><script>alert(666)<:script>.txt"
    ];

    var FileFinder;
    var fileFinder;

    function getFilename(filepath) {
        return filepath.substring(filepath.lastIndexOf('/') + 1);
    }

    module('file-finder', {
        setup: function () {
            FileFinder = require('bitbucket/internal/feature/filebrowser/file-finder').FileFinder;
            fileFinder = new FileFinder();

            sinon.stub(FileFinder.prototype, "_makeFileFinderView", function () {
                return {
                    init: function () {
                        return false;
                    }
                };
            });

            fileFinder.files = files.map(function(f) {
                return {
                    name: f
                };
            });

        },
        teardown: function () {
            FileFinder.prototype._makeFileFinderView.restore();
            fileFinder = undefined;
            FileFinder = undefined;
        }
    });

    test('Filter by extension type', function () {
        fileFinder.showFiles('.java');
        strictEqual(fileFinder.filteredFiles.length, 3);
    });

    test('Filter by path', function () {
        fileFinder.showFiles('/juices');
        strictEqual(fileFinder.filteredFiles.length, 2);
    });

    test('Filename separated by different characters', function () {
        fileFinder.showFiles('cr-br');
        strictEqual(fileFinder.filteredFiles.length, 1);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "creme-brulee.js", "Expected to find creme-brulee.js");

        fileFinder.showFiles('cr_br');
        strictEqual(fileFinder.filteredFiles.length, 1);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "creme_brulee.js", "Expected to find creme_brulee.js");

        fileFinder.showFiles('cr.br');
        strictEqual(fileFinder.filteredFiles.length, 1);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "creme.brulee.js", "Expected to find creme.brulee.js");
    });

    test('CamelCasing of filename', function () {
        fileFinder.showFiles('PeaJ');
        strictEqual(fileFinder.filteredFiles.length, 2);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "PeachJuice.java", "Expected to find PeachJuice.java");
        strictEqual(getFilename(fileFinder.filteredFiles[1].name), "PearJuice.java", "Expected to find PearJuice.java");
    });

    test('Space separation', function () {
        fileFinder.showFiles('beverage /beer hein .pint');
        strictEqual(fileFinder.filteredFiles.length, 1);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "heineken.pint", "Expected to find heineken.pint");
    });

    test('Regex characters', function () {
        fileFinder.showFiles('a][\\^$.|?*+)(.js');
        strictEqual(fileFinder.filteredFiles.length, 1);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "a][\\^$.|?*+)(.js", "Expected to find a][\\^$.|?*+)(.js");
    });

    test('Ranking of results', function () {
        fileFinder.showFiles('icecream');
        strictEqual(fileFinder.filteredFiles.length, 3);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "icecream.soy", "Expected to find icecream.soy");
        strictEqual(getFilename(fileFinder.filteredFiles[1].name), "chocolate.soy", "Expected to find chocolate.soy");
        strictEqual(getFilename(fileFinder.filteredFiles[2].name), "vanilla.soy", "Expected to find vanilla.soy");
    });

    test('Ranking of exact matches', function () {
        fileFinder.showFiles('StashPlugin');
        strictEqual(fileFinder.filteredFiles.length, 3);
        strictEqual(getFilename(fileFinder.filteredFiles[0].name), "StashPlugin.xml", "Expected to find StashPlugin.xml");
        strictEqual(getFilename(fileFinder.filteredFiles[1].name), "StashRestPlugin.xml", "Expected to find StashRestPlugin.xml");
        strictEqual(getFilename(fileFinder.filteredFiles[2].name), "pom.xml", "Expected to find pom.xml");
    });

    test('Empty filter', function () {
        fileFinder.showFiles('');
        strictEqual(fileFinder.filteredFiles.length, files.length, "Expected all files to be there");
    });

    test('Highlight matches', function () {
        var pattern = FileFinder.getPattern('blah');
        strictEqual(FileFinder.highlightMatches(pattern, 'nothing.txt'), 'nothing.txt', "not matches expected");
        strictEqual(FileFinder.highlightMatches(pattern, 'dir/blah.txt'), 'dir/<mark>blah</mark>.txt', "Match term");

        pattern = FileFinder.getPattern(null);
        strictEqual(FileFinder.highlightMatches(pattern, 'dir/null.txt'), 'dir/null.txt', "null should not be highlighed");
    });

    test('Filename is escaped', function () {
        fileFinder.showFiles('<script');
        strictEqual(fileFinder.filteredFiles.length, 1);
    });

});