// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/permissions/system/system_permission_settings.h"

#include <map>
#include <memory>
#include <utility>

#include "base/auto_reset.h"
#include "base/check.h"
#include "base/check_deref.h"
#include "base/no_destructor.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/permissions/system/platform_handle.h"

namespace system_permission_settings {

namespace {

PlatformHandle* instance_for_testing_ = nullptr;
bool g_mock_system_prompt_for_testing_ = false;
bool g_mock_system_permission_denied_for_testing_ = false;

std::map<ContentSettingsType, bool>& GlobalTestingBlockOverrides() {
  static base::NoDestructor<std::map<ContentSettingsType, bool>>
      g_testing_block_overrides;
  return *g_testing_block_overrides;
}

PlatformHandle* GetPlatformHandle() {
  if (instance_for_testing_) {
    return instance_for_testing_;
  }
  return CHECK_DEREF(CHECK_DEREF(g_browser_process).GetFeatures())
      .system_permissions_platform_handle();
}

}  // namespace

// static
void SetInstanceForTesting(PlatformHandle* instance_for_testing) {
  instance_for_testing_ = instance_for_testing;
}

// static
bool CanPrompt(ContentSettingsType type) {
  if (g_mock_system_prompt_for_testing_) {
    return true;
  }

  if (GlobalTestingBlockOverrides().contains(type)) {
    return false;
  }

  return GetPlatformHandle()->CanPrompt(type);
}

// static
base::AutoReset<bool> MockSystemPromptForTesting() {
  return base::AutoReset<bool>(&g_mock_system_prompt_for_testing_, true);
}
// static
base::AutoReset<bool> MockShowSystemSettingsForTesting() {
  return base::AutoReset<bool>(&g_mock_system_permission_denied_for_testing_,
                               true);
}

// static
bool IsDenied(ContentSettingsType type) {
  if (g_mock_system_permission_denied_for_testing_) {
    return true;
  }

  if (GlobalTestingBlockOverrides().find(type) !=
      GlobalTestingBlockOverrides().end()) {
    return GlobalTestingBlockOverrides().at(type);
  }
  return GetPlatformHandle()->IsDenied(type);
}

// static
bool IsAllowed(ContentSettingsType type) {
  if (g_mock_system_permission_denied_for_testing_) {
    return false;
  }

  if (GlobalTestingBlockOverrides().find(type) !=
      GlobalTestingBlockOverrides().end()) {
    return !GlobalTestingBlockOverrides().at(type);
  }
  return GetPlatformHandle()->IsAllowed(type);
}

// static
void IsDeniedFresh(ContentSettingsType type,
                   SystemPermissionDeniedCallback callback) {
  if (g_mock_system_permission_denied_for_testing_) {
    std::move(callback).Run(true);
    return;
  }
  if (GlobalTestingBlockOverrides().find(type) !=
      GlobalTestingBlockOverrides().end()) {
    std::move(callback).Run(GlobalTestingBlockOverrides().at(type));
    return;
  }
  GetPlatformHandle()->IsDeniedFresh(type, std::move(callback));
}

// static
void OpenSystemSettings(content::WebContents* web_contents,
                        ContentSettingsType type) {
  GetPlatformHandle()->OpenSystemSettings(web_contents, type);
}

// static
void Request(ContentSettingsType type,
             SystemPermissionResponseCallback callback) {
  // If 'g_mock_system_prompt_for_testing_' is true, don't request the
  // permission.
  if (g_mock_system_prompt_for_testing_) {
    return;
  }
  GetPlatformHandle()->Request(type, std::move(callback));
}

// static
std::unique_ptr<ScopedObservation> Observe(
    SystemPermissionChangedCallback observer) {
  return GetPlatformHandle()->Observe(observer);
}

ScopedSettingsForTesting::ScopedSettingsForTesting(ContentSettingsType type,
                                                   bool blocked)
    : type_(type) {
  CHECK(GlobalTestingBlockOverrides().find(type) ==
        GlobalTestingBlockOverrides().end());
  GlobalTestingBlockOverrides()[type] = blocked;
}

ScopedSettingsForTesting::~ScopedSettingsForTesting() {
  GlobalTestingBlockOverrides().erase(type_);
}

}  // namespace system_permission_settings
