class   Controller  {
	constructor(view,model){
		this.view = view
		this.model = model

		this.loading = false
		this.lastSearch = null
		this.error = null
		this.results = []

		this.view.setLoading(false)
		this.view.bindSearch(this.search.bind(this))
	}
	reset() {
		this.loading = false
		this.error = null
		this.results = []
	}

	async search(searchMovie) {
		try {
            this.reset();
            this.loading = true;
            this.view.setLoading(true);
            
            const movies = await this.model.getMovies(searchMovie);
            
            if (movies && movies.length > 0) {
                this.results = movies;
            } else {
                this.error = "No movies found.";
            }
        } catch (error) {
            console.error('Error occurred during search:', error);
            this.error = "An error occurred during search.";
        } finally {
            this.loading = false;
            this.view.setLoading(false);
            this.view.renderResults(this.results, this.error);
        }
	}
} 

export default Controller