class SongsController < ApplicationController
  layout "application"
  before_filter :get_user
  before_filter :login_required, :except => [:show, :index]

  # GET /songs
  # GET /songs.xml
  def index
    @songs = Song.find(:all, :order => :audio_file_name)
    @songs = @songs.paginate :per_page => 20, :page => params[:page]
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @songs }
    end
  end

  # GET /songs/1
  # GET /songs/1.xml
  def show
    @song = Song.find(params[:id])
    session[:song_id] = @song.id
        
    @uploader = User.find(:first, :conditions => ["id = ?", @song.user_id])

    @comments = Comment.on_song(@song.id)

    @follow = Follow.find(:first, :conditions => ["user_id = ? and user_follow_id = ?", @curr_user.id, @song.user_id])
    
    @user_profile = UserProfile.find(:first, :conditions => ["user_id = ?", @song.user_id])

    #update listen count per page view. Should only be per listen though...
    if @song.listen_count.nil?
      @song.update_attribute("listen_count", 1) 
    else
      views = @song.listen_count += 1
      @song.update_attribute("listen_count", views)
    end

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @song }
    end
  end

  # GET /songs/new
  # GET /songs/new.xml
  def new
    @song = Song.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @song }
    end
  end

  # GET /songs/1/edit
  def edit
    @song = Song.find(params[:id])
  end

  def flasher(*arg)
    puts arg
  end

  # POST /songs
  # POST /songs.xml
  def create
    @song = Song.new(params[:song])
    @song.user_id = session[:user_id]
        
    respond_to do |format|
      if @song.save
      
        #create md5sum based on the file being uploaded
        digest = Digest::MD5.new
      	File.open("#{RAILS_ROOT}/public/audios/#{@song.id}", "r") do |f|
      	    digest.update f.read(8192) until f.eof
 	end
        
        #see if a file exists in the system with the same md5sum
        dup = Song.find(:first, :conditions => { :md5sum => digest.to_s })
	
	#a file already exists, delete the new one that was uploaded and inform the user
	if not dup.nil?
		@song.destroy
		flash[:alert] = 'This song already exists in the system.'
		format.html { render :action => "new" }
  	else
        	
        	@song.update_attribute("md5sum", digest.to_s)
		flash[:notice] = 'Song was successfully uploaded.'
		format.html { redirect_to(@song) }
		format.xml  { render :xml => @song, :status => :created, :location => @song }
        end
        
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @song.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /songs/1
  # PUT /songs/1.xml
  def update
    @song = Song.find(params[:id])

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

  # DELETE /songs/1
  # DELETE /songs/1.xml
  def destroy
    @song = Song.find(params[:id])
    @song.destroy

    respond_to do |format|
      format.html { redirect_to(songs_url) }
      format.xml  { head :ok }
    end
  end
  
  def playlist
  	respond_to do |format|
  		format.xml
  		end
  	end
  		

  def rate
  	@song = Song.find(params[:id])
    @song.rate(params[:stars], current_user, params[:dimension])
    id = "ajaxful-rating-#{!params[:dimension].blank? ? "#{params[:dimension]}-" : ''}song-#{@song.id}"
    render :update do |page|
    page.replace_html id, ratings_for(@song, :wrap => false, :dimension => params[:dimension])
    page.visual_effect :highlight, id
    end
  end
end
