module Avo

def self.asset_manager

def self.asset_manager
  @manager ||= AssetManager.new
end

def self.configuration

def self.configuration
  @configuration ||= Configuration.new
end

def self.configuration=(config)

def self.configuration=(config)
  @configuration = config
end

def self.configure

def self.configure
  yield configuration
end

def self.plugin_manager

def self.plugin_manager
  @plugin_manager ||= PluginManager.new
end

def app_status

def app_status
  license.valid?
end

def avo_filters_installed?

def avo_filters_installed?
  defined?(Avo::DynamicFilters).present?
end

def boot

def boot
  boot_logger
  boot_fields
  @cache_store = get_cache_store
  plugin_manager.boot_plugins
  Avo.run_load_hooks(:boot, self)
end

def boot_fields

def boot_fields
  @field_manager = Avo::Fields::FieldManager.build
end

def boot_logger

def boot_logger
  file_logger = ActiveSupport::Logger.new(Rails.root.join("log", "avo.log"))
  file_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
  file_logger.formatter = proc do |severity, time, progname, msg|
    "[Avo] #{time}: #{msg}\n".tap do |i|
      puts i
    end
  end
  @logger = file_logger
end

def get_cache_store

def get_cache_store
  if Rails.env.production?
    case Rails.cache.class.to_s
    when "ActiveSupport::Cache::MemCacheStore", "ActiveSupport::Cache::RedisCacheStore"
      Rails.cache
    else
      ActiveSupport::Cache.lookup_store(:file_store, Rails.root.join("tmp", "cache"))
    end
  elsif Rails.env.test?
    Rails.cache
  else
    ActiveSupport::Cache.lookup_store(:memory_store)
  end
end

def has_main_menu?

def has_main_menu?
  return false if Avo.license.lacks_with_trial(:menu_editor)
  return false if Avo.configuration.main_menu.nil?
  true
end

def has_profile_menu?

def has_profile_menu?
  return false if Avo.license.lacks_with_trial(:menu_editor)
  return false if Avo.configuration.profile_menu.nil?
  true
end

def init

def init
  Avo::Current.error_manager = Avo::ErrorManager.build
  Avo::Current.resource_manager = Avo::Resources::ResourceManager.build
  Avo::Current.tool_manager = Avo::Tools::ToolManager.build
  Avo.run_load_hooks(:init, self)
end

def main_menu

def main_menu
  return unless Avo.plugin_manager.installed?("avo-menu")
  # Return empty menu if the app doesn't have the profile menu configured
  return Avo::Menu::Builder.new.build unless has_main_menu?
  Avo::Menu::Builder.parse_menu(&Avo.configuration.main_menu)
end

def mount_path

def mount_path
  Avo::Engine.routes.find_script_name({})
end

def profile_menu

def profile_menu
  return unless Avo.plugin_manager.installed?("avo-menu")
  # Return empty menu if the app doesn't have the profile menu configured
  return Avo::Menu::Builder.new.build unless has_profile_menu?
  Avo::Menu::Builder.parse_menu(&Avo.configuration.profile_menu)
end

def root_path(paths: [], query: {}, **args)

Renerate a dynamic root path using the URIService
def root_path(paths: [], query: {}, **args)
  Avo::Services::URIService.parse(Avo::Current.view_context.avo.root_url.to_s)
    .append_paths(paths)
    .append_query(query)
    .to_s
end