# -*- encoding : utf-8 -*-
  class Admin::ImageFilesController < ApplicationController
  before_filter :auth_admin
  layout "admin"

  def tag_operations
    
  end
  
  def tag_operations_save
    @ids = []
    params[:ids].to_s.split(",").each do |id|
      @ids << id.to_i
    end
    
    @tag_ids = []
    params[:tag_ids].each do |id|
      @tag_ids << id.to_i
    end
    
    @image_files = ImageFile.where(:id => @ids)
    @tags = Tag.where(:id => @tag_ids)
    if params[:operation] == "remove"
      @image_files.each do |image_file|
        TagTaggable.where(:taggable_type => "ImageFile", :taggable_id => image_file.id, :tag_id => @tag_ids).destroy_all
        
      end
      
      
    elsif params[:operation] == "add"
      @image_files.each do |image_file|
        @tags.each do |tag|
          image_file.tags << tag
          
        end
      end
      
    end
    
  end


  def index
    
    @t1 = Time.now
   
    
    
    if params[:album_id]
      params[:album_id] = params[:album_id]
      #elsif session[:album_id]
      #params[:album_id] = session[:album_id]
    else
      #params[:album_id] = 1
    end
    
    if params[:album_folder_id]
      @album_folder = AlbumFolder.find(params[:album_folder_id])
      
      
    end
    
    if params[:tag_id]
      @tag = Tag.find(params[:tag_id])
      
      
    end

    if params[:album_id]
      @album = Album.find(params[:album_id])
      session[:album_id] = @album.id
    end
    
    @albums = Album.all.order(:name)
    
    
    if params[:view_all_images]
      @image_files =ImageFile
    
    elsif params[:view_images]
      @image_files =ImageFile.where(:album_id => nil)
    elsif @album
      @image_files = ImageFile.where(:album_id => @album.id).order("image_files.created_at DESC")
    elsif @album_folder
      @image_files = @album_folder.image_files.order("image_files.created_at DESC")
        
    else
      @image_files =ImageFile.where(:id => nil)
    end
    
    if @tag
      @image_files = @image_files.joins(:tags).where("tags.id" => @tag.id)
    end
    
    @image_file = ImageFile.new(:album_id => @album.id) if params[:album_id]
    
    #@tags_ids = TagTaggable.where(:taggable_type => "ImageFile", :taggable_id => @image_files.ids).map(&:tag_id).uniq
  
    if params[:tag_search_ids] and params[:tag_search_ids].size > 0
      tag_search_ids = []
      params[:tag_search_ids].each do |t|
        tag_search_ids << t.to_i
      end
      @image_files = @image_files.includes(:tag_taggables).where( :tag_taggables => {:tag_id =>  tag_search_ids} )
      
      
      #.where(:tag_taggables => {:tag_id => params[:tag_ids]})
    end
    
    
    
    
    if params[:q] and params[:q] != ""
      @image_files = @image_files.search_by_text(params[:q])
    end
    
    @count = @image_files.count
		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 100
		@page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    @image_files = @image_files.page(@page).per(per_page)
    
    
    # processing...
    
    
    if request.xhr?
    render :layout => false
    end
  end

  def new
  end

  def edit
    @image_file = ImageFile.find(params[:id])
    @ids = params[:ids].to_s.split(",")
  end

  def create
    @image_file_create = true
    @image_file = ImageFile.new(:album_id => params[:album_id], :file =>params[:files])
    
    if @image_file.save

    else

    end


  end

  def update
    @image_file = ImageFile.find(params[:id])
    
    @ids = []
    params[:ids].to_s.split(",").each do |id|
      @ids << id.to_i
    end
    
    
    if @ids.size > 0 and params[:multiples]
      
      params[:image_file].delete(:name) if !params[:name]
      params[:image_file].delete(:photograph) if !params[:photograph]
      params[:image_file].delete(:description) if !params[:description]
      params[:image_file].delete(:tag_ids) if !params[:tags]
      

      
      ImageFile.where(:id => @ids).each do |image_file|
        image_file.update_attributes(params.require(:image_file).permit!)

  
      end
      
      
    else
    
      if @image_file.update_attributes(params.require(:image_file).permit!)

      else
  
      end
      
    end
  end

  def destroy
    @image_file = ImageFile.find(params[:id])
    @image_file.destroy

    flash[:notice] = "L'image à bien été supprimée."
  end

  def show
    @image_file = ImageFile.find(params[:id])
    @ids = params[:ids].to_s.split(",")
  end
  
  def rotate
    deg = params[:direction] == "right" ? -90 : 90
    
    @image_file = ImageFile.find(params[:id])
    @image_file.rotate(deg)
  end
  
  def import_albums

      AlbumFolder.destroy_all
      Album.destroy_all
      @folder = "/Volumes/SSD2TO/ova"
      @i = 0
      explore_folder @folder
    
      render :inline => "ok"
    

  end
  
  def explore_folder(folder, parent_type=nil, parent=nil)
    if @i < 500000
      Dir.entries(folder).each do |entry|
        entry_folder = File.join(folder,entry)
        if File.directory? entry_folder and !(entry =='.' || entry == '..' || entry == '.DS_Store')
        
          type = entry.split("-")[0]
          name = entry.sub! type.to_s+"-", ''
        
          if type == "folder"
            @i += 1
            album_folder = AlbumFolder.create(:name => name, :parent_id => (parent.id if parent))
          
            explore_folder entry_folder, "folder", album_folder 
        
          elsif type == "album"
            @i += 1
            album = Album.create(:name => name, :album_folder_id => (parent.id if parent))
        
            explore_folder entry_folder, "album", album 
        
          elsif type == "photo"
            if parent_type == "album"
            
              image_file = ImageFile.find_by_icloud_id(name.to_s)
              if image_file
                image_file.album_id = parent.id
                image_file.save
                
              end
              
            end
            
          
          else
            explore_folder entry_folder
          end
        
        
        
  
        
        end
      end
    end
  end
  
  def import_images
    





    
    render :inline =>( Time.now.to_i - origine_time.to_i).to_s
    
      
  end
  
  
  def download
    @image_file = ImageFile.find(params[:id])
    send_file @image_file.file.path, :disposition => 'attachment'
    
  end
  
  
  
  
  
  
  

end
