class MediasController < ApplicationController

  layout "medias"

  def index
    current_params = {}
    @pagin = false
    @no_search = false
    @feature_value = nil
    @nopicto = no_picto
    if params[:saved_search_id]
      @pagin = true
      @no_search = true
      current_params = SavedSearch.find(params[:saved_search_id]).criteria
      cearch = {}
      sort = "by_#{params[:sort]}"
      current_params['pp_category_id'] = (current_params['pp_category_id'].blank? || current_params['pp_category_id']==['all'] )? [] : current_params['pp_category_id'].map(&:to_i)
      cearch[:per_page] = params[:per_page].nil? ? NB_PER_PAGE : params[:per_page].to_i
      cearch[:page] = params[:page].to_i if params[:page] && params[:page].to_i > 0
      cearch[:conditions] = Image.params_to_conditions(current_params)
      cearch[:with] = Image.params_to_with(current_params)
      # cearch[:retry_stale] = true
      cearch[:excerpts] = {limit: 1024}
      keywords = current_params[:key_words] ? Pixways::SearchesHelper.filter_keywords(current_params[:key_words]) : ''
      @medias = Image.instance_eval(sort).search(keywords, cearch)
      @medias.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane
    elsif params[:search]
      if params[:sort] == "random"
        @medias =  Image.where(id: params[:ids])
      else
        @pagin = true
        @no_search = true
        current_params = Rails.cache.read(params[:search])
        cearch = {}
        sort = 'reception_date'
        sort = current_params["sort"] if current_params["sort"]
        sort = "by_#{sort}"
        current_params['pp_category_id'] = (current_params['pp_category_id'].blank? || current_params['pp_category_id']==['all'] )? [] : current_params['pp_category_id'].map(&:to_i)
        cearch[:per_page] = params[:per_page].nil? ? NB_PER_PAGE : params[:per_page].to_i
        cearch[:page] = params[:page].to_i if params[:page] && params[:page].to_i > 0
        cearch[:conditions] = Image.params_to_conditions(current_params)
        cearch[:with] = Image.params_to_with(current_params)
        # cearch[:retry_stale] = true
        cearch[:excerpts] = {limit: 1024}
        if current_params['paris2024'] == '1'
          paris2024_search = t('paris2024_search').join('" | "')
          paris2024_search = '"' + paris2024_search + '"'
          if current_params['translation'] == '1'
            translated_paris2024 = session[:locale]=='fr' ? t('paris2024_search',locale: :en).join('" | "') : t('paris2024_search',locale: :fr).join('" | "')
            translated_paris2024 = '"' + translated_paris2024 + '"'
          end
        end
        if current_params['translation'] == '1' && current_params['paris2024'] == '1'
          keywords_unfiltered = current_params["key_words"].blank? ? "(#{paris2024_search}) | (#{translated_paris2024})" : "(#{current_params[:key_words]}) and ((#{paris2024_search}) | (#{translated_paris2024}))"
        elsif current_params['paris2024'] == '1'
          keywords_unfiltered = current_params["key_words"].blank? ? paris2024_search : "(#{current_params[:key_words]}) and (#{paris2024_search})"
        else
          keywords_unfiltered = current_params["key_words"].blank? ? '' : current_params[:key_words]
        end
        # keywords_unfiltered = current_params["key_words"].blank? ? ( paris2024_search.blank? ? '' : paris2024_search ) : ( paris2024_search.blank? ? current_params[:key_words] : "(#{current_params[:key_words]}) and (#{paris2024_search})" )
        keywords = Pixways::SearchesHelper.filter_keywords(keywords_unfiltered)
        @medias = Image.instance_eval(sort).search(keywords, cearch)
        @medias.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane
      end
    elsif params[:ids]
      @medias =  Image.where(id: params[:ids])
    elsif params[:reportage]
      @feature_ref = params[:reportage]
      @feature_value = params[:reportage]
      @feature_prov_sk = Provider.find(params[:provider_id]).string_key
      @feature_prov_id = params[:provider_id]
      rep = Reportage.find_by(no_reportage: @feature_value, string_key: @feature_prov_sk)
      @feature_number = rep.nb_photos
      @medias = Image.joins(:reportage_photos).where(reportage_photos: {reportage_id: rep.id}, images: {content_error: 0})
    elsif params[:offre]
      @offre = params[:offre].to_i
      rep = Reportage.find(@offre)
      @feature_ref = rep.no_reportage
      @feature_value = rep.rep_titre
      @feature_number = rep.nb_photos
      @feature_text = rep.rep_texte
      @medias = Image.joins(:reportage_photos).where(reportage_photos: {reportage_id: rep.id}, images: {content_error: 0}).order('reportage_photos.rang')
    elsif params[:light_box]
      @medias = LightBoxImage.where(light_box_id: params[:light_box]).order('position DESC').collect{ |i| Image.find(i.image_id.to_i) if i.image_id.to_i > 0 }
    else
      @pagin = true
      cearch = {}
      with = {}
      with[:provider_id] = session[:provs]
      with[:content_error] = 0
      sort = "by_reception_date"
      current_params['pp_category_id'] = (current_params['pp_category_id'].blank? || current_params['pp_category_id']==['all'] )? [] : current_params['pp_category_id'].map(&:to_i)
      cearch[:per_page] = params[:per_page].nil? ? NB_PER_PAGE : params[:per_page].to_i
      cearch[:page] = params[:page].to_i if params[:page] && params[:page].to_i > 0
      cearch[:conditions] = {}
      cearch[:with] = with
      @medias = Image.instance_eval(sort).search('', cearch)
    end

    # @browser_css = request.user_agent =~ /Firefox/ ? '_firefox' : ''
    #
    # browser=request.user_agent
    # @browser_css=''
    # if browser =~ /Firefox/
    #   @browser_css='_firefox'
    # elsif browser =~ /Chrome/ || browser =~ /Safari/ || browser =~ /Edge/
    #     @browser_css=''
    # elsif browser =~ /Opera/
    # elsif browser =~ /MSIE/
    # else @browser_css=''
    # end
  end

  private

end
