/*
 * Copyright (C) 2020 The Flogger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.flogger.context;

import com.google.common.flogger.FluentLogger;
import com.google.common.flogger.StackSize;
import com.google.common.flogger.context.ScopedLoggingContext.LoggingContextCloseable;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Fallback context data provider used when no other implementations are available for a platform.
 */
final class NoOpContextDataProvider extends ContextDataProvider {
  private static final ContextDataProvider NO_OP_INSTANCE = new NoOpContextDataProvider();

  /**
   * Returns a singleton "no op" instance of the context data provider API which logs a warning if
   * used in code which attempts to set context information or modify scopes. This is intended for
   * use by platform implementations in cases where no context is configured.
   */
  static final ContextDataProvider getNoOpInstance() {
    return NO_OP_INSTANCE;
  }

  private static final class NoOpScopedLoggingContext extends ScopedLoggingContext
      implements LoggingContextCloseable {
    // Since the ContextDataProvider class is loaded during Platform initialization we must be very
    // careful to avoid any attempt to obtain a logger instance until we can be sure logging config
    // is complete.
    private static final class LazyLogger {
      private static final FluentLogger logger = FluentLogger.forEnclosingClass();
    }
    private final AtomicBoolean haveWarned = new AtomicBoolean();

    private void logWarningOnceOnly() {
      if (haveWarned.compareAndSet(false, true)) {
        LazyLogger.logger
            .atWarning()
            .withStackTrace(StackSize.SMALL)
            .log(
                "Scoped logging contexts are disabled; no context data provider was installed.\n"
                    + "To enable scoped logging contexts in your application, see the "
                    + "site-specific Platform class used to configure logging behaviour.\n"
                    + "Default Platform: com.google.common.flogger.backend.system.DefaultPlatform");
      }
    }

    @Override
    public ScopedLoggingContext.Builder newContext() {
      return new ScopedLoggingContext.Builder() {
        @Override
        public LoggingContextCloseable install() {
          logWarningOnceOnly();
          return NoOpScopedLoggingContext.this;
        }
      };
    }

    @Override
    public ScopedLoggingContext.Builder newContext(ScopeType scopeType) {
      // Ignore scope bindings when there's no way to propagate them.
      return newContext();
    }

    @Override
    public boolean addTags(Tags tags) {
      logWarningOnceOnly();
      return false;
    }

    @Override
    public boolean applyLogLevelMap(LogLevelMap m) {
      logWarningOnceOnly();
      return false;
    }

    @Override
    public void close() {}
  }

  private final ScopedLoggingContext noOpContext = new NoOpScopedLoggingContext();

  @Override
  public ScopedLoggingContext getContextApiSingleton() {
    return noOpContext;
  }

  @Override
  public String toString() {
    return "No-op Provider";
  }
}

@@ -642,16 +642,17 @@
 ntext;%0A%0A
+%0A
 import c
@@ -690,16 +690,62 @@
 Logger;%0A
+import com.google.common.flogger.MetadataKey;%0A
 import c
@@ -3273,36 +3273,310 @@
 ly();%0A      
-return false
+// Superclass methods still do argument checking, which is important for consistent behaviour.%0A      return super.addTags(tags);%0A    %7D%0A%0A    @Override%0A    public %3CT%3E boolean addMetadata(MetadataKey%3CT%3E key, T value) %7B%0A      logWarningOnceOnly();%0A      return super.addMetadata(key, value)
 ;%0A    %7D%0A%0A   
@@ -3634,17 +3634,27 @@
 evelMap 
-m
+logLevelMap
 ) %7B%0A    
@@ -3690,21 +3690,51 @@
  return 
-false
+super.applyLogLevelMap(logLevelMap)
 ;%0A    %7D%0A

Add "addMetadata()" overload to the no-op provider to ensure warnings are issued as expected.