module BitsnoteAssets
  module ActionViewExtensions
    OFFLINE = (::Rails.env.development? or ::Rails.env.test?)

    ANGULAR_VERSION = '1.2.23'
    JQUERY_UI_VERSION = "1.11.1"
    BOOTSTRAP_VERSION = "3.2.0"
    FONTAWESOME_VERSION = "4.1.0"

    GOOGLE_CDN = "//ajax.googleapis.com"
    BOOTSTRAP_CDN = "//maxcdn.bootstrapcdn.com"


    CDNS = {
      :angular_js => {
        :default => "#{GOOGLE_CDN}/ajax/libs/angularjs/#{ANGULAR_VERSION}/angular.min.js"
      },
      :jquery_ui => {
        :google     => "#{GOOGLE_CDN}/ajax/libs/jqueryui/#{JQUERY_UI_VERSION}/jquery-ui.min.js",
        :microsoft  => "//ajax.aspnetcdn.com/ajax/jquery.ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js",
        :jquery     => "//code.jquery.com/ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js",
        :yandex     => "//yandex.st/jquery-ui/#{JQUERY_UI_VERSION}/jquery-ui.min.js"
      },
:bootstrap_js => {
        :default => "#{BOOTSTRAP_CDN}/bootstrap/#{BOOTSTRAP_VERSION}/js/bootstrap.min.js"
      },
      :bootstrap_css => {
        :default => "#{BOOTSTRAP_CDN}/bootstrap/#{BOOTSTRAP_VERSION}/css/bootstrap.min.css"
      },
      :bootstrap_theme_css => {
        :default => "#{BOOTSTRAP_CDN}/bootstrap/#{BOOTSTRAP_VERSION}/css/bootstrap-theme.min.css"
      },
      :fontawesome_css => {
        :default => "#{BOOTSTRAP_CDN}/font-awesome/#{FONTAWESOME_VERSION}/css/font-awesome.min.css"
      }
    }

    def angular_js_url(name)
      return CDNS[:angular_js][name]
    end

    def jquery_ui_url(name)
      return CDNS[:jquery_ui][name]
    end

    def bootstrap_javascript_url(name)
      return CDNS[:bootstrap_js][name]
    end

    def bootstrap_stylesheet_url(name)
      return CDNS[:bootstrap_css][name]
    end

    def bootstrap_theme_stylesheet_url(name)
      return CDNS[:bootstrap_theme_css][name]
    end

    def fontawesome_stylesheet_url(name)
      return CDNS[:fontawesome_css][name]
    end

    def js_include_tag(cdn, asset, url, options = {})
      options.reverse_merge! :local_copy => false

      asset = asset + '.min' if options.delete(:compressed)

      if OFFLINE and !options.delete(:force)
        options.delete(:local_copy) # not used in OFFLINE mode
        return javascript_include_tag(asset, options)
      else
        local_copy = options.delete(:local_copy)
        j = [ javascript_include_tag(self.send(url, cdn.to_sym), options) ]
        if local_copy
          v = yield(asset)
          j << javascript_tag("#{v} || document.write(unescape('#{javascript_include_tag(asset, options).gsub('<','%3C')}'))")
        end
        j.join("\n").html_safe
      end
    end

    def jquery_ui_include_tag(name, options = {})
      js_include_tag(name, 'jquery-ui/jquery-ui', :jquery_ui_url, options) do |asset|
        "window.jQuery.ui"
      end
    end

    def angular_js_include_tag(name, options = {})
      js_include_tag(name, 'angular/angular', :angular_js_url, options) do |asset|
        "window.angular"
      end
    end
  end

  class Engine < ::Rails::Engine
    initializer 'bitsnote-assets.action_view' do |app|
      ActiveSupport.on_load(:action_view) do
        include BitsnoteAssets::ActionViewExtensions
      end
    end
  end
end