// Copyright 2020 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/ash/app_restore/full_restore_prefs.h"

#include "ash/constants/ash_pref_names.h"
#include "ash/constants/chrome_pref_names.h"
#include "ash/wm/window_restore/window_restore_util.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

namespace ash::full_restore {

void RegisterProfilePolicyPrefs(PrefRegistrySimple* registry) {
  registry->RegisterBooleanPref(kRestoreAppsEnabled, true);
  registry->RegisterBooleanPref(kGhostWindowEnabled, true);
}

bool HasSessionStartupPref(PrefService* prefs) {
  return prefs->HasPrefPath(ash::chrome_prefs::kRestoreOnStartup);
}

void SetDefaultRestorePrefIfNecessary(PrefService* prefs) {
  DCHECK(!HasRestorePref(prefs));

  if (!HasSessionStartupPref(prefs)) {
    prefs->SetInteger(prefs::kRestoreAppsAndPagesPrefName,
                      static_cast<int>(RestoreOption::kAskEveryTime));
    return;
  }

  SessionStartupPref session_startup_pref =
      SessionStartupPref::GetStartupPref(prefs);
  if (session_startup_pref.type == SessionStartupPref::LAST) {
    prefs->SetInteger(prefs::kRestoreAppsAndPagesPrefName,
                      static_cast<int>(RestoreOption::kAskEveryTime));
  } else {
    prefs->SetInteger(prefs::kRestoreAppsAndPagesPrefName,
                      static_cast<int>(RestoreOption::kDoNotRestore));
  }
}

void UpdateRestorePrefIfNecessary(PrefService* prefs) {
  if (!prefs->HasPrefPath(ash::chrome_prefs::kRestoreOnStartup)) {
    return;
  }

  SessionStartupPref session_startup_pref =
      SessionStartupPref::GetStartupPref(prefs);
  if (session_startup_pref.ShouldRestoreLastSession()) {
    prefs->SetInteger(prefs::kRestoreAppsAndPagesPrefName,
                      static_cast<int>(RestoreOption::kAlways));
  } else {
    prefs->SetInteger(prefs::kRestoreAppsAndPagesPrefName,
                      static_cast<int>(RestoreOption::kAskEveryTime));
  }
}

}  // namespace ash::full_restore
