class FilterManager {
    constructor(dataStore) {
        this.dataStore = dataStore;
        this.makeFilters = [];
        this.modelFilters = [];
        this.trimFilters = [];
        this.bodyTypeFilters = [];
        this.transmissionFilters = [];
        this.exteriorFilters = [];
        this.subscribers = [];
       
    }

    createMakeFilter(make) {
        const makeFilter = new MakeFilter(make, this.dataStore);
        this.makeFilters.push(makeFilter);
        this.notifySubscribers();
        return makeFilter;
    }

    createModelFilter(model) {
        const modelFilter = new ModelFilter(model, this.dataStore);
        this.modelFilters.push(modelFilter);
        this.notifySubscribers();
        return modelFilter;
    }

    createTrimFilter(trim) {
        const trimFilter = new TrimFilter(trim, this.dataStore);
        this.trimFilters.push(trimFilter);
        this.notifySubscribers();
        return trimFilter;
    }

    createTransmissionFilter(transmission) {
        const transmissionFilter = new TransmissionFilter(transmission, this.dataStore);
        this.transmissionFilters.push(transmissionFilter);
        this.notifySubscribers();
        return transmissionFilter;
    }

    
    createExteriorFilter(exterior) {
        const exteriorFilter = new ExteriorFilter(exterior, this.dataStore);
        this.exteriorFilters.push(exteriorFilter);
        this.notifySubscribers();
        return exteriorFilter;
    }

    createBodyTypeFilter(bodyType) {
        const bodyTypeFilter = new BodyTypeFilter(bodyType, this.dataStore);
        this.bodyTypeFilters.push(bodyTypeFilter);
        this.notifySubscribers();
        return bodyTypeFilter;
    }

    removeFilter(filterType, filterValue) {
        this.dataStore.removeFilter(filterType, filterValue);

        if (filterType === 'make') {
            const makeFilter = this.makeFilters.find(filter => filter.value === filterValue);
            if (makeFilter) {
                makeFilter.uncheckCheckbox();
            }
            
        }  
        
        if (filterType === 'model') {
            const modelFilter = this.modelFilters.find(filter => filter.value === filterValue);
            if (modelFilter) {
                modelFilter.uncheckCheckbox();
            }
            
        }

        if (filterType === 'trim') {
            // because it came from response as an array 
            this.trimFilters.filter(filter => filter.value === filterValue).forEach(trimFilter=>{
                if (trimFilter) {
                    trimFilter.uncheckCheckbox();
                }
            });
        }

        if (filterType === 'body_type') {
            const bodyType = this.bodyTypeFilters.find(filter => filter.value === filterValue);
            if (bodyType) {
                bodyType.uncheckCheckbox();
            }
        }

        if (filterType === 'transmission_type') {
            const transmission = this.transmissionFilters.find(filter => filter.value === filterValue);
            if (transmission) {
                transmission.uncheckCheckbox();
            }
        }

        if (filterType === 'exterior_color') {
            const exterior = this.exteriorFilters.find(filter => filter.value === filterValue);
            if (exterior) {
                exterior.uncheckCheckbox();
            }
        }
        this.notifySubscribers();
    }

    resetList(type) {

        if (type === 'make') {
           this.makeFilters = [];
        }  
        
        if (type === 'model') {
            this.modelFilters = [];
            
        }

        if (type === 'trim') {
            this.trimFilters = [];
        }

        if (type === 'body_type') {
           this.bodyTypeFilters = [];
        }

        if (type === 'transmission_type') {
            this.transmissionFilters = [];
        }

        if (type === 'exterior_color') {
            this.exteriorFilters = [];
        }
        this.notifySubscribers();
    }

    notifySubscribers() {
        this.subscribers.forEach(subscriber => subscriber({
            makeFilters: this.makeFilters, 
            modelFilters: this.modelFilters,
            trimFilters: this.trimFilters
        }));
    }

    subscribe(subscriber) {
        this.subscribers.push(subscriber);
    }

    unsubscribe(subscriber) {
        this.subscribers = this.subscribers.filter(sub => sub !== subscriber);
    }
}
