# encoding: utf-8
module Hex
  module Cms
    module ControllerMethods
      extend ActiveSupport::Concern

      included do
        if respond_to?(:helper_method)
          helper_method :current_menu, :current_menu?
          helper_method :cms_menu_path
          helper_method :cms_menu_url
        end
      end

      # proxy to the Rails default logger
      def logger
        Rails.logger
      end

      # gets current menu item
      # from rails request
      # you can override this in article controller for example
      def current_menu
        # if we know @cms_page and it's menu, we are lucky...
        return @cms_page.menu if @cms_page && @cms_page.menu
        return @cms_menu if @cms_menu

        # ... if we don't, we must find other way to get it...
        # that means we must override this method!
        nil
      end

      # used when rendering menu... to check if currently rendered item
      # is above (ancestor of) current menu
      def current_menu?(rendered_item)
        return true if cms_menu_path(rendered_item) == request.path

        m = current_menu
        return false if m.blank?
        rendered_item.is_or_is_ancestor_of?(m) || rendered_item.is_or_is_descendant_of?(m)
      end

      # returns path for link to given menu item
      # @param [Cms::Menu] item
      # TODO move to Cms::Menu, if possible - url generation should be it's own job..
      # at least it could generate the whole string for eval (because routes aren't recognized within model)
      def cms_menu_path(item, opts = {})
        raise ArgumentError, "cms_menu_path: first parameter - Cms::Menu expected, got #{item.inspect}" unless item.kind_of?(::Cms::Menu)
        tmp, id_provider = item.menu_route_code.split(':')
        if id_provider.blank?
          logger.debug "cms_menu_path: calling #{tmp} via send..."
          send(:"#{tmp}_path", opts)
        else
          # let's get value for this id_provider
          id = item.send(id_provider)
          logger.debug "cms_menu_path: calling #{tmp} via send... with #{id_provider} as parameter (#{id})"
          send(:"#{tmp}_path", id, opts)
        end
      end

      def cms_menu_url(item, opts = {})
        raise ArgumentError, "cms_menu_url: first parameter - Cms::Menu expected, got #{item.class.name}" unless item.kind_of?(::Cms::Menu)
        tmp, id_provider = item.menu_route_code.split(':')
        if id_provider.blank?
          logger.debug "cms_menu_url: calling #{tmp} via send..."
          send(:"#{tmp}_url", opts)
        else
          # let's get value for this id_provider
          id = item.send(id_provider)
          logger.debug "cms_menu_url: calling #{tmp} via send... with #{id_provider} as parameter (#{id})"
          send(:"#{tmp}_url", id, opts)
        end
      end

      # render 404 page from anywhere
      def render_404
        @web_title = I18n.t('cms.error404.title')
        respond_to do |format|
          format.html { render :template => '/cms/show_404', :layout => 'application', :status => 404 }
          format.all { render :text => I18n.t('cms.error404.title'), :status => 404, :content_type => 'text/html' }
        end
      end
    end
  end
end
