(function (paw) {
    function NewRecordModalComponent() {
        this.removedDeleteAction = null;
        this.removedCreateAction = null;
        this.data = null;

        this.$employeesOptionsOrigin = null;
        this.permissions = null;
        this.activeContextId = null;
    }

    NewRecordModalComponent.prototype.setup = function (data, permissions) {
        this.data = data;
        this.permissions = permissions;
        this.initEmployeesDropdown();
        return this;
    };

    NewRecordModalComponent.prototype.getActivePermissions = function () {
        return this.permissions[this.activeContextId];
    };

    NewRecordModalComponent.prototype.init = function (contextId) {
        this.activeContextId = contextId;
        this.handleEmployeesDropdownPermissions();
        this.handleDeleteActionPermissions();
        this.handleCreateActionPermissions();
    };
    
    NewRecordModalComponent.prototype.initEmployeesDropdown = function () {
        let self = this;
        this.$employees = $('#NewRecordEmployee');
        this.$employeesOptionsOrigin = this.$employees.find('option').clone();
        this.$employees.on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
            self.handleDeleteActionPermissions();
        });
    };

    NewRecordModalComponent.prototype.handleEmployeesDropdownPermissions = function () {
        let self = this;
        let permissions = this.getActivePermissions();

        if (paw.utils.value.isSet(this.$employeesOptionsOrigin)) {
            this.$employees.empty().append(this.$employeesOptionsOrigin);
        }

        this.$employees.attr('disabled', (permissions.isOwnData && !permissions.isLeader) || !permissions.canUpdateOthers);

        if (!permissions.canUpdateOwn) {
            this.$employees.find('option[value="' + this.data.workerCode + '"]').remove();
        }
        if (!permissions.canUpdateOthers) {
            this.$employees.find('option').each(function () {
                let $item = $(this);
                if ($item.val() === self.data.workerCode && permissions.canUpdateOwn) {
                    return;
                }
                $item.remove();
            });
        }

        this.$employees.selectpicker('refresh');
        
    };

    NewRecordModalComponent.prototype.handleDeleteActionPermissions = function () {
        let permissions = this.getActivePermissions();
        let selectedEmployees = this.$employees.val();
        if (paw.utils.value.isSet(this.removedDeleteAction)) {
            this.removedDeleteAction.$prev.after(this.removedDeleteAction.node);
            this.removedDeleteAction = null;
        }

        let canDelete = false;

        // Can delete only own
        if (permissions.isOwnData) {
            canDelete = permissions.canDeleteOwn;
            // console.log("Can delete only own", canDelete);
        }
        // Can delete own but not others
        else if (permissions.canDeleteOwn && !permissions.canDeleteOthers) {
            canDelete = selectedEmployees.length === 1 && selectedEmployees.indexOf(this.data.workerCode) > -1;
            // console.log("Can delete own, but not others", canDelete);
        }
        // Can delete others but not own
        else if (permissions.canDeleteOthers && !permissions.canDeleteOwn) {
            canDelete = selectedEmployees.indexOf(this.data.workerCode) === -1;
            // console.log("Can delete others, not own", canDelete);
        }
        // Can delete others
        else if (permissions.canDeleteOthers) {
            canDelete = true;
            // console.log("Can delete otrhers");
        }

        if (!canDelete) {
            let $deleteAction = $('#newRecordModalDelete');
            this.removedDeleteAction = {
                $parent: $deleteAction.parent(),
                $prev: $deleteAction.prev(),
                node: $deleteAction.get(0)
            };
            $deleteAction.remove();
        }
    };

    NewRecordModalComponent.prototype.handleCreateActionPermissions = function () {
        let permissions = this.getActivePermissions();
        var $createAction = $('#newrecord-modal-add');
        if (paw.utils.value.isSet(this.removedCreateAction)) {
            this.removedCreateAction.$prev.after(this.removedCreateAction.node);
            this.removedCreateAction = null;
        }

        if (!permissions.canUpdateOwn && !permissions.canUpdateOthers) {
            this.removedCreateAction = {
                $parent: $createAction.parent(),
                $prev: $createAction.prev(),
                node: $createAction.get(0)
            };
            $createAction.remove();
        }

    };

    paw.view.workhours.newRecordModal = new NewRecordModalComponent();
})(paw);