const STATUS_PRE_ACCEPTED = "pre_accepted";

const STATUS_ACCEPTED = "accepted";

const STATUS_SCHEDULED = "scheduled";

const STATUS_IN_PROGRESS = "in_progress";

const STATUS_BOARDING_TRIP = "boarding";

const STATUS_FINISH_TRIP = "finished";

const STATUS_CANCELED = "canceled";

const STATUS_SEARCHING = "searching";

const STATUS_GET_BID_BY_DRIVERS    = "get_bid_by_drivers";

const STATUS_SEARCHING_FAVORITE = "searching_favorite";

const TRIP_CALL_KNOB = "2";

const TRIP_CALL_PARTNER = "3";

const TRIP_CENTRAL = "4";

const TRIP_FREIGHT = "6";

const TRIP_CALL_TAXIMETER = "9";

const TRIP_CALL_RIDE = "7";

const TRIP_CALL_CORPORATE = "10";

const managerGetTrip = {
    timeout: null,
    requestTrip: false
};

const managerTripInWait = {
    timeout: null,
};

function errorStatusTrip(objectStatus) {
    if (objectStatus == null) {
        PreloadRequest.progress("Ocorreu um erro no status dessa viagem");
        clearDataTrip();
        return true;
    }

    if (objectStatus.callback === false) {
        PreloadRequest.progress("Viagem já está finalizada");
        clearDataTrip();
        return true;
    }

    return false;
}


function checkCancellationFeeByCustomer(userId) {
    return new Promise((resolve, reject) => {
        ConnectionServer.sendRequest("Customer/SearchValueCancellationFee", "POST", {userId}, resolve, reject, undefined, false)
    });
}

function showDataTrip() {
    const elementProperty = new ElementProperty();

    const trip = Session.get("tripDriver");

    if (trip.length === 0)
        return;

    const objectStatus = checkStatusTrip(trip);

    if (errorStatusTrip(objectStatus))
        return;

    viewController.elementProperty.removeClass(".container-advertising-banner","active");

    if (trip.tripCallTypeId === TRIP_CALL_TAXIMETER && trip.status === STATUS_IN_PROGRESS) {
        const hasWaypointNotConfirm = trip.tripWayPoints.find(item=>{
            return item.confirmed == false;
        });

        if(hasWaypointNotConfirm || trip.tripWayPoints.length === 0)
            return createLayoutInTrip();

        viewController.elementProperty.removeClass(".footer-race", "hidden-to-taximetro");
    } else {
        viewController.elementProperty.addClass(".btn-taximetro", "active")
    }

    if (trip.tripCallTypeId === TRIP_CALL_TAXIMETER) {
        viewController.elementProperty.addClass(".list-place-trip", "without-destination");
    } else {
        viewController.elementProperty.removeClass(".list-place-trip", "without-destination");
    }

    if (trip.tripCallTypeId === TRIP_FREIGHT) {
        viewController.elementProperty.getElement("#detailFreight", element => element.style = "");
    } else {
        viewController.elementProperty.getElement("#detailFreight", element => element.style.display = "none");
    }

    if (trip.tripCallTypeId === TRIP_CALL_CORPORATE || trip.tripCallTypeId === TRIP_CALL_RIDE) {
        if(trip.tripCallTypeId === TRIP_CALL_RIDE){
            viewController.elementProperty.getElement("#showRouteInApp",element=>{
                element.style = "";
            });
        }else{
            viewController.elementProperty.getElement("#showRouteInApp",element=>{
                element.style.display = "none";
            });
        }
        viewController.elementProperty.addClass(".map", "disabled-map-corporate", "lite-map-corporate");
        document.querySelector("#openChat").style.display = "none";
        document.querySelector("#cancelTrip").style.display = "none";
        const chatRepository = new ChatRepository();
        chatRepository.getLastMessageUnread(trip.tripReference, item => {
            if (item.userId != Session.getValueInSession("user", "idUser")) {
                document.querySelector("#openCustomerChatRider").classList.add("active");
                viewController.elementProperty.getElement('#songNewMessageChat', element => {
                    element.play();
                });
            }
        })
    } else {
        document.querySelector("#openChat").style = "";
        document.querySelector("#cancelTrip").style = "";
    }


    const hideElements = [
        ".toggle-status-driver",
        ".container-option-detail-status-driver"
    ];

    const showElements = [
        ".data-trip",
        ".status-running"
    ];

    clearSearchinCarMap();

    elementProperty.getElement(".info-trip", element => element.style = "");

    elementProperty.getElement(".footer-race", element => {
        if (!element.classList.contains("active"))
            element.classList.add("active");

        element.classList.add("min-v2");
        element.classList.add("in-trip-active");
    });

    elementProperty.getElement(".footer-arrow", element => {
        element.style.display = "none";
    });

    if (trip.tripReference === Session.get("tripInWait")) {
        openInfoTripInWait(trip);
    }

    elementProperty.visibleElements(hideElements, false);

    elementProperty.visibleElements(showElements, true);

    mapService.existTrip = true;

    updateWaypoints();

    elementProperty.removeClass(".footer-race", "minimize");

    showDataCustomerTrip(trip);
    //showBonusTripFinish(trip);
    showPlaceTrip(trip);

    if (typeof initTripRide === "function")
        initTripRide(trip);

    checkCancellationFeeByCustomer(trip.userId).then(({data}) => {
        Session.set("valueCancellationFeeByCustomer", data);
    });

    viewController.elementProperty.addClass(".button-float-datasheet", "active");

    changeStatusNameTrip(objectStatus.message, objectStatus.status);

    if (chatNotification)
        chatNotification.observerNotification(trip, () => {
            viewController.addParams("chat-trip", Session.get("tripDriver"));
            Route.redirectDynamic("Driver", "Chat");
            clearNotification();
        });

    updateRoute();

    updateDataTrip(trip.tripReference);

    viewController.elementProperty.addClass(".schedule-bar", "hidden")
    viewController.elementProperty.removeClass("#map", "disabled")
    viewController.elementProperty.removeClass(".content-only-offer-driver-modal", "active");

    changePostTemplate();

}

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

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

    viewController.elementProperty.addEventInElement(".list-place-trip", "onclick", function () {
        if (trip.tripCallTypeId !== TRIP_CALL_RIDE)
            InfoClientTrip.showDataClientTrip(trip);
    });
}

