// Copyright 2023 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/browser/ntp/model/set_up_list_prefs.h"

#import "components/ntp_tiles/pref_names.h"
#import "components/prefs/pref_registry_simple.h"
#import "components/prefs/pref_service.h"
#import "ios/chrome/browser/ntp/model/set_up_list_item_type.h"
#import "ios/chrome/browser/ntp/model/set_up_list_metrics.h"
#import "ios/chrome/browser/shared/public/features/features.h"

namespace set_up_list_prefs {

const char kDefaultBrowserItemState[] =
    "set_up_list.default_browser_item.state";
const char kAutofillItemState[] = "set_up_list.autofill_item.state";
const char kNotificationsItemState[] =
    "set_up_list.content_notification_item.state";
const char kSafariImportItemState[] = "set_up_list.safari_import_item.state";
const char kBackgroundCustomizationItemState[] =
    "set_up_list.background_customization_item.state";
const char kAllItemsComplete[] = "set_up_list.all_items_complete";
const char kLastInteraction[] = "set_up_list.last_interaction";

void RegisterPrefs(PrefRegistrySimple* registry) {
  int unknown = static_cast<int>(SetUpListItemState::kUnknown);
  registry->RegisterIntegerPref(kDefaultBrowserItemState, unknown);
  registry->RegisterIntegerPref(kAutofillItemState, unknown);
  registry->RegisterIntegerPref(kNotificationsItemState, unknown);
  registry->RegisterIntegerPref(kSafariImportItemState, unknown);
  registry->RegisterIntegerPref(kBackgroundCustomizationItemState, unknown);
  registry->RegisterBooleanPref(kAllItemsComplete, false);
  registry->RegisterTimePref(kLastInteraction, base::Time());
}

const char* PrefNameForItem(SetUpListItemType type) {
  switch (type) {
    case SetUpListItemType::kDefaultBrowser:
      return kDefaultBrowserItemState;
    case SetUpListItemType::kAutofill:
      return kAutofillItemState;
    case SetUpListItemType::kNotifications:
      return kNotificationsItemState;
    case SetUpListItemType::kAllSet:
      NOTREACHED();
    case SetUpListItemType::kSafariImport:
      return kSafariImportItemState;
    case SetUpListItemType::kBackgroundCustomization:
      return kBackgroundCustomizationItemState;
  }
}

SetUpListItemState GetItemState(PrefService* prefs, SetUpListItemType type) {
  int value = prefs->GetInteger(PrefNameForItem(type));
  return static_cast<SetUpListItemState>(value);
}

void SetItemState(PrefService* prefs,
                  SetUpListItemType type,
                  SetUpListItemState state) {
  int value = static_cast<int>(state);
  prefs->SetInteger(PrefNameForItem(type), value);
}

void MarkItemComplete(PrefService* prefs, SetUpListItemType type) {
  // If it is already completed and already removed from list, skip setting.
  switch (GetItemState(prefs, type)) {
    case SetUpListItemState::kUnknown:
    case SetUpListItemState::kNotComplete:
      set_up_list_metrics::RecordItemCompleted(type);
      SetItemState(prefs, type, SetUpListItemState::kCompleteInList);
      break;
    case SetUpListItemState::kCompleteInList:
    case SetUpListItemState::kCompleteNotInList:
      // Already complete, so there is nothing to do.
      break;
  }
}

void MarkAllItemsComplete(PrefService* prefs) {
  if (prefs->GetBoolean(kAllItemsComplete)) {
    // All items were already complete.
    return;
  }
  prefs->SetBoolean(kAllItemsComplete, true);
  set_up_list_metrics::RecordAllItemsCompleted();
}

bool AllItemsComplete(PrefService* prefs) {
  return prefs->GetBoolean(kAllItemsComplete);
}

bool IsSetUpListDisabled(PrefService* prefs) {
  return !prefs->GetBoolean(ntp_tiles::prefs::kTipsHomeModuleEnabled);
}

void RecordInteraction(PrefService* prefs) {
  prefs->SetTime(kLastInteraction, base::Time::Now());
}

base::Time GetLastInteraction(PrefService* prefs) {
  return prefs->GetTime(kLastInteraction);
}

}  // namespace set_up_list_prefs
