const managerScheduleTripAccepted = {
    haveObservedScheduleTrip: false,
};

function initListFreightAccepted() {
    if(managerScheduleTripAccepted.haveObservedScheduleTrip)
        return;

    managerScheduleTripAccepted.haveObservedScheduleTrip = true;

    viewController.elementProperty.addEventInElement("#openScheduleTripAccepted", "onclick", () => {
        Route.startClassesInComponent([
            {component: "#containerListScheduleTripAccepted", class: "active"},
        ], () => {
            viewController.elementProperty.removeClass("#containerListScheduleTripAccepted", "active");
        });
    });

    const tripRepository = new TripRepository();

    tripRepository.getTripScheduledAccepted(Session.getValueInSession("user", "idDriver"), result => {
        result.map(({data: trip, type}) => {
            if (type === tripRepository.typeAdded)
                addItemFreightAccepted(trip);

            if (type === tripRepository.typeRemoved)
                removeItemFreightAccepted(trip);

        }).join("");

        showScheduledBar();

        document.querySelector("#countScheduleTripAccepted").innerHTML =
            `${document.querySelectorAll(".schedule-trip-accepted").length}`;

        initEventDetailFreight();
    });

    verifyFreightAccepted();
}


function verifyFreightAccepted() {
    const tripRepository = new TripRepository();

    tripRepository.checkTripAccepted(Session.getValueInSession("user", "idDriver"), (result) => {
        if (result.length > 0) {
            const trip = result[0];

            if (trip.type !== tripRepository.typeAdded)
                return;

            Route.goHomeBack();

            //if (Session.get("acceptedFreight") !== freight.data.tripReference)
            //    return getInfoFreightPreAccepted(freight.data.tripReference);

            updateInfoHomeDriver(trip.data);

            navigator.notification.beep();
        }
    })
}

function updateInfoHomeDriver(trip){
    if(trip.schedule != null) {
        viewController.elementProperty.addClass(".container-notification-scheduled-trip", "active");

        viewController.elementProperty.getElement("#nameDriverScheduledTrip", element => {
            element.innerHTML = Session.getValueInSession("user", "nickname");
        });

        viewController.elementProperty.addEventInElement("#btnCloseNotificationScheduledTrip", "onclick", () => {
            viewController.elementProperty.removeClass(".container-notification-scheduled-trip", "active");
        });

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

    updateTripDriver();
}

function removeItemFreightAccepted(trip) {
    const element = document.querySelector(`.item-schedule-trip[tripReference='${trip.tripReference}']`);

    if (element != null)
        element.remove();
}

function addItemFreightAccepted(trip) {
    if (trip.disableOnPromotion == true){
        totalValue = trip.price
    } else {
        if (trip.driverCommissionType === "%"){
            pricePercentage = (trip.price) * (trip.driverCommissionValue/100);
            totalValue = (trip.price) - pricePercentage;
        } else {
            totalValue = (trip.price) - (trip.driverCommissionValue);
        }
    }
    viewController.elementProperty.getElement("#listFreightAccepted", element => {
        element.innerHTML += `<li class="item-schedule-trip schedule-trip-accepted" tripReference="${trip.tripReference}">
                            ${verifyExistSchedule(trip)}
                            <p>
                                <span>Origem: </span>
                                <span>${trip.origin.address}</span>
                            </p>
                            <p>
                                <span>Destino: </span>
                                <span>${trip.destination.name}</span>
                            </p>
                            <p>
                                <span>Valor: </span>
                                <span class="value-freight-item">${CityConfig.getTypeCoin()}${Mask.maskMoney(parseFloat(totalValue))}</span>
                            </p>
                            <div>
                                 <div>
                                    <span>Tipo: </span>
                                    <span>${trip.tripType.toUpperCase()}</span>
                                </div>
                               <div class="content-option-trip">
                                 ${checkTypeEventByTrip(trip)}
                                 <button type="button" class="btn-schedule-trip-cancel" tripReference="${trip.tripReference}">Cancelar</button>
                                </div>
                            </div>
                        </li>`;
    })
}