function showDataCustomerTrip(trip) {
    if (trip.tripCallTypeId === TRIP_CALL_CORPORATE || trip.tripCallTypeId === TRIP_CALL_RIDE) {
        viewController.elementProperty.getElement(".container-info-customer-trip", element => {
            element.style.opacity = "0";
            element.style.paddingBottom = "0px";
            element.style.marginBottom = "0px";
        });
        return;
    } else {
        viewController.elementProperty.getElement(".container-info-customer-trip", element => {
            element.style = ""
        });
    }
    viewController.elementProperty.getElement(".profile-image-customer", element => {
        if (trip.customer != null) {
            if (trip.customer.profileImage == null) {
                element.style.backgroundImage = "url('webfiles/img/icon/default.jpg')";
                return;
            }
        } else {
            element.style.backgroundImage = "url('webfiles/img/icon/default.jpg')";
            return;
        }

        const config = Session.get("configGeneral");
        const picturesLink = config.find(item => {
            return item.key === 'link_storage_pictures';
        });
        
        if (trip.tripCallTypeId !== TRIP_CENTRAL) {

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

            // element.style.backgroundImage = `url('${CLIENT_PROFILE + trip.customer.profileImage}')`;
            element.style.backgroundImage = `url('${finalLink}')`;
        } else {
            // element.style.backgroundImage = `url('${PATH_IMAGE_CENTRAL + trip.customer.profileImage}')`;
            element.style.backgroundImage = `url('${picturesLink.value}${trip.customer.profileImage}')`;
        }
    });

    viewController.elementProperty.getElement("#nameClient", element => {
        if (trip.tripCallTypeId !== TRIP_CENTRAL) {
            if (trip.customer != null)
                element.innerHTML = trip.customer.name;
        } else {
            element.innerHTML = "Central"
        }
    });

    viewController.elementProperty.getElement(".container-qrcode-driver", element => {
        element.classList.remove("active");
        element.style.display = "none";
    });

    viewController.elementProperty.addEventInElement(".point-minimize", "onclick", () => {
        viewController.elementProperty.getElement(".footer-race", element => {
            if (element.classList.contains("minimize")) {
                element.classList.remove("minimize");
            } else {
                element.classList.add("minimize");
            }
        });
    });
}

