class Public::AnnonceAccountsController < ApplicationController
  layout "public"
  
  before_filter :auth_annonce_account, :except => [:new, :create, :confirm, :show, :parrainage, :find_parrain]
  
  
  def parrainage
    cookies.permanent[:mlm_token] = params[:mlm_token]
    redirect_to "/"
  end
  
  def index
    
    @annonce_accounts = AnnonceAccount.all
  end


  def show
		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 10
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    
    @annonce_account = AnnonceAccount.enableds.find_by_id(params[:id])
    if @annonce_account
      @annonces = @annonce_account.annonces.valid_to_show.order("list_head DESC, created_at DESC").page(page).per(per_page).all
    end
  end


  def new
    if params[:r] and params[:r] != ""
      session[:before_auth_url] = nil
    end
    
    
    @no_search = true
    if params[:for_annonce] and params[:for_annonce] != ""
      session[:for_annonce] = true
      params[:step] = "login"
    else
      session[:for_annonce] = nil
      params[:for_annonce] = nil
    end
    
    params[:for_mail] =nil if params[:for_mail] == ""
    
    @annonce_account = AnnonceAccount.new()
    if cookies[:mlm_token] and @parent = AnnonceAccount.find_parrain(cookies[:mlm_token]) 
      @annonce_account.parent_code = @parent.mlm_token.upcase
    
    elsif params[:p] and @parent = AnnonceAccount.find_parrain(params[:p]) 
      @annonce_account.parent_code = @parent.mlm_token.upcase
      
    end  

  end

  def show_details
    @no_search = true
    if current_annonce_account.mlm_children_ids.include?(params[:id].to_i)
      
      @annonce_account = AnnonceAccount.find(params[:id])
      
      
    end
    
  end

  def edit
    @annonce_account = AnnonceAccount.find(params[:id])
  
  end

  def find_parrain
    @parent = AnnonceAccount.find_parrain(params[:parent_code]) 
    
    if @parent
      cookies.permanent[:mlm_token] = @parent.mlm_token
    else
      cookies.permanent[:mlm_token] = nil
    end
  end


  def create
    params[:for_annonce] =nil if params[:for_annonce] == ""
    params[:for_mail] =nil if params[:for_mail] == ""
    
    params[:step] = "login"
    @no_search = true
  	@annonce_account = AnnonceAccount.new(params.require(:annonce_account).permit!)
    @annonce_account.remote_ip = request.remote_ip
    
    @parent = AnnonceAccount.find_parrain(@annonce_account.parent_code) 
    
    if @annonce_account.save
      IpHistory.create(:element => @annonce_account, :annonce_account => @annonce_account, :remote_ip => request.remote_ip)
      AnnonceAccountMailer.confirm(@annonce_account).deliver
      if @parent
        AnnonceAccountMailer.new_user(@annonce_account).deliver
      end
      @annonce_account.authenticate(params[:password])
      
      cookies[:annonce_account_auth_token] = @annonce_account.token
      
      
      if session[:for_annonce]
        session[:for_annonce] =nil 
        redirect_to new_public_annonce_path(:ins => "ins_finish"), notice: "Vous êtes désormais inscrit."
      elsif session[:before_auth_url]
        url = session[:before_auth_url]
        session[:before_auth_url] = nil
        redirect_to url
      else
        redirect_to public_my_account_path(:ins => "ins_finish"), notice: "Vous êtes désormais inscrit."
      end
    
          
  	else
  		render :action => "new" 
  	end
  end

  def confirm
    @annonce_account = AnnonceAccount.find_by_token(params[:id])
    @annonce_account.enabled = true
    @annonce_account.save(:validate => false)
    
    
    cookies[:annonce_account_auth_token] = @annonce_account.token
    
    redirect_to public_my_account_path
    #redirect_to "/"
  end
  
  
  def update
    @annonce_account = AnnonceAccount.find(params[:id])
    
    if params[:order]
      
    @annonce_account.force_address = true  
    end
    
    if @annonce_account == current_annonce_account
    	
    
      if @annonce_account.update_attributes(params.require(:annonce_account).permit!)
        
        if params[:order]
          
          if params[:credit_product_id]
            @credit_product = CreditProduct.find(params[:credit_product_id])
            @order = Order.new(:devise => @credit_product.devise)
            session[:order] = @order
            session[:order].credit_product = @credit_product
      
      
      
          elsif session[:order]
            @order = Order.new(session[:order])
          else
      
          end
    
          @annonce_account = current_annonce_account
          @order.annonce_account = @annonce_account
          
          render :template => "public/orders/new"
        else
          
          redirect_to public_my_account_path
        end
    	else
        if params[:order]
          @order = Order.new(session[:order])
    		  render :template => "public/orders/billing"
        else
          render :template => "public/my_account/edit_infos" 
          
        end
      end
    end
  end
  		


  def destroy

    	#@annonce_account = AnnonceAccount.find(params[:id])
  	  #@annonce_account.destroy

    

  end


  def mail
    @no_search = true
    @annonce_account = AnnonceAccount.enableds.find_by_id(params[:id])
      
    if @annonce_account
      @annonce_message = AnnonceMessage.new(:destinataire => @annonce_account)
      if params[:annonce_id]
        if current_annonce_account and @annonce = current_annonce_account.annonces.find_by_id(params[:annonce_id])
      
        elsif current_admin
      
          @annonce = Annonce.find(params[:annonce_id])
        else
      
          @annonce = Annonce.valid_to_show.find_by_id(params[:annonce_id])
        end
        if @annonce
          @annonce_message.annonce = @annonce
        else
          @annonce_invalide = true
        end
      
      
      end
    
      @annonce_message.expediteur  = current_annonce_account if current_annonce_account
      if @annonce_message.expediteur
        @annonce_message.expediteur_mail = current_annonce_account.email
        @annonce_message.tel = current_annonce_account.tel_number
      end
    end
    
  end


end
