package com.outofbounds.empire.Movies.Controllers;

import com.outofbounds.empire.MovieDBApi.Models.MovieDBApi;
import com.outofbounds.empire.Movies.Models.Movie;
import com.outofbounds.empire.Movies.Repositories.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
class MovieController {

    @Autowired
    public MovieRepository movieRepository;

    /**
     * Basic route for movies
     * @return Movie
     */
    @CrossOrigin(origins = "http://localhost:8000")
    @RequestMapping(method = RequestMethod.GET, value = "/movies")
    public @ResponseBody
    List<Movie> movies() throws Exception {
        return movieRepository.findAll();
    }

    //@CrossOrigin(origins = "http://localhost:8000")
    @RequestMapping(method = RequestMethod.DELETE, value = "/movies/{id}")
    public @ResponseBody
    boolean deleteMovie(
            @PathVariable int id
    )
    {
        Movie movie = movieRepository.findById(id);

        if (movie == null){
            return false;
        }
        movieRepository.delete(movie);
        return true;
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/movies/{id}")
    public @ResponseBody
    Movie updateMovie(
            @PathVariable int id

    ){
        Movie movie = movieRepository.findById(id);
        return movie;
    }


    @CrossOrigin(origins = "http://localhost:8000")
    @RequestMapping(method = RequestMethod.GET, value = "/movies/{id}")
    public @ResponseBody
    Movie movie(@PathVariable int id) {
        return movieRepository.findById(id);
    }

    @CrossOrigin(origins = "http://localhost:8000")
    @RequestMapping(method = RequestMethod.GET, value = "/movies/generate/random")
    public @ResponseBody
    List<Movie> createMovie() throws Exception {
        MovieDBApi movieDB = new MovieDBApi();
        ArrayList<Movie> movies = movieDB.MovietoDB();
        for (int i = 0; i < movies.size() ; i++) {
            Movie movie = new Movie(
                    movies.get(i).getTitle(),
                    movies.get(i).getDescription(),
                    18,
                    movies.get(i).getCategory(),
                    100,
                    movies.get(i).getImageUrl()
            );
            movieRepository.save(movie);
        }
        return movieRepository.findAll();
    }
}
@@ -389,16 +389,109 @@
 .List;%0A%0A
+import static com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS.required;%0A%0A
 @RestCon
@@ -1451,66 +1451,803 @@
 t id
-%0A%0A    )%7B%0A        Movie movie = movieRepository.findById(id
+,%0A            @RequestParam (required = false) String title,%0A            @RequestParam (required = false) String description,%0A            @RequestParam (required = false) Integer ageLimit,%0A            @RequestParam (required = false) String category,%0A            @RequestParam (required = false) Integer price,%0A            @RequestParam (required = false) String url%0A    )%7B%0A        Movie movie = movieRepository.findById(id);%0A        try %7B%0A            movie.setTitle(title);%0A            movie.setDescription(description);%0A            movie.setAgeLimit(ageLimit);%0A            movie.setCategory(category);%0A            movie.setPrice(price);%0A            movie.setImageUrl(url);%0A        %7D catch (NullPointerException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        movieRepository.save(movie
 );%0A 
@@ -2274,17 +2274,16 @@
 %0A    %7D%0A%0A
-%0A
     @Cro

update fix