# -*- encoding : utf-8 -*-
class Public::MenuItemsController < ApplicationController
  
  layout :get_public_layout
  
  def redirect
    redirect_to menu_item_path(:url => params[:url]), status: 301
  end
  
  def archive
    @menu_item= MenuItem.archived.find(params[:id])
  end
  
  
  def show
    
    if params[:lang]
      
      @lang = LangSite.find_by_slug(params[:lang])
    else
      @lang = LangSite.first
      
    end
    
    
    if params[:url]
      
      url = params[:url].split("/")
      url = url.map do |u|
        u.to_slug
      end
      url = url.join("/")
      
      orignal_url = url
      
      if url.scan(/peuples\-sauvages\/experimentes/).size > 0
        url = url.gsub(/peuples\-sauvages\/experimentes/, 'compagnons-animaux/experimentes') 
        @original_menu_item = MenuItem.find(19)
      end  
      if url.scan(/animaux\-domestiques\/experimentes/).size > 0
        url = url.gsub(/animaux\-domestiques\/experimentes/, 'compagnons-animaux/experimentes') 
        @original_menu_item = MenuItem.find(20)
      end  
        
      
      
      

      menu_id = [1,2]
      
      
      
      @menu_item_lang = @lang.menu_item_langs.joins(:menu_item).where(:menu_items => {:menu_id => menu_id}).find_by_permalink(url)
      
      if @menu_item_lang
        @menu_item = @menu_item_lang.menu_item
      
        @menu_item.ancestors_menu = @menu_item.ancestors
        @ancestor_1 = @menu_item.ancestors.reverse[1] if @menu_item
      
      elsif menu_item_history = MenuItemSlugHistory.where(:permalink => url, :locale => @lang.slug).first
        redirect_to "/#{@lang.slug}/#{menu_item_history.menu_item_lang.permalink}.html"
        
        
      else
        redirect_to "/"
      end
      

      
    else
      menu_id = 1
      
      @menu_item_lang = @lang.menu_item_langs.joins(:menu_item).where(:menu_items => {:menu_id => menu_id}).find_by_permalink("index")
      @menu_item = @menu_item_lang.menu_item
      
      if cookies.permanent[:visites_enquete]
        cookies.permanent[:visites_enquete] =cookies.permanent[:visites_enquete].to_i + 1
      else
        cookies.permanent[:visites_enquete] = 1
      end
    
    
    end
    if @menu_item and @menu_item.menu_content and @menu_item.menu_content.class.to_s == "Page"
      @lang_page = @menu_item.menu_content.lang_pages.find_by_lang_site_id(@lang.id)
      @title = @lang_page.title
      @description = @lang_page.description
      @keywords = @lang_page.keywords
      @twitter_title = @lang_page.twitter_title if @lang_page.twitter_title?

    end
    
    if @menu_item and @menu_item.menu_content and @menu_item.menu_content.page_type_id == 2
      @url_to_redirect = nil
      
      MenuItem.where(:parent_id => @menu_item.id).order(:position).each do |menu_item|

        menu_item_lang = menu_item.menu_item_langs.find_by_lang_site_id(@lang.id)



        if menu_item_lang.enabled == true and menu_item_lang.visible == true and menu_item.menu_content.page_type_id == 3
          @url_to_redirect = menu_item_path(:url => menu_item_lang.url, :lang => @lang.slug)
          break
          
        end
      end
      
      if @url_to_redirect
        redirect_to @url_to_redirect
      end
      
    end
    
   
    
  end

end
