class ApiSearchController < ApiController

  # have to be signed in currently to see this screen
  before_filter :api_signed_in_user, :except => :jam_tracks

  respond_to :json

  def index
    if 1 == params[Search::PARAM_MUSICIAN].to_i || 1 == params[Search::PARAM_BAND].to_i
      query = params.clone
      query[:remote_ip] = request.remote_ip
      if 1 == query[Search::PARAM_MUSICIAN].to_i
        @search = Search.musician_filter(query, current_user)
      else
        @search = Search.band_filter(query, current_user)
      end
      respond_with @search, responder: ApiResponder, :status => 200
    elsif 1 == params[Search::PARAM_SESSION_INVITE].to_i
      @search = Search.session_invite_search(params[:query], current_user)
    else
      @search = Search.text_search(params, current_user)
    end
  end

  def musicians
    if request.get?
      if params[:results]
        @search = MusicianSearch.user_search_filter(current_user).search_results_page
        respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index'
      else
        render :json => MusicianSearch.search_filter_json(current_user), :status => 200
      end

    elsif request.post?
      sobj = MusicianSearch.user_search_filter(current_user)
      filter = params[:filter]
      if filter == 'reset'
        @search = sobj.reset_search_results
      else
        json = JSON.parse(filter, :create_additions => false)
        @search = sobj.search_results_page(json, [params[:page].to_i, 1].max)
      end
      respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index'
    end
  end

  def bands
    if request.get?
      if params[:results]
        @search = BandSearch.user_search_filter(current_user).search_results_page(params[:subtype])
        respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index'
      else
        render :json => BandSearch.search_filter_json(current_user, params[:subtype]), :status => 200
      end

    elsif request.post?
      sobj = BandSearch.user_search_filter(current_user)
      filter = params[:filter]
      if filter == 'reset'
        @search = sobj.reset_search_results(params[:subtype])
      else
        json = JSON.parse(filter, :create_additions => false)
        @search = sobj.search_results_page(params[:subtype], json, [params[:page].to_i, 1].max)
      end
      respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index'
    end
  end

  def jam_tracks
    if request.get?
      if params[:iso639]
        render(json: JamTrackSearch.all_languages.to_json, status: 200)
        
      elsif params[:indexed]
        fname = "#{Rails.root}/tmp/jtx_indices.json"
        
        unless File.exists?(fname) && File.size(fname) > 0
          SampleApp::Application.load_tasks
          Rake::Task["mobile:jam_tracks_json"].invoke
        end
        json = JSON.parse(File.read(fname))
        render(json: json, status: 200)
        
      else
        render(json: {})
      end
      
    elsif request.post?
      jts = JamTrackSearch.new
      filter = request.params[:api_search]
      result = jts.search_results_page(filter)
      render(json: result.to_json, status: 200)
    end
  end

end
