'use strict';

function Waiver() {
    const _self = this;

    this.waiverList = null;
    this.answeredWaiverCount = 0;
    this.callbackFunction = null;
    this.callBackFunctionFail = null;

    this.get = function(successFunction) {
        let lUrlApi = getRootApi() + 'waiver/shopingCartRequest';
        console.log(lUrlApi);

        $.ajax({
            type: 'GET',
            url: lUrlApi,
            headers: _Tokken.getHeaders()
        }).done(function (data) {
            console.log('Liste des waivers');
            console.log(data);
            if (successFunction) {
                successFunction(data);
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {
            if (_CallbackError) {
            _CallbackError(jqXHR, textStatus, errorThrown, 'errorAPI');
            }
        });
    };

    this.postResponse = function(info, successFunction) {
        let lUrlApi = getRootApi() + 'waiver/shopingCartResponse';
        console.log(lUrlApi);

        $.ajax({
            type: 'POST',
            url: lUrlApi,
            contentType: 'application/json',
            data: JSON.stringify(info),
            headers: _Tokken.getHeaders()
        }).done(function (data) {
            console.log(data);
            _self.answeredWaiverCount += 1;
            if (successFunction) {
                successFunction(data);
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {
            if (_CallbackError) {
            _CallbackError(jqXHR, textStatus, errorThrown, 'errorAPI');
            }
        });
    };

    this.getResponse = function(successFunction) {
        let lUrlApi = getRootApi() + 'waiver/shopingCartResponse';
        console.log(lUrlApi);

        $.ajax({
            type: 'GET',
            url: lUrlApi,
            headers: _Tokken.getHeaders()
        }).done(function (data) {
            console.log('Liste des waivers-responses');
            console.log(data);
            if (successFunction) {
                successFunction(data);
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {
            if (_CallbackError) {
            _CallbackError(jqXHR, textStatus, errorThrown, 'errorAPI');
            }
        });
    };

    this.guidExistInResponse = function(guid, responseList) {
        if (guid && responseList && responseList.length > 0) {
            const fLen2 = responseList.length;

            for (let j = 0; j < fLen2; j++) {
                if (guid === responseList[j].itemGuid) {
                    return true;
                }
            }
        }

        return false;
    };

    this.getResponseSuccess = function (responseList) {
        const fLen = _self.waiverList.length;

        // La liste existe toujours même si vide...
        if (responseList) {
            _self.answeredWaiverCount = responseList.length;

            document.querySelector('.contracts-container .contracts').innerHTML = '';

            for (let i = 0; i < fLen; i++) {
                let existInResponse = _self.guidExistInResponse(_self.waiverList[i].itemGuid, responseList);
                let txt = '';
                if (existInResponse) {
                    txt = '<span class="c-success"><i class="fas fa-check-circle mr-3"></i>' + (langue === 'fr' ? 'Signé' : 'Signed') + '</span>';
                } else {
                    txt = '<a href="#" onclick="displayWaiver(\'' + _self.waiverList[i].itemGuid +  '\', ' + _self.waiverList[i].waiverProductId + ', \'' + _self.waiverList[i].question + '\', \'' + _self.waiverList[i].formulairePng + '\')">' + (langue === 'fr' ? 'Voir et accepter les conditions' : 'See and accept conditions') + '</a>';
                }

                let html = '' +
                    '<div class="col-12 col-md-6 col-lg-4 mb-3" data-guid="' + _self.waiverList[i].itemGuid + '">' +
                        '<div class="s-box ticket-box">'+
                            '<div class="media">' +
                                '<img class="img-fluid mr-3" src="' + getCustomerPicturePath(_self.waiverList[i].customerPicture) + '" alt="' + (langue === 'fr' ? 'Photo du membre' :  'Member picture') + '" width="50px">' +
                                '<div class="media-body">' +
                                    '<span class="name">' + _self.waiverList[i].customerFirstName + '</span><br>' +
                                    '<span>' + _self.waiverList[i].description + '</span>' +
                                '</div>' +
                            '</div>' +
                            '<div class="mt-3 is-signed">' + txt + '</div>' +
                        '</div>' +
                    '</div>';

                $('.contracts-container .contracts').append(html);
            }

            if (responseList.length === _self.waiverList.length) { document.querySelector('.btn-confirm-waiver').removeAttribute('disabled'); }

            _self.callbackFunction();
        }
    };

    this.getRequestSuccess = function(wList) {
        _self.waiverList = wList;

        if (_self.waiverList) {
            _self.getResponse(_self.getResponseSuccess);
            return;
        }

        console.log('pas de waiver... on fait quoi???');
        if (_self.callBackFunctionFail) { _self.callBackFunctionFail(1); }
    };

    this.checkForWaiver = function(cbFunction, cbFunctionFail) {
        _self.callbackFunction = cbFunction;
        _self.callBackFunctionFail = cbFunctionFail;

        //On appelle le get pour savoir si on doit ensuite aller chercher les réponses
        _self.get(_self.getRequestSuccess);
    };
}

const _Waiver = new Waiver();
