class FinishTrip {
    static show(trip, callbackClose) {
        if (trip.tripCallTypeId === TRIP_CALL_CORPORATE)
            return new Promise(resolve => {
                Session.delete("tripDriver");
                SwalCustom.messageDialog("", "Viagem concluida!", () => {
                }, "success");
                resolve();
                PreloadRequest.clear();
                callbackClose();
            });

        return new Promise(resolve => {
            const elementProperty = new ElementProperty();

            elementProperty.visibleElements([".modal-finish"], true);
            document.querySelector(".updateBalanceNow").classList.add("active");
            elementProperty.getElement(".steering-wheel-animation", element => {
                element.classList.add("stop");
            });

            if (trip.recalculatedPrice != null) {
                document.querySelector(".obs-trip-finished").innerHTML = trip.recalculatedPrice.message || "";
                document.querySelector(".obs-trip-finished").classList.add("active");
            } else {
                document.querySelector(".obs-trip-finished").innerHTML = "";
                document.querySelector(".obs-trip-finished").classList.remove("active");
            }
            if (!trip.priceRange) {
                document.querySelector("#typeTripPriceNormal").style.display = "block";
                document.querySelector("#typeTripPriceRange").style.display = "none";
            } else {
                document.querySelector("#typeTripPriceNormal").style.display = "none";
                document.querySelector("#typeTripPriceRange").style.display = "block";

                document.querySelector("#descriptionFinishedTrip").innerHTML = trip.priceRange.description;
            }

            if (trip.tripCallTypeId === TRIP_CALL_TAXIMETER) {
                viewController.elementProperty.getElement(".info-trip", element => element.style.display = "none");
                FinishTrip.close(trip, callbackClose);
            } else {
                viewController.elementProperty.getElement(".info-trip", element => element.style = "");
            }

            viewController.elementProperty.getElement("#nameClientFinish", element => {
                if (trip.customer != null)
                    element.innerHTML = trip.customer.name;
            });

            viewController.elementProperty.getElement(".match-finished", element => {
                element.innerHTML = trip.origin.address;
            });

            viewController.elementProperty.getElement(".destiny-finished", element => {
                element.innerHTML = trip.destination.name;
            });

            viewController.elementProperty.getElement("#tripPaymentForm", element => {
                element.innerHTML = trip.tripPaymentForm;
            });

            showBonusTripFinish(trip);

            Session.delete("tripDriver");

            resolve();

            elementProperty.getElement(".image-profile-driver", element => {
                const config = Session.get("configGeneral");
                const picturesLink = config.find(item => {
                    return item.key === 'link_storage_pictures';
                });

                const profileImage = trip.customer.profileImage
                const finalLink = `${picturesLink.value}customer/profile/compressed/${profileImage}`;

                if (trip.customer === null || trip.customer.profileImage === null) {
                    element.src = 'webfiles/img/icon/default.png';
                    return;
                }
                // element.src = CLIENT_PROFILE + trip.customer.profileImage;
                element.src = finalLink;
            });

            elementProperty.getElement(".container-img", element => {
                if (trip.customer === null || trip.customer.profileImage === null) {
                    element.style.backgroundImage = 'url(webfiles/img/icon/default.png)';
                    return;
                }
                // element.style.backgroundImage = `url(${CLIENT_PROFILE + trip.customer.profileImage})`;
                element.style.backgroundImage = `url(${finalLink})`;
            });

            elementProperty.getElement(".name-client-trip", element => {
                if (trip.customer === null){
                    return;
                }
                element.innerHTML = trip.customer.nickname;
            });

            PreloadRequest.clear();

            FinishTrip.close(trip, callbackClose);
        });
    }

    static close(trip, callback) {

        const elementProperty = new ElementProperty();

        elementProperty.addEventInElement(".close-trip-finish", "onclick", function () {
            elementProperty.visibleElements(['.modal-finish'], false);
            /*elementProperty.getElement(".steering-wheel-animation", element => {
                element.classList.remove("stop");
                callback();
            });*/

            setMonitoring(false);

            callback();


            if (trip.tripCallTypeId === TRIP_CALL_PARTNER)
                return;

            if (trip.tripCallTypeId === TRIP_CALL_KNOB)
                return;

            if (trip.tripCallTypeId === TRIP_CALL_TAXIMETER)
                return;

            const evaluation = new Evaluation();

            const config = Session.get("cityConfig");

            if (config.showUserRating == false) {
                return evaluation.hidden();
            }

            function evaluateLocation(){
                const places = [];
                document.querySelectorAll(".blacklist-address-selected.active").forEach(element => {
                    places.push(JSON.parse(element.getAttribute("label")));
                });

                if (places.length > 0)
                    AddressBlacklistController.insert(places);
            }

            function reportAreaRisk(){
                document.querySelectorAll(".risk-area-address-selected.active").forEach(element => {
                    const result = JSON.parse(element.getAttribute("label"));

                    AddressBlacklistController.riskArea({
                        address: result.address,
                        latitude: parseFloat(result.latitude),
                        longitude: parseFloat(result.longitude),
                        tripReference: trip.tripReference,
                        userId: Session.get("user").idUser
                    })
                });
            }

            evaluation.show(trip, function (rating) {

                TripRatingController.rating(rating, trip.tripReference);

                evaluateLocation();

                reportAreaRisk();

                evaluation.hidden();
            })
        });
    }
}