;
(function () {
    'use strict';
    angular
        .module('app.widgets')
        .directive('chkall', chk_all);

    function chk_all() {
        var directive = {
            link: link,
            restrict: 'AC',
            scope: {
                ngItemCheck: '=',
                ngCheckAll: '=',
                ngContextmenu: '='
            }
        };

        return directive;

        function link(scope, element, attrs) {

            let vm = scope,
                $element = $(element);

            let IS_ShiftDOWN = false,
                IS_Ctr_DOWN = false,
                KEYDOWN_IDX = 0;

            function shiftDown(idx) {
                if (IS_ShiftDOWN) {
                    let start = Math.min(idx, KEYDOWN_IDX),
                        end = Math.max(idx, KEYDOWN_IDX);

                    $element.find('.tr-file').removeClass('active contextmenu_active');
                    for (; end >= start;) {
                        $element.find(`.tr-file:eq(${start++})`).addClass('active');
                    }
                }
            }

            function ifAllCheck() {
                let $tbody = $element.find('#J-tbody');

                let $tr_file = $tbody.find('.tr-file'),
                    $tr_file_active = $tbody.find('.active'),
                    $chk_all = $element.find('.chk-all:eq(0)');

                if ($tr_file.length === $tr_file_active.length) {
                    $chk_all.addClass('active');
                } else {
                    $chk_all.removeClass('active');
                }

                if (typeof vm.ngItemCheck === "function") {
                    return vm.ngItemCheck($tr_file_active.length, getCheckFiles());
                }
                return false;
            }


            scope.$on('$destroy', function () {
                element.off('click', $('.chk-all:eq(0)'));
                element.off('click', '.chk');
                element.off('click', '.tr-file');
                element.off('contextmenu', '.tr-file');
                $(document).off('keydown');
                $(document).off('keyup');
            });

            element.on('click', '.chk-all', function (ev) {
                let $this = $(this),
                    is_all = $this.hasClass('active'),
                    $tr_file = $element.find('.tr-file');

                if (is_all) {
                    $this.removeClass('active');
                    $tr_file.removeClass('active');
                } else {
                    $this.addClass('active');
                    $tr_file.addClass('active');
                }

                if (typeof vm.ngCheckAll === "function") {
                    return vm.ngCheckAll(!is_all, getCheckFiles());
                }
                return false;
            }).on('click', '.chk', function (ev) {
                let idx = $(this).data().index;
                IS_ShiftDOWN || (KEYDOWN_IDX = idx);
                $element.find('.contextmenu_active').removeClass('active contextmenu_active');
                let $this = $(this).parent();

                if ($this.hasClass('active')) {
                    $this.removeClass('active contextmenu_active');
                    shiftDown(idx);
                    return ifAllCheck();
                }

                $this.addClass('active');
                shiftDown(idx);
                return ifAllCheck();
            }).on('click', '.tr-file', function (ev) {

                let idx = $(this).data().index;
                IS_ShiftDOWN || (KEYDOWN_IDX = idx);
                let $this = $(this),
                    $active = $element.find('.active')
                if (!IS_Ctr_DOWN) {
                    $active.removeClass('active contextmenu_active');
                }
                if ($this.hasClass('active') && IS_Ctr_DOWN) {
                    $this.removeClass('active contextmenu_active');
                    return ifAllCheck();
                }

                $this.addClass('active');
                shiftDown(idx);
                return ifAllCheck();
            }).on('contextmenu', '.tr-file', function (ev) {
                let $this = $(this);
                $element.find('.contextmenu_active').removeClass('active contextmenu_active');

                if (!$this.hasClass('active')) {
                    $this.addClass('active contextmenu_active');
                }
                ev.preventDefault();

                //   vm.contextmenu.call(scope, ev, tbody, fileTool, $mdDialog, $timeout);
                if (typeof vm.ngContextmenu === "function") {
                    return vm.ngContextmenu(ev, getCheckFiles());
                }
                return ifAllCheck();
            });

            $(document).on('keydown', function (e) {
                if (e.key === "Shift") {
                    IS_ShiftDOWN = true;
                    return;
                }
                if (e.key === 'Control') {
                    IS_Ctr_DOWN = true;
                }
            }).on('keyup', function (e) {
                IS_ShiftDOWN = false;
                IS_Ctr_DOWN = false;
            });

            function getCheckFiles() {
                let tbody = $element.find('#J-tbody');
                let tr_file_active = [...tbody.find('.active')];
                let files = [];
                return tr_file_active.map((v) => {
                    let data = $(v).data();
                    return (data['$scope'].file);
                });
            }
        }


    }
})();
