class ActionController::Base
  before_action :lookup_domain if Rails.env.production?
  before_action :set_locale

  helper_method :current_license,

  def not_found
    respond_to do |format|
      format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
      format.json { head :not_found }
    end
  end

  private

  def lookup_domain
    if ENV["SERVER_MODE"] == "cloud" && request.subdomains.last && request.subdomains.last != "www"
      if license = cache_license # request.subdomain.downcase
        logger.info "Location - " + license.dbschema
        ActiveRecord::Base.establish_connection(website_connection(license))
      else
        # reconnect_default_db
        logger.info 'License is nil'
        not_found
      end
    elsif ENV["SERVER_MODE"] == "application" || request.subdomains.last && request.subdomains.last != "www"
      # check for license file
      if !current_license.exists?
        redirect_to activate_path
      elsif current_license.expired?
        redirect_to review_license_path
      end
    else
      not_found
    end
  end

  def current_license
    @license ||= License.new(ENV["SX_PROVISION_URL"], request.host)
  end

  def cache_license
    if (current_license.detail_with_local_cache == true)
      return current_license
    else
      return nil
    end
  end

  def check_subdomain
    current_license.check_license_subdomain
  end

  def check_installation
    if current_company.nil?
      redirect_to install_path
    end
  end

  def website_connection(license)
    default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase,
    :username => license.dbusername, :password => license.dbpassword)
  end

  def reconnect_default_db
    ActiveRecord::Base.establish_connection(Rails.env)
  end

  # Regular database.yml configuration hash
  def default_connection
    @default_config ||= ActiveRecord::Base.connection_config
  end

  #change locallization
  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  # RESTful url for localize
  def default_url_options
    { locale: I18n.locale }
  end

end

class ActionController::API
  before_action :lookup_domain if Rails.env.production?

  def not_found
    respond_to do |format|
      format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
      format.json { head :not_found }
    end
  end

  private

  def lookup_domain
    if ENV["SERVER_MODE"] == "cloud" && request.subdomains.last && request.subdomains.last != "www"
      if license = cache_license # request.subdomain.downcase
        logger.info "Location - " + license.dbschema
        ActiveRecord::Base.establish_connection(website_connection(license))
      else
        # reconnect_default_db
        logger.info 'License is nil'
        not_found
      end
    elsif ENV["SERVER_MODE"] == "application" || (request.subdomains.last && request.subdomains.last != "www")
      not_found unless current_license.exists? && !current_license.expired?
    else
      not_found
    end
  end

  def current_license
    @license ||= License.new(ENV["SX_PROVISION_URL"], request.host)
  end

  def cache_license
    if (current_license.detail_with_local_cache == true)
      return current_license
    else
      return nil
    end
  end

  def check_license(lookup)
    License.check_license_file(lookup)
  end

  def website_connection(license)
    default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase,
    :username => license.dbusername, :password => license.dbpassword)
  end

  def reconnect_default_db
    ActiveRecord::Base.establish_connection(Rails.env)
  end

  # Regular database.yml configuration hash
  def default_connection
    @default_config ||= ActiveRecord::Base.connection.instance_variable_get("@config").dup
  end

  #change locallization
  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  # RESTful url for localize
  def default_url_options
    { locale: I18n.locale }
  end

end
