// 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.

#import "ios/chrome/test/ios_chrome_scoped_testing_variations_service.h"

#import "components/metrics/startup_visibility.h"
#import "components/network_time/network_time_tracker.h"
#import "components/variations/service/variations_service.h"
#import "components/variations/service/variations_service_client.h"
#import "ios/chrome/test/testing_application_context.h"
#import "services/network/public/cpp/shared_url_loader_factory.h"
#import "services/network/test/test_network_connection_tracker.h"
#import "testing/gtest/include/gtest/gtest.h"

using variations::VariationsService;
using variations::VariationsServiceClient;

// Test VariationsServiceClient used to create
// IOSChromeScopedTestingVariationsService.
// TODO(crbug.com/377275759): Check if TestVariationsServiceClient and
// IOSChromeScopedTestingVariationsService can be consolidated with
// implementations elsewhere.
class TestVariationsServiceClient : public VariationsServiceClient {
 public:
  TestVariationsServiceClient() = default;
  TestVariationsServiceClient(const TestVariationsServiceClient&) = delete;
  TestVariationsServiceClient& operator=(const TestVariationsServiceClient&) =
      delete;
  ~TestVariationsServiceClient() override = default;

  // VariationsServiceClient:
  base::Version GetVersionForSimulation() override { return base::Version(); }
  scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory()
      override {
    return nullptr;
  }
  network_time::NetworkTimeTracker* GetNetworkTimeTracker() override {
    return nullptr;
  }
  bool OverridesRestrictParameter(std::string* parameter) override {
    return false;
  }
  bool IsEnterprise() override { return false; }

 private:
  // VariationsServiceClient:
  version_info::Channel GetChannel() override {
    return version_info::Channel::UNKNOWN;
  }
};

IOSChromeScopedTestingVariationsService::
    IOSChromeScopedTestingVariationsService() {
  EXPECT_EQ(nullptr,
            TestingApplicationContext::GetGlobal()->GetVariationsService());
  enabled_state_provider_ =
      std::make_unique<metrics::TestEnabledStateProvider>(false, false);
  metrics_state_manager_ = metrics::MetricsStateManager::Create(
      TestingApplicationContext::GetGlobal()->GetLocalState(),
      enabled_state_provider_.get(),
      /*backup_registry_key=*/std::wstring(),
      /*user_data_dir=*/base::FilePath(), metrics::StartupVisibility::kUnknown);

  variations_service_ = VariationsService::Create(
      std::make_unique<TestVariationsServiceClient>(),
      TestingApplicationContext::GetGlobal()->GetLocalState(),
      metrics_state_manager_.get(),
      /*disable_network_switch=*/"dummy-disable-background-switch",
      network::TestNetworkConnectionTracker::CreateGetter());
  TestingApplicationContext::GetGlobal()->SetVariationsService(
      variations_service_.get());
}

IOSChromeScopedTestingVariationsService::
    ~IOSChromeScopedTestingVariationsService() {
  EXPECT_EQ(variations_service_.get(),
            TestingApplicationContext::GetGlobal()->GetVariationsService());
  TestingApplicationContext::GetGlobal()->SetVariationsService(nullptr);
  variations_service_.reset();
  metrics_state_manager_.reset();
  enabled_state_provider_.reset();
}

VariationsService* IOSChromeScopedTestingVariationsService::Get() {
  return variations_service_.get();
}
