namespace :qi do
  desc "TODO"
  task migrate_all: :environment do
    
    
    puts "test"
    Dir["#{Rails.root.join('db','migrate')}/*"].sort.each do |mf|
      version = mf.split('/')[-1].split('_')[0]
      puts version
      ENV['VERSION'] = version
      Rake::Task["db:migrate"].execute
      
      sleep(10)
    end
    
    
  end
  
  task :import_photos_organisateurs => :environment do
    if false
      if album = Album.where(:name => "import logos organisateurs").first
      else
        album = Album.create(:name => "import logos organisateurs")
      end 
      dir = "#{Rails.root.join('import_photos','ORGANISATEURS','LOGO')}/*"
      Dir[dir].sort.each do |mf|
      
        puts mf
      
        id = File.basename(mf.to_s.gsub('_',""))
      
        puts id
      
      
        if organisateur = Organisateur.where(:id => id).first
          image_file = ImageFile.new(:file => File.open(mf), :album_id => album.id)
          if image_file.save
            organisateur.logo = image_file
            organisateur.save
          else
            puts image_file.errors.messages
          end
        
        end
      
      end
    else
      if album = Album.where(:name => "import photos organisateurs").first
      else
        album = Album.create(:name => "import photos organisateurs")
      end 
      dir = "#{Rails.root.join('import_photos','ORGANISATEURS','PHOTOS')}/*"
      Dir[dir].sort.each do |mf|
      
        puts mf
      
        id = File.basename(mf.to_s.gsub('_',""))
      
        puts id
      
      
        if organisateur = Organisateur.where(:id => id).first
          image_file = ImageFile.new(:file => File.open(mf), :album_id => album.id)
          if image_file.save
            organisateur.image_file = image_file
            organisateur.save
          else
            puts image_file.errors.messages
          end
        
        end
      
      end
    end
    
  end
  
  
  task :import_photos_circuits => :environment do
    if true
      if album = Album.where(:name => "import logos circuits").first
      else
        album = Album.create(:name => "import logos circuits")
      end 
      dir = "#{Rails.root.join('import_photos','CIRCUITS','LOGO')}/*"
      Dir[dir].sort.each do |mf|
      
        puts mf
      
        id = File.basename(mf.to_s.gsub('_',""))
      
        puts id
      
      
        if organisateur = Circuit.where(:id => id).first
          image_file = ImageFile.new(:file => File.open(mf), :album_id => album.id)
          if image_file.save
            organisateur.logo = image_file
            organisateur.save
          else
            puts image_file.errors.messages
          end
        
        end
      
      end
    else
      if album = Album.where(:name => "import photos circuits").first
      else
        album = Album.create(:name => "import photos circuits")
      end 
      dir = "#{Rails.root.join('import_photos','CIRCUITS','PHOTOS')}/*"
      Dir[dir].sort.each do |mf|
      
        puts mf
      
        id = File.basename(mf.to_s.gsub('_',""))
      
        puts id
      
      
        if organisateur = Circuit.where(:id => id).first
          image_file = ImageFile.new(:file => File.open(mf), :album_id => album.id)
          if image_file.save
            organisateur.image_file = image_file
            organisateur.save
          else
            puts image_file.errors.messages
          end
        
        end
      
      end
    end
    
  end

end