/*global define*/
/**
 * Sends a request for validate,cancel or Schedule a pickup request to USPS.
 *
 * @class de_epages.usps.ui.packagepickup
 *
 * @uses jQuery
 * @uses ep
 * @uses ep.ajax
 *
 * @since 6.17.2
 *
 */
define('de_epages/usps/ui/packagepickup', [
    'jquery',
    'ep',
    '$dict!ep/dict',
    '$ready!',
    'ep/ajax',
    'ep/fn/busy'
], function ($, ep, epDict) {
    'use strict';
    /**
     * @param {jQuery}  PickUpFields              The DOM node that contains all the used elements.
     * @param {String}  ShippingLabelID           Number of the shipping label affected.
     * @param {jQuery}  PickupDateDiv             The DOM node of the Pick up Date.
     * @param {jQuery}  PackageLocation           The DOM node of the input which contains the package location.
     * @param {jQuery}  SpecialInstructions       The DOM node of the input which contains the Special Instructions.
     * @param {jQuery}  PickUpInfo                The DOM node of the div element that contains the messages.
     * @param {jQuery}  ConfirmationNumberdiv     The DOM node of the div element that contains confirmation number.
     * @param {String}  PickUpRequestButton       The DOM node of the button that leads to do a request.
     * @param {String}  PickUpCancelButton        The DOM node of the button that leads to cancel a request.
     * @param {String}  PickUpAvailabilityButton  The DOM node of the button that leads to check the availability for the request.
     */
    return function (PickUpFields, ShippingLabelID, PickupDateDiv, PackageLocation, SpecialInstructions, PickUpInfo, ConfirmationNumberdiv,
        PickUpRequestButton, PickUpCancelButton, PickUpAvailabilityButton) {

        PickUpRequestButton.on('click', function () {
            PickUpFields.busy('show');
            ep.ajax({
                data: {
                    ObjectID: ShippingLabelID,
                    PackageLocation: PackageLocation.val(),
                    SpecialInstructions: SpecialInstructions.val(),
                    ChangeAction: 'JSONUSPSPickUpRequest'
                },
                cache: false,
                type: "POST",
                dataType: "json"
            }).done(function (data) {
                PickUpFields.busy('hide');
                if (data.ReturnCode === 1) {
                    PickUpInfo.empty()
                        .append(data.ErrorDesc)
                        .attr('class', 'DialogMessage MessageWarning');
                } else {

                    PickUpInfo.empty()
                        .append('Confirmation Number: ' + data.ReturnMessage)
                        .attr('class', 'DialogMessage MessageInfo');

                    ConfirmationNumberdiv.empty()
                        .append(data.ReturnMessage);

                    PickupDateDiv.empty()
                        .append(data.ScheduledDay);

                    PackageLocation.attr('disabled', 'disabled');
                    SpecialInstructions.attr('disabled', 'disabled');
                    PickUpRequestButton.attr('disabled', 'disabled');
                    PickUpCancelButton.removeAttr("disabled");
                    PickUpAvailabilityButton.attr('disabled', 'disabled');
                }
            }).fail(function (errJSON) {
                PickUpFields.busy('hide');
                var errMessage,
                thisJSON = $.parseJSON(errJSON.responseText);
                if(thisJSON.Errors[0].Reason == 'FORMAT_NOT_INTEGER'){
                    errMessage = epDict.translate('{PickupLocationNotValid}');
                } else {
                    errMessage = thisJSON.Errors[0].Reason;
                }
                PickUpInfo.empty()
                    .append(errMessage)
                    .attr('class', 'DialogMessage MessageWarning');
            });
        });

        PickUpCancelButton.on('click', function () {
            PickUpFields.busy('show');
            ep.ajax({
                data: {
                    ObjectID: ShippingLabelID,
                    ChangeAction: 'JSONUSPSPickUpCancelRequest'
                },
                cache: false,
                type: "POST",
                dataType: "json"
            }).done(function (data) {
                PickUpFields.busy('hide');
                if (data.ReturnCode === 1) {
                    PickUpInfo.empty()
                        .append(data.ErrorDesc)
                        .attr('class', 'DialogMessage MessageWarning');
                } else {
                    PickUpInfo.empty()
                        .append(data.ReturnMessage)
                        .attr('class', 'DialogMessage MessageInfo');

                    ConfirmationNumberdiv.empty()
                        .append('Canceled');

                    PickupDateDiv.empty()
                    .append('-');
                    PackageLocation.removeAttr("disabled");
                    SpecialInstructions.removeAttr("disabled");
                    PickUpRequestButton.removeAttr("disabled");
                    PickUpCancelButton.attr('disabled', 'disabled');
                    PickUpAvailabilityButton.removeAttr("disabled");
                }
            }).fail(function (errJSON) {
                PickUpFields.busy('hide');
                var thisJSON = $.parseJSON(errJSON.responseText),
                    errMessage = thisJSON.Errors[0].Reason;
                PickUpInfo.empty()
                    .append(errMessage)
                    .attr('class', 'DialogMessage MessageWarning');
            });
        });

        PickUpAvailabilityButton.on('click', function () {
            PickUpFields.busy('show');
            ep.ajax({ // AJAX request
                data: {
                    ObjectID: ShippingLabelID,
                    PackageLocation: PackageLocation.val(),
                    SpecialInstructions: SpecialInstructions.val(),
                    ChangeAction: 'JSONUSPSPickUpAvailabilityRequest'
                },
                cache: false,
                type: "POST",
                dataType: "json"
            }).done(function (data) {
                PickUpFields.busy('hide');
                if (data.ReturnCode === 1) {
                    PickUpInfo.empty()
                        .append(data.ErrorDesc)
                        .attr('class', 'DialogMessage MessageWarning');
                } else {
                    var message = 'Day:' + data.DayOftheWeek + ' Date:' + data.ReturnedDate + ' Carrier Route:' + data.CarrierRoute;
                    PickUpInfo.empty()
                        .append(message)
                        .attr('class', 'DialogMessage MessageInfo');
                }
            }).fail(function (errJSON) {
                PickUpFields.busy('hide');
                var errMessage,
                thisJSON = $.parseJSON(errJSON.responseText);
                if(thisJSON.Errors[0].Reason == 'FORMAT_NOT_INTEGER'){
                    errMessage = epDict.translate('{PickupLocationNotValid}');
                } else {
                    errMessage = thisJSON.Errors[0].Reason;
                }
                PickUpInfo.empty()
                    .append(errMessage)
                    .attr('class', 'DialogMessage MessageWarning');
            });
        });
    };
});