class TripRepository {
    constructor() {
        this.reference = null;
        this.unsubscribe = null;
        this.collection = "trips";
        this.typeModified = "modified";
        this.typeRemoved = "removed";
        this.typeAdded = "added";

        this.typeKnobTrip = "2";

        this.connection = false;
    }

    close() {
        this.connection = false;

        if (this.unsubscribe)
            this.unsubscribe();
    }

    searchTrip(driverId, callback) {
        this.connection = true;
        this.dbFirestore = getDBFirestore();

        this.reference = this.dbFirestore.collection(this.collection)
            .where("callingDriver", "array-contains", driverId)
            .where("driver", "==", null).limit(1);

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            snapshot.docChanges().map(change => {
                callback(change.doc.data(),change.type)
            });

        });
    }

    getTrip(tripReference,callback){
        this.dbFirestore = getDBFirestore();
        this.reference = this.dbFirestore.collection(this.collection)
            .where("tripReference", "==", tripReference).limit(1);

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            snapshot.docChanges().map(change => {
                callback(change.doc.data(),change.type)
            });

        });
    }


    getTripPreAccepted(tripReference,callback){
        this.dbFirestore = getDBFirestore();
        this.reference = this.dbFirestore.collection(this.collection)
            .where("tripReference", "==", tripReference).limit(1);

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            snapshot.docChanges().map(change => {
                callback(change.doc.data(),change.type)
            });

        });
    }


    getKnobTrip(driverId, callback) {
        this.dbFirestore = getDBFirestore();
        this.close()
        this.reference = this.dbFirestore.collection(this.collection)
            .where("tripCallTypeId", "==", this.typeKnobTrip)
            .where("status", "==", STATUS_BOARDING_TRIP)
            .where("driverId", "==", driverId).limit(1);

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            snapshot.docChanges().map(change => {
                callback(change.doc.data(),change.type)
            });

        });
    }

    getTripScheduled(cityId,tripTypeId, callback) {
        this.dbFirestore = getDBFirestore();

        this.reference = this.dbFirestore.collection(this.collection)
            .where("cityId", "==", cityId)
            .where("driverId", "==", null)
            .where("tripTypeId", "==", tripTypeId)
            .where("status", "==", STATUS_SCHEDULED).orderBy("date","desc");

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            callback(snapshot.docChanges().map(change => {
                return {data: change.doc.data(), type: change.type};
            }));

        });
    }

    getTripOffer(cityId,tripTypeId, callback) {
        this.dbFirestore = getDBFirestore();

        this.reference = this.dbFirestore.collection(this.collection)
            .where("cityId", "==", cityId)
            .where("driverId", "==", null)
            .where("tripTypeId", "==", tripTypeId)
            .where("status", "==", STATUS_GET_BID_BY_DRIVERS).orderBy("date","desc");

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            callback(snapshot.docChanges().map(change => {
                return {data: change.doc.data(), type: change.type};
            }));

        });
    }

    getTripScheduledAccepted(driverId, callback) {
        this.dbFirestore = getDBFirestore();

        this.reference = this.dbFirestore.collection(this.collection)
            .where("driverId", "==", driverId)
            .where("status", "==", STATUS_SCHEDULED).orderBy("date","desc");

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            callback(snapshot.docChanges().map(change => {
                return {data: change.doc.data(), type: change.type};
            }));

        });
    }

    checkTripPreAcceptedByDriver(driverId, callback) {
        this.dbFirestore = getDBFirestore();

        this.reference = this.dbFirestore.collection(this.collection)
            .where("driverId", "==", driverId)
            .where("status", "==", STATUS_PRE_ACCEPTED).orderBy("date","desc");

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            callback(snapshot.docChanges().map(change => {
                return {data: change.doc.data(), type: change.type};
            }));

        });
    }



    checkTripAccepted(driverId, callback) {
        this.dbFirestore = getDBFirestore();

        this.reference = this.dbFirestore.collection(this.collection)
            .where("driverId", "==", driverId)
            .where("status", "==", STATUS_ACCEPTED).limit(1);

        this.unsubscribe = this.reference.onSnapshot(snapshot => {
            callback(snapshot.docChanges().map(change => {
                return {data: change.doc.data(), type: change.type};
            }));
        });
    }
}

const tripKnobRepository = new TripRepository();
const searchTripRepository = new TripRepository();

const tripRepository = new TripRepository();
const tripPreAcceptedRepository = new TripRepository();