var Wick = (window.Wick = window.Wick || {});

Wick.pdfFileLoader = {
    maximumFileSize: 100000,
    fileSizeErrorText: 'File size exceeds the maximum limit',
    fileTypeErrorText: 'File type must be PDF',
    class: {
        disableButtonsClass: 'pdf-loader__buttons--disable',
    },
    fileInput: typeof Wick.fileLoader === 'function' ? Wick.fileLoader('pdf-loader') : null,
    loader: typeof Wick.Loader === 'function' ? new Wick.Loader('.pdf-loader-wrapper') : null,
    elements: {
        buttonsLoader: $('.pdf-loader__buttons'),
        addProjectList: $('#add-to-project-list'),
        convertToCSV: $('#convert-to-csv'),
    },

    enableButton() {
        this.elements.buttonsLoader.removeClass(this.class.disableButtonsClass);
    },
    disableButton() {
        this.elements.buttonsLoader.addClass(this.class.disableButtonsClass);
    },

    validateFile(files) {
        const { name, size } = files[0];

        if (size > Wick.pdfFileLoader.maximumFileSize) {
            Wick.pdfFileLoader.fileInput.showError(Wick.pdfFileLoader.fileSizeErrorText);
            Wick.pdfFileLoader.disableButton();
            return false;
        }

        if (!name.endsWith('pdf')) {
            Wick.pdfFileLoader.fileInput.showError(Wick.pdfFileLoader.fileTypeErrorText);
            Wick.pdfFileLoader.disableButton();
            return false;
        }

        return true;
    },

    bindEvents() {
        Wick.pdfFileLoader.fileInput.getElementInput().change(function () {
            const fileName = this.files[0].name;

            if (this.files.length) {
                if (Wick.pdfFileLoader.validateFile(this.files)) {
                    Wick.pdfFileLoader.fileInput.hideError();
                    Wick.pdfFileLoader.enableButton();
                }

                Wick.pdfFileLoader.fileInput.setLabelText(fileName);
            } else {
                Wick.pdfFileLoader.fileInput.clearFileLoader();
                Wick.pdfFileLoader.disableButton();
            }
        });

        Wick.pdfFileLoader.elements.addProjectList.click(() => {
            Wick.pdfFileLoader.loader.showLoader();
            setTimeout(() => {
                Wick.pdfFileLoader.loader.hideLoader();
                Wick.pdfFileLoader.fileInput.clearFileLoader();
                Wick.pdfFileLoader.disableButton();
            }, 1000);
        });
        Wick.pdfFileLoader.elements.convertToCSV.click(() => {
            Wick.pdfFileLoader.loader.showLoader();
            setTimeout(() => {
                Wick.pdfFileLoader.loader.hideLoader();
                Wick.pdfFileLoader.fileInput.clearFileLoader();
                Wick.pdfFileLoader.disableButton();
            }, 1000);
        });
    },

    init() {
        Wick.pdfFileLoader.bindEvents();
    },
};

$(document).ready(() => {
    if (!$('.pdf-loader-wrapper').length) {
        return;
    }

    Wick.pdfFileLoader.init();

    if (Wick.pdfFileLoader.fileInput.getElementLabel().length) {
        Wick.Responsive.onBreakpoint(function () {
            $clamp(Wick.pdfFileLoader.fileInput.getElementLabel()[0], { clamp: 1 });
        }, 'up-lg');
        Wick.Responsive.onBreakpoint(function () {
            $clamp(Wick.pdfFileLoader.fileInput.getElementLabel()[0], { clamp: 2 });
        }, 'down-lg');
    }
});
