class Panel::Archive::FrontEnd::ArchiveFilesController < OrbitWidgetController
  include AdminHelper

  def initialize
    super
    @app_title = 'archive_files'
  end

  def get_search_result
      @search = ArchiveFileCategory.solr_search do 
        fulltext params[:search_query]
        with(:frontend_search,true)
      end

      search_result =  @search.results.collect{|result| result.id}
      params[:category_id] = @search.results.collect{|result| result.id}

      @archive_file_categorys = ArchiveFileCategory.any_in(_id:search_result).page( params[:page_main]).per(@page_num)
  end

  def index
    @item = Page.find(params[:page_id])
    @title = @item.title
    @frontend_style = !@item.frontend_style.blank? ? @item.frontend_style : "1"
    if !@item.frontend_data_count.blank?
      @page_num = @item.frontend_data_count
    else
      @page_num = 10
    end

    date_now = Time.now
    # @archive_file_categorys = ArchiveFileCategory.all
    # @archive_files = ArchiveFile.where( :is_hidden => false ).desc(:is_top).page(params[:page]).per(@page_num)

    if !params[:category_id].blank?
      @archive_file_categorys = @module_app.categories.enabled.any_in(:_id => params[:category_id])
      @archive_file_category_ids = @module_app.categories.enabled.any_in(:_id => params[:category_id]).collect{|a|a.id}
    else
      @archive_file_categorys = @module_app.categories.enabled
      @archive_file_category_ids = @module_app.categories.enabled.collect{|a|a.id}
    end

    if @frontend_style == "1"

      if !params[:category_id].blank? && !params[:tag_id].blank?
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids, :tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
        # @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil
      elsif !params[:category_id].blank?
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids, :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
        # @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil
      elsif !params[:tag_id].blank?
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids,:tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
      else
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids, :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
      end

    elsif @frontend_style == "2"

      @archive_file_datas = Array.new

      @archive_file_categorys.each do |afcg|

        if !params[:category_id].blank? && !params[:tag_id].blank?
          @archive_files = ArchiveFile.can_display.where(:category_id.in => [afcg.id], :tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort])
        elsif !params[:category_id].blank?
          @archive_files = ArchiveFile.can_display.where(:category_id.in => [afcg.id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort])
        elsif !params[:tag_id].blank?
          @archive_files = ArchiveFile.can_display.where(:category_id => afcg.id, :tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort])
        else
          @archive_files = ArchiveFile.can_display.where(:category_id => afcg.id, :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort])
        end

        archive_file_datas = Array.new

        @archive_files.each do |af|

          archive_file_multiple_datas = Array.new

          af.archive_file_multiples.asc(:sort_number).each do | afile |
            archive_file_multiple_datas << {
             "file_title" => (!afile.file_title.blank? ? afile.file_title : t('archive.download') ) ,
             "url" => afile.file.url,
             "icon" => af.get_file_icon(afile.file.url)
            } if afile.file.file and afile.choose_lang_display(I18n.locale.to_s)
          end

          archive_file_datas << { "title" => af.title, "is_hot" => (!af.is_hot.blank? ? '1' : ''), "archive_file_multiples" => archive_file_multiple_datas } if !af.archive_file_multiples.blank? and !archive_file_multiple_datas.blank?
        end
  
        @archive_file_datas << { "title" => afcg.title, "archive_files" => archive_file_datas } if !@archive_files.blank?
      end

    end   
    # get_categorys

    if params[:sort]
      @archive_files = get_sorted_and_filtered('archive_file', nil, @archive_files)
    end
  end

  def show
    @item = Page.find(params[:page_id])
    @title = @item.title
    @archive_file = ArchiveFile.find(params[:id])
    get_categorys
  end

  protected

  def reload_archive_files
    @item = Page.find(params[:page_id])
    @title = @item.title
    if @item.frontend_data_count
      @page_num = @item.frontend_data_count
    else
      @page_num = 0
    end
    date_now = Time.now
    @archive_file_categorys = ArchiveFileCategory.all
    # @archive_files = ArchiveFile.where( :is_hidden => false ).desc(:is_top).page(params[:page]).per(@page_num)

    if !params[:category_id].blank?
      @archive_files = ArchiveFile.can_display.where(:archive_file_category_id => params[:category_id]).desc( :is_top ).page( params[:page_main] ).per(@page_num)
      @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil
    elsif !params[:tag_id].blank?
      @archive_files = ArchiveFile.can_display.where(:tagged_ids => params[:tag_id]).desc( :is_top ).page( params[:page_main] ).per(@page_num)
    else
      @archive_files = ArchiveFile.can_display.desc( :is_top ).page( params[:page_main] ).per(@page_num)
    end
    get_categorys
  end

  def get_categorys
    @archive_file_categorys = ArchiveFileCategory.excludes('disabled' => true)
  end
end
