class Public::AnnonceMessagesController < ApplicationController
  layout "public"
  
  before_filter :auth_annonce_account#, :only => [:index, :show]
  def index
    
    @no_search = true
    

    
    @annonce_messages = AnnonceMessage.where("destinataire_id = ? or expediteur_id = ?",current_annonce_account.id, current_annonce_account.id).where("parent_id is null")
    
    @annonce_messages = @annonce_messages.order("last_message_at DESC, created_at DESC").all
    
    
    
    
  end


  def show
    @annonce_message = AnnonceMessage.where("destinataire_id = ? or expediteur_id = ?",current_annonce_account.id, current_annonce_account.id).find(params[:id])
    @annonce_message.readed = true
    @annonce_message.save
  end
  
  def answer
    @parent_annonce_message = AnnonceMessage.where("destinataire_id = ? or expediteur_id = ?",current_annonce_account.id, current_annonce_account.id).find(params[:id])
    @annonce_message = AnnonceMessage.new()
    @annonce_message.expediteur = current_annonce_account
    @annonce_message.destinataire = @parent_annonce_message.expediteur
  end
  
  def answer_save
    
    @parent_annonce_message = AnnonceMessage.where("destinataire_id = ? or expediteur_id = ?",current_annonce_account.id, current_annonce_account.id).find(params[:id])
    @annonce_message = AnnonceMessage.new(params.require(:annonce_message).permit!)
    @annonce_message.expediteur = current_annonce_account
    if @parent_annonce_message.expediteur == current_annonce_account
      @annonce_message.destinataire = @parent_annonce_message.destinataire
    else
      @annonce_message.destinataire = @parent_annonce_message.expediteur
    end
    @annonce_message.parent_id = @parent_annonce_message.id
    if BlackListIp.where(:remote_ip => @annonce_message.remote_ip, :enabled => true).count == 0
      if @annonce_message.save
        IpHistory.create(:element => @annonce_message, :annonce_account => current_annonce_account, :remote_ip => request.remote_ip)
      
        AnnonceAccountMailer.new_message(@annonce_message.destinataire).deliver if @annonce_message.destinataire
        
        @parent_annonce_message.last_message_at = Time.now
        @parent_annonce_message.save
        
      else
        render :action => :answer
      end
    else
        render :inline => "erreur"
    end
    
    
  end


  def new
    
  end

  def edit
   
  end
  
 

  def create
    @annonce_message = AnnonceMessage.new(params.require(:annonce_message).permit!)
    @annonce_message.expediteur = current_annonce_account if current_annonce_account
    @annonce_message.remote_ip = request.remote_ip
    @annonce_message.last_message_at = Time.now
    blacklist_ips = ["173.193.202.116"]
    
    if BlackListIp.where(:remote_ip => @annonce_message.remote_ip, :enabled => true).count == 0
      if @annonce_message.save
        
        IpHistory.create(:element => @annonce_message, :annonce_account => current_annonce_account, :remote_ip => request.remote_ip)
      
        AnnonceAccountMailer.new_message(@annonce_message.destinataire).deliver if @annonce_message.destinataire
      
        redirect_to session[:mail_prev], :notice => "Votre message a bien été envoyé." if session[:mail_prev]
      

          
    	else
    		render :template => "public/annonce_accounts/mail" 
    	end
    end
  end

  
  def update
    @annonce = current_annonce_account.annonces.find(params[:id])
   
    

    
    respond_to do |format|
      if @annonce.update_attributes(params.require(:annonce).permit!)



        format.html { 
          redirect_to public_my_account_path
          }
        format.js
        
      else
        
        format.html { render :action => "edit" }
        format.js { "" }
      end
    end
    
    
      
  end
  		


  def destroy

    	 #@annonce = current_annonce_account.annonces.find(params[:id])
  	   #@annonce.enabled = nil
       #@annonce.save
       
       #redirect_to public_my_account_path, :notice => "Votre annonce a bien été désactivée"

    

  end
  


end
