namespace :image_files do
  desc "TODO"
  
  
  task import_metadatas: :environment do
    ImageFile.all.each do |image_file|
      @image_file = image_file
      puts @image_file.attributes
      xml = @image_file.xml_content
      elements = {}
      elements[:description] = xml.match(/<description>(.*)<description>/m)[1]
      elements[:keywords] = xml.match(/<keywords>(.*)<\/keywords>/m)[1].to_s.split("/sep/")
  
      elements[:id] = xml.match(/<id>(.*)<id>/m)[1]
      elements[:altitude] = xml.match(/<altitude>(.*)<altitude>/m)[1].to_s
      elements[:location] = xml.match(/<location>(.*)<location>/m)[1].to_s.split("/")

     
  
      if @image_file
        puts @image_file.attributes
    
        @image_file.description = elements[:description] if elements[:description] != "missing value"
        @image_file.altitude = elements[:altitude].gsub(/,/, '.')     if elements[:altitude] != "missing value"
        @image_file.latitude = elements[:location][0].gsub(/,/, '.')     if elements[:location][0] != "missing value"
        @image_file.longitude = elements[:location][1].gsub(/,/, '.')     if elements[:location][1] != "missing value"
  
    
    
    
        elements[:keywords].each do |key|
          puts key.to_slug
      
          if key.include?("©")
            @image_file.photograph = key.gsub(/©/, '')    
        
        
          elsif key.to_slug and key.to_slug != ""
        
      
            key = key.force_encoding('UTF-8')
            puts key
            if Tag.find_by_slug(key.to_slug)
              tag = Tag.find_by_slug(key.to_slug)
            else
              tag = Tag.create!(:name =>key)
        
            end
      
            @image_file.tags << tag if tag
      
          end
        end
        @image_file.xml_ok = true
        @image_file.save
      end
  
      puts elements
      puts @image_file.attributes
    
    end
  
  end
  
  
  task import_images: :environment do
    puts "Environment Check: Rails Environment = #{Rails.env}"
    
    
    origine_time = Time.now.to_i
    
    
    ImageFile.destroy_all
    
     if Rails.env == "development"
    
       @folder = "/Volumes/SSD2TO/ov2"
      elsif Rails.env == "production"
       @folder = "/media/bigone/shareds/@onevoice/@onevoice"
     end
    @i = 0
    
    
    Dir.entries(@folder).each do |entry|
      entry_folder = File.join(@folder,entry)
      if File.directory? entry_folder and !(entry =='.' || entry == '..' || entry == '.DS_Store')
      
        @i = @i + 1
        
        
        if @i <= 500000
      
          i_f = 0
          Dir.entries(entry_folder).each do |photo_entry|
            photo_entry_folder = File.join(entry_folder,photo_entry)
            
            if File.directory? entry_folder and !(photo_entry =='.' || photo_entry == '..' || photo_entry == '.DS_Store')
              
              
              if !photo_entry.include? ".xml" and i_f < 1
                i_f += 1
                @image_file = ImageFile.new(:icloud_id => entry, :imported => true)
                @image_file.file = File.open(photo_entry_folder) 
                @image_file.xml_content = File.read(entry_folder+"/"+entry+".xml") 
                
                puts entry
                
                if @image_file.save
                  
                else
                  ImageError.create :path => entry_folder
                  
                end
              
              end
              
              
              
            end
            
          end
      
      

      
      end
    end
    
  end
    
  end

  desc "TODO"
  task import_albums: :environment do
  end

end
