class ActorsController < ApplicationController
  # GET /actors
  # GET /actors.xml
  def index
    @actors = Actor.all
    # ENSURE NOT TOO MUCH SPACE IS TAKEN BY THE BIO ON THE LIST SCREEN
    @actors.each do |actor| 
      if actor.bio
        actor.bio = short_text(actor.bio) 
      end  
    end
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @actors }
    end
  end

  # GET /actors/1
  # GET /actors/1.xml
  def show
    @actor = Actor.find(params[:id])
    @photo = Photo.find_by_actor_id(params[:id], :order => 'random()')
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @actor }
    end
  end
  
  # GET/ACTORS/1/MOVIES_HAVE - A LIST OF WHICH MOVIES HAVE
  # A GIVEN ACTOR.
  def movies_have
    @actor = Actor.find(params[:id]) 
  end

  # POST /ACTOR/1/MOVIE_ADD?MOVIE_ID-X
   def movie_add
     @actor = Actor.find(params[:id])
     movie_ids = params[:movies]
     # @movie = Movie.find(params[:movie])
     unless movie_ids.blank?
       movie_ids.each do |movie_id|
         movie=Movie.find(movie_id)
         unless @actor.involved_in?(movie)
           logger.info "adding actor to movie #{movie.id}"
           @actor.movies << movie
           flash[:notice] = 'Movie was successfully added' 
         else
           flash[:error] = 'Actor is already in this movie' 
         end
       end
     end      
     # redirect_to :action => :movies, :id => @actor
     
     @movies = Movie.find(movie_ids)
     respond_to do |fmt|
       fmt.html {redirect_to :action => :movies, :id => @actor}
       fmt.js
     end   
     
   end      

   # POST /actors/1/movie_remove?movies[]=
   def movie_remove

     # CONVERT IDS FROM ROUTING TO OBJECT
     @actor = Actor.find(params[:id])

     # GET A LIST OF MOVIES TO REMOVE FROM QUERY STRING
     movie_ids = params[:movies]

     unless movie_ids.blank?
       movie_ids.each do |movie_id|
         movie = Movie.find(movie_id)
         if @actor.involved_in?(movie)
           logger.info "Removing actor from movie #{movie.id}"
           @actor.movies.delete(movie)
           flash[:notice] = "Movie was removed from actor/ess' repertoire"
         end
       end
     end
     # redirect_to :action => :movies, :id => @actor
     @movies = Movie.find(movie_ids)
     respond_to do |fmt|
       fmt.html {redirect_to :action => :movies, :id => @actor}
       fmt.js
     end
     
   end

  # GET/ACTORS/1/MOVIES
  def movies
    @actor = Actor.find(params[:id])
    @movies = @actor.movies
  end

  # GET /actors/new
  # GET /actors/new.xml
  def new
    @actor = Actor.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @actor }
    end
  end

  # GET /actors/1/edit
  def edit
    @actor = Actor.find(params[:id])
  end

  # POST /actors
  # POST /actors.xml
  def create
    @actor = Actor.new(params[:actor])

    respond_to do |format|
      if @actor.save
        flash[:notice] = 'Actor was successfully created.'
        format.html { redirect_to(@actor) }
        format.xml  { render :xml => @actor, :status => :created, :location => @actor }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @actor.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /actors/1
  # PUT /actors/1.xml
  def update
    @actor = Actor.find(params[:id])

    respond_to do |format|
      if @actor.update_attributes(params[:actor])
        flash[:notice] = 'Actor was successfully updated!'
        format.html { redirect_to(@actor) }
        format.xml  { head :ok }
        format.js
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @actor.errors, :status => :unprocessable_entity }
        format.js
      end
    end
  end

  # DELETE /actors/1
  # DELETE /actors/1.xml
  def destroy
    @actor = Actor.find(params[:id])
    @actor.destroy
    respond_to do |format|
      format.html { redirect_to(actors_url) }
      format.xml  { head :ok }
    end
  end
  
  
end