function showBonusTripFinish(trip) {
    viewController.elementProperty.getElement(".value-trip", element => {

        const valueOffline = Session.get("infoUpdateTaximeter");

        if(!Array.isArray(valueOffline)){
            element.innerHTML = `${CityConfig.getTypeCoin()}${Mask.maskMoney(parseFloat(valueOffline.price))}`;
            return;
        }
        
        viewController.elementProperty.removeClass(".bonus-trip", "active");

        const hasDiscountCoupon = trip?.tripDiscountCoupon?.value == '100' && trip?.tripDiscountCoupon?.valueType == '%';

        if (trip.rewardTrip != null || hasDiscountCoupon ) {
            viewController.elementProperty.getElement("#infoTripFinishBonus", element => {
                const config = Session.get("configGeneral");
                const textBonusFinishTrip = config?.find(item => {
                    return item?.key === 'text_the_end_trip_with_bonus';
                });

                element.innerHTML = textBonusFinishTrip?.value || 
                `   <span data-i18n="finish-trip-value-pay-by"></span>
                    <nameapp></nameapp>
                `
            });
            
            viewController.elementProperty.getElement(".value-bonus", element => {
                element.innerHTML = `${CityConfig.getTypeCoin()}${Mask
                    .maskMoney(parseFloat(hasDiscountCoupon ? trip.tripDiscountCoupon?.oldPrice: trip?.rewardTrip?.value))}`;
            });

            viewController.elementProperty.addClass(".bonus-trip", "active");
        }
        

        if (trip.tripCallTypeId === TRIP_CALL_RIDE) {
            const totalRiders = trip.ride.riders.length;
            element.innerHTML = `${CityConfig.getTypeCoin()}${Mask.maskMoney(parseFloat(trip.price) * totalRiders)}`;
            return;
        }

        const configCity = Session.get("cityConfig");
        const paymentFormTypeId = trip.paymentFormTypeId;
        
        if (paymentFormTypeId == 2 || paymentFormTypeId == 4 || paymentFormTypeId == 7) {
            element.innerHTML = `${CityConfig.getTypeCoin()}${Mask.maskMoney(parseFloat(trip.price))}`;
        } else {
            if (configCity.enabledDriverFields.accept.price == true) {
                element.innerHTML = `${CityConfig.getTypeCoin()}${Mask.maskMoney(parseFloat(trip.price))}`;
            } else {
                element.innerHTML = `${CityConfig.getTypeCoin()} --`;
            }
        }        
        
    });
}

function updateWaypoints() {
    const elementProperty = new ElementProperty();

    const trip = Session.get("tripDriver");

    elementProperty.getElement(".list-place-trip", element => {

        if (trip.status !== STATUS_IN_PROGRESS) {
            element.innerHTML = `<li class="active">${trip.origin.address}</li>`;
        } else {
            element.innerHTML = `<li class="success">${trip.origin.address}</li>`;
        }

        let inProgress = false;

        const waypoints = trip.tripWayPoints.filter(waypoint => {
            if (trip.status !== STATUS_IN_PROGRESS) {
                element.innerHTML += `<li>${waypoint.address}</li>`;
            } else {
                if (waypoint.confirmed === "0" && !inProgress) {
                    inProgress = true;
                    element.innerHTML += `<li class="active">${waypoint.address}</li>`;
                } else {
                    if (waypoint.confirmed === "1") {
                        element.innerHTML += `<li class="success">${waypoint.address}</li>`;
                    } else {
                        element.innerHTML += `<li>${waypoint.address}</li>`;
                    }
                }

                return waypoint.confirmed === "0";
            }
        });

        if (waypoints.length === 0 && trip.status === STATUS_IN_PROGRESS) {
            element.innerHTML += `<li class="active">${trip.destination.name}</li>`;
        } else {
            element.innerHTML += `<li>${trip.destination.name}</li>`;
        }
    });
}

function openInfoTripInWait(trip) {
    clearTimeout(managerTripInWait.timeout);

    const elementProperty = new ElementProperty();
    elementProperty.getElement(".modal-evaluation", element => {
        if (element.classList.contains("active")) {
            managerTripInWait.timeout = setTimeout(() => openInfoTripInWait(trip), 2000);
            return;
        }
        Session.delete("tripInWait");

        InfoClientTrip.showDataClientTripWait(trip);
    })
}


function updateDataTrip(tripReference) {

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

    if (cityConfig.status_realtime_database) {
        updateTripInProgressFirestore(tripReference);
    } else {
        updateTripInProgress(tripReference);
    }
}

