# -*- encoding : utf-8 -*-
class Admin::ShareImagesController < ApplicationController
  include ActionController::Streaming
    # enable zipline
    include Zipline
   
   
   
   before_filter :auth_admin, :except => [:show]

  
  layout "admin"
  
  
  def index
  

     
      find_share_images
      
      
  		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 500
  		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
      @share_images = @share_images.page(page).per(per_page)
    
  


   
    
    
  end

  def new

    
    
    @share_image = ShareImage.new()
    @image_files = ImageFile.where(:id => params[:ids].split(",").map(&:to_i))
   
    
    
  end

  def create
    
    @share_image= ShareImage.new(params.require(:share_image).permit!)
    @image_files = ImageFile.where(:id => params[:ids].split(",").map(&:to_i))
     @image_files.each do |image_file|
       @share_image.image_files << image_file
   
     end
    
    respond_to do |format|
      if @share_image.save
    
        format.html { 
          
          }
        format.js
        
      else
        
        format.html { render :action => "new" }
        format.js { render :action => "new" }
      end
    end
  
  end

  def edit

    @share_image= ShareImage.find(params[:id])
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    
    @admin = true
  end
  

  def update
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    @share_image= ShareImage.find(params[:id])
  
    
    if request.xhr?
      @share_image_parent = @share_image.parent if @share_image.parent
      @share_images = ShareImage.where(:parent_id => @share_image.parent_id)
    end
  
    if params[:share_image][:parent_id] and params[:share_image][:parent_id] == "no-menu-selected"
      @share_image.parent_id = nil
      @share_image.save
      params[:share_image].delete(:parent_id)
      
    end
    
    @reorder = true if params[:reorder]
    
    
    
    
  
    
    
    respond_to do |format|
      if @share_image.update_attributes(params.require(:share_image).permit!)
        
        
      
        flash[:notice] = "Le share_image à été modifié avec succès."
        format.html { redirect_to(admin_share_images_path(:parent_id => @share_image.parent_id)) }
        if @reorder
          
          
          format.js { 
            
            render :action => "update" }
        else
         
          format.js { 
             @share_image = ShareImage.find(@share_image.id)
            }
        end
      else
        
        flash[:error] = "Ce share_image n'a pas pu être déplacé."
        if @reorder
          format.js { render :action => "update_reorder_failled" }
        else
          format.html { render :action => "edit" }
          format.js { render :action => "edit" }
        end
        
        
      end
    end
    
  end

  def destroy
    
    params[:lang] = params[:lang] || "fr"
    
   
    
    @share_image = ShareImage.find(params[:id])
    @share_image.destroy
    flash[:notice] = "Lien supprimé"
    respond_to do |format|
    
        
        format.html { redirect_to(admin_share_images_path()) }
        format.js
    
  
    end
  end

  def show
    @share_image= ShareImage.find_by_slug(params[:id])
    
    if @share_image.password? and params[:password] and params[:password].to_s == @share_image.password
      session[:share_image_allowed] = session[:share_image_allowed] || {}
      session[:share_image_allowed][@share_image.slug] = true
      
      
      
    end
    
    render :layout => false
  end
  
  
  def zip
    @share_image= ShareImage.find_by_slug(params[:id])
    
    if @share_image.password? and params[:password] and params[:password].to_s == @share_image.password
      session[:share_image_allowed] = session[:share_image_allowed] || {}
      session[:share_image_allowed][@share_image.slug] = true
      
      
      
    end
    
    
    files = []
    
    @share_image.image_files.each do |image_file|
      files << [File.open(image_file.file.current_path), image_file.id.to_s+File.extname(image_file.file.current_path) ]
        
    end
    
    puts files
    
    zipline( files, @share_image.slug+'.zip')
    
  end
  
  
  
  

end
