class MoviesController < ApplicationController
  
   def movielist
      # WE GET HERE BY A "movies" LINK FROM THE LIST OF GENRES, SO WE HAVE
      # THE GENRE ID FOR WHICH THE MOVIES ARE REQUESTED. WE GET ALL THE 
      # MOVIES FOR THE GIVEN GENRE. 

      @gen  = params[:id]
      @geno = Genre.find(params[:id])
      @movies = Movie.find(:all, :conditions => "genre_id = #{@gen}" )
      list
    end
  
  # GET /movies
  # GET /movies.xml
  def index
    @movies = Movie.all
    list
  end

  def list 
    # ENSURE NOT TOO MUCH SPACE IS TAKEN BY THE SYNOPSIS ON THE LIST SCREEN
    @movies.each do |movie| 
      if movie.synopsis
        movie.synopsis = short_text(movie.synopsis) 
      end  
    end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @movies }
    end
  end

  # GET /movies/1
  # GET /movies/1.xml
  def show
    @movie = Movie.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @movie }
    end
  end
  
  # GET/MOVIES/1/STAFF - A LIST OF WHICH ACTORS ARE IN A GIVEN MOVIE
  def staff
    @movie = Movie.find(params[:id]) 
  end  
  
  # TAKES A MOVIE ID AND A SINGLE ACTOR ID AND
  # ADD THE MOVIE TO THE ACTOR (MAKE THE ACTOR ACT IN THAT MOVIE)
  # POST /MOVIE/1/ACTOR_ADD?ACTOR_ID-X
   def actor_add
     @movie = Movie.find(params[:id])
     @actor = Actor.find(params[:actor])
     unless @movie.has_in?(@actor)
       @movie.actors << @actor
       flash[:notice] = 'Movie staffed by an actor/actress succesfully' 
     else
       flash[:error] = 'Movie already has this actor/actress' 
     end
     redirect_to :action => :actors, :id => @movie
   end      
  
   # POST /movies/1/actor_remove?actors[]=
   def actor_remove
     #Convert ids from routing to object
     @movie = Movie.find(params[:id])
     #get list of actors to remove from query string
     actor_ids = params[:actors]
     unless actor_ids.blank?
       actor_ids.each do |actor_id|
         actor = Actor.find(actor_id)
         if @movie.has_in?(actor)
           logger.info "Detaching an actor/ess from a movie #{actor.id}"  
           @movie.actors.delete(actor)
           flash[:notice] = 'Actor was successfully removed from the movie'
         end
       end
     end
     redirect_to :action => :actors, :id => @movie
   end
  
  # GET/MOVIES/1/ACTORS - WHICH ACTORS A SPECIFIC MOVIE HAS
  def actors
    @movie = Movie.find(params[:id]) # LOAD THE GIVEN MOVIE AS AN OBJECT
    @actors = @movie.actors          # GET THE ACTORS INSIDE THE OBJECT
  end
  
  # GET /movies/new
  # GET /movies/new.xml
  def new
    @movie = Movie.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @movie }
    end
  end

  # GET /movies/1/edit
  def edit
    @movie = Movie.find(params[:id])
  end

  # POST /movies
  # POST /movies.xml
  def create
    @movie = Movie.new(params[:movie])
    respond_to do |format|
      if @movie.save
        flash[:notice] = 'Movie was successfully created.'
        format.html { redirect_to(@movie) }
        format.xml  { render :xml => @movie, :status => :created, :location => @movie }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @movie.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /movies/1
  # PUT /movies/1.xml
  def update
    @movie = Movie.find(params[:id])
    respond_to do |format|
      if @movie.update_attributes(params[:movie])
        flash[:notice] = 'Movie was successfully updated.'
        format.html { redirect_to(@movie) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @movie.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /movies/1
  # DELETE /movies/1.xml
  def destroy
    @movie = Movie.find(params[:id])
    @movie.destroy
    respond_to do |format|
      format.html { redirect_to(movies_url) }
      format.xml  { head :ok }
    end
  end
  
  def before_update()
    update_attribute :synopsis, synopsis.split("\r\n")
  end  
  
  
end