function updateTripInProgressFirestore(tripReference) {

    clearTimeout(managerGetTrip.timeout);

    tripRepository.close();

    tripRepository.getTrip(tripReference, (data, type) => {
        if (type === tripRepository.typeModified || type === tripRepository.typeAdded) {

            if (data.status === STATUS_SEARCHING ||
                data.status === STATUS_SEARCHING_FAVORITE ||
                data.status === STATUS_SCHEDULED ||
                data.status === STATUS_PRE_ACCEPTED)
                return;

            if(!Array.isArray(Session.get("tripDriver"))){
                if(Session.get("tripDriver").oldDestinationId !== data.oldDestinationId){
                    SwalCustom.messageDialog("Cliente alterou o local do seu destino, siga para o novo trajeto", "Alteração de rota na viagem",()=>{
                        showDataTrip();
                    })
                }
            }

            Session.set("tripDriver", data);

            //showBonusTripFinish(data);
            showDataCustomerTrip(data);
            showPlaceTrip(data);

            if (data.status === STATUS_BOARDING_TRIP)
                initCustomerWaitTime();

            if (data.status === STATUS_IN_PROGRESS) {
                customerWaitTime.stop();
                setMonitoring(true);
            }

            checkPreAvailable();
        }

        if (type === tripRepository.typeRemoved) {
            tripRepository.close();
            TripController.actionGet(tripReference).then(({status, data}) => {

                if (data.status === STATUS_FINISH_TRIP) {
                    const trip = Session.get("tripDriver");
                    if (Array.isArray(trip))
                        return;

                    modalFinishTrip(trip);
                    return;
                }
                if (data.status === STATUS_CANCELED) {
                    if (data.canceledBy !== "driver") {
                        openAppForeground();

                        viewController.elementProperty.getElement('#songClientCancelTrip', element => {
                            element.play();
                        });

                        Route.goHomeBack();

                        SwalCustom.messageDialog(data.canceledBy !== "admin" ?
                                getTextTranslate("trip", "message-cancel-trip-by-customer") :
                                getTextTranslate("trip", "message-cancel-trip-by-admin")
                            , getTextTranslate("trip", "title-cancel-trip"), () => {
                            }, "error");
                    }
                    clearDataTrip();
                }
            });
        }
    });
}

function updateTripInProgress(tripReference) {

    tripRepository.close();

    if (managerGetTrip.requestTrip)
        return;

    managerGetTrip.requestTrip = true;

    clearTimeout(managerGetTrip.timeout);

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

    if (cityConfig.status_realtime_database)
        return;

    TripController.actionGet(tripReference).then(({status, data}) => {
        if (data.status === STATUS_BOARDING_TRIP)
            initCustomerWaitTime();

        if (data.status === STATUS_FINISH_TRIP) {
            return;
        }

        if (data.status === STATUS_IN_PROGRESS) {
            customerWaitTime.stop();
            setMonitoring(true);
        }

        if (data.status === STATUS_CANCELED) {
            if (data.canceledBy !== "driver") {
                openAppForeground();

                viewController.elementProperty.getElement('#songClientCancelTrip', element => {
                    element.play();
                });

                Route.goHomeBack();

                SwalCustom.messageDialog(data.canceledBy !== "admin" ?
                    getTextTranslate("trip", "message-cancel-trip-by-customer") :
                    getTextTranslate("trip", "message-cancel-trip-by-admin")
                    , getTextTranslate("trip", "title-cancel-trip"), () => {
                }, "error");
            }
            clearDataTrip();
            return;
        }

        //verifyCancelTripInWait(data);

        checkPreAvailable();

        const time = 5000;

        managerGetTrip.timeout = setTimeout(function () {
            updateTripInProgress(tripReference);
        }, time)
    }).catch((e) => {
        managerGetTrip.timeout = setTimeout(function () {
            updateTripInProgress(tripReference);
        }, 5000)
    }).finally(() => {
        managerGetTrip.requestTrip = false;
    });
}

function verifyTripPreAccepted(tripReference) {

    tripPreAcceptedRepository.getTripPreAccepted(tripReference, (data, type) => {

        if (type === tripPreAcceptedRepository.typeModified ||
            type === tripPreAcceptedRepository.typeAdded) {
            if (data.status !== STATUS_PRE_ACCEPTED &&
                data.status !== STATUS_SEARCHING &&
                data.status !== STATUS_SEARCHING_FAVORITE) {
                tripPreAcceptedRepository.close();
            }
        }

        if (type === tripPreAcceptedRepository.typeRemoved) {
            tripPreAcceptedRepository.close();

            TripController.actionGet(tripReference).then(({status, data}) => {

                if (data.status === "canceled") {

                    if (data.canceledBy !== "driver") {
                        SwalCustom.messageDialog("", getTextTranslate("trip", "title-cancel-trip-await"), undefined, "info");
                        openAppForeground();
                    }


                    new ElementProperty().removeClass(".option-trip-in-wait", "active");
                    managerPreAvailable.isSearch = false;

                    const user = Session.get("user");

                    user.preAvailableTrip = null;
                    user.preAvailable = "0";

                    Session.set("user", user);
                }
            });
        }

    })
}

function clearDataTrip() {

    setMonitoring(false);

    customerWaitTime.stop();

    Session.delete("tripCallKnob");
    Session.delete("valueCancellationFeeByCustomer");

    const userLogged = Session.get("user");

    const elementProperty = new ElementProperty();

    elementProperty.getElement("#btnChangeStatusTrip", element => {
        element.classList.remove("signal")
    });

    elementProperty.removeClass(".text-alert-driver", "active");

    userLogged.status = "disponivel";

    Session.set("user", userLogged);


    showScheduledBar();

    Session.delete("tripDriver");

    if (document.querySelector(".v2") != null)
        elementProperty.visibleElements([".container-option-detail-status-driver"], true)

    elementProperty.visibleElements(['.toggle-status-driver'], true);

    elementProperty.visibleElements(['.data-trip', '.status-running'], false);

    elementProperty.getElement(".container-menu-options", element => {
        element.classList.remove("active");
    });

    elementProperty.getElement(".menu-close", element => {
        element.classList.remove("open");
    });

    elementProperty.getElement(".footer-race", element => {
        element.classList.remove("active");
        element.classList.remove("min-v2");
        element.classList.remove("in-trip-active");
    });


    elementProperty.getElement(".container-qrcode-driver", element => {
        element.classList.remove("active");
        element.style = "";
    });

    elementProperty.getElement(".footer-arrow", element => {
        element.style.display = "block";
    });
    elementProperty.getElement(".status-running", element => {
        element.style.display = "none";
    });
    elementProperty.getElement(".map", element => {
        element.classList.remove("active");
    });

    viewController.elementProperty.removeClass(".map", "disabled-map-corporate", "lite-map-corporate");
    viewController.elementProperty.removeClass(".container-taximetro", "active", "start-taximetro");
    viewController.elementProperty.removeClass(".btn-taximetro", "active");

    elementProperty.removeClass(".footer-race", "minimize");

    managerPreAvailable.isSearch = false;

    chatNotification.closeChat();

    if (mapService.map != null)
        mapService.removeRoute();

    searchTripProgress.stopProgress();

    updateTripDriver();
}

function clearNotification() {
    const elementProperty = new ElementProperty();
    elementProperty.getElement(".amount-msg", element => {
        element.innerHTML = '';
    });
}

function changeStatusNameTrip(name, status, zIndex = 200) {
    const elementProperty = new ElementProperty();

    elementProperty.getElement("#btnChangeStatusTrip", element => {
        element.innerHTML = name;
        element.classList.remove("active");
    });

    elementProperty.getElement("#statusDriverTrip", element => {
        element.innerHTML = status;
    });

    elementProperty.getElement(".status-running", element => {
        element.style.zIndex = zIndex;
    });
}

function backInitZoom() {
    const elementProperty = new ElementProperty();

    elementProperty.getElement(".map", map => {
        elementProperty.getElement(".footer-race", footer => {
            if (!map.classList.contains("active"))
                return;

            footer.classList.remove("zoom");
            map.classList.remove("active");
            mapService.zoomInDriver();
        });
    });
}

function sendTripOffline() {
    PreloadRequest.progress(getTextTranslate("trip", "progress-update-info-offline"));

    new ElementProperty().getElement(".footer-race", element => {
        element.style.display = "none";
    });

    const driverController = new DriverController();
    const tripFinish = Session.get("tripFinish");

    if (tripFinish.length !== 0) {

        function requestFinish() {
            driverController.finishTripOffline(tripFinish.tripReference,
                {
                    date: tripFinish.dateFinish,
                    location: {
                        latitude: tripFinish.position.lat,
                        longitude: tripFinish.position.lng
                    }
                })
                .then(() => {
                    Session.delete("tripFinish");
                    updateTripDriver();
                })
                .catch(() => {
                    setTimeout(sendTripOffline, 3000);
                });
        }

        if (tripFinish.tripCallTypeId === TRIP_CALL_TAXIMETER) {
            const infoUpdateTaximeter = Session.get("infoUpdateTaximeter");
            if (!Array.isArray(infoUpdateTaximeter)) {
                TaximeterTripController.update(infoUpdateTaximeter)
                    .then(() => {
                        Session.delete("infoUpdateTaximeter");
                        requestFinish();
                    })
                    .catch(() => {
                        setTimeout(sendTripOffline, 3000);
                    });
                return;
            }
        }

        requestFinish();

        return;
    }
    updateTripDriver();
}

function initCustomerWaitTime() {
    if (customerWaitTime.isActive)
        return;

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

    if (Array.isArray(Session.get("customerWaitTime")))
        Session.set("customerWaitTime", new Date());

    customerWaitTime.setTimeDiff(parseInt(cityConfig.waitingTimeCustomerCancellationFee));

    customerWaitTime.start();
}
