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

#ifndef CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
#define CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_

#include <memory>
#include <string_view>

#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/ui/page_action/page_action_controller.h"
#include "components/find_in_page/find_result_observer.h"
#include "components/find_in_page/find_tab_helper.h"
#include "content/public/browser/web_contents_observer.h"

class FindBar;
class FindBarPlatformHelper;

namespace chrome {
class BrowserCommandController;
}

namespace content {
class WebContents;
}

namespace gfx {
class Rect;
}

namespace find_in_page {
enum class SelectionAction;
enum class ResultAction;
}  // namespace find_in_page

class FindBarController : public content::WebContentsObserver,
                          public find_in_page::FindResultObserver {
 public:
  FindBarController(
      std::unique_ptr<FindBar> find_bar,
      chrome::BrowserCommandController* browser_command_controller);

  FindBarController(const FindBarController&) = delete;
  FindBarController& operator=(const FindBarController&) = delete;

  ~FindBarController() override;

  // Shows the find bar. Any previous search string will again be visible.
  // The find operation will also be started depending on |find_next| and
  // if there is currently a text selection. |find_next| means the user
  // used a command to advance the search and |forward_direction| indicates if
  // the find should be forward or backwards.
  void Show(bool find_next = false, bool forward_direction = true);

  // Ends the current session. |selection_action| specifies what to do with the
  // selection on the page created by the find operation. |result_action|
  // specifies what to do with the contents of the Find box (after ending).
  void EndFindSession(find_in_page::SelectionAction selection_action,
                      find_in_page::ResultAction result_action);

  // Changes the WebContents that this FindBar is attached to. This
  // occurs when the user switches tabs in the Browser window. |contents| can be
  // NULL.
  void ChangeWebContents(content::WebContents* contents);

  // content::WebContentsObserver:
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void NavigationEntryCommitted(
      const content::LoadCommittedDetails& load_details) override;

  // find_in_page::FindResultObserver:
  void OnFindEmptyText(content::WebContents* web_contents) override;
  void OnFindResultAvailable(content::WebContents* web_contents) override;

  void SetText(std::u16string text);

  // Called when the find text is updated in response to a user action.
  void OnUserChangedFindText(std::u16string_view text);

  // Will be called only from `Browser`.
  void HandleActiveTabChanged(content::WebContents* new_contents);

  FindBar* find_bar() const { return find_bar_.get(); }

  // Updates the page action, which the find bar appears anchored to.
  void UpdatePageAction();

  // Called when the find bar visibility changes.
  void OnFindBarVisibilityChanged();

 private:
  // Sends an update to the find bar with the tab contents' current result. The
  // `web_contents()` must be non-NULL before this call. This handles
  // de-flickering in addition to just calling the update function.
  void UpdateFindBarForCurrentResult();

  // For Windows and Linux this function sets the prepopulate text for the
  // Find text box. The propopulate value is the last value the user searched
  // for in the current tab, or (if blank) the last value searched for in any
  // tab. Mac has a global value for search, so this function does nothing on
  // Mac.
  void MaybeSetPrepopulateText();

  // Gets the text that is selected in the current tab, or an empty string.
  std::u16string GetSelectedText();

  std::unique_ptr<FindBar> find_bar_;

  std::unique_ptr<FindBarPlatformHelper> find_bar_platform_helper_;

  // The last match count and ordinal we reported to the user. This is used
  // by UpdateFindBarForCurrentResult to avoid flickering.
  int last_reported_matchcount_ = 0;
  int last_reported_ordinal_ = 0;

  // If the user has changed the text in the find bar. Used to avoid
  // replacing user-entered text with selection.
  bool has_user_modified_text_ = false;

  // Manages the highlight on the page action.
  std::optional<page_actions::ScopedPageActionActivity>
      find_bar_page_action_activity_ = std::nullopt;

  base::ScopedObservation<find_in_page::FindTabHelper,
                          find_in_page::FindResultObserver>
      find_tab_observation_{this};

  // Tracks whether find bar was visible when the current main frame navigation
  // started. See crbug.com/469819146.
  // - If true, find bar should close when navigation commits (user was
  //   searching old page).
  // - If false, user opened find bar after the current navigation started and
  //   likely intends to search the new page, so find bar stays open.
  // - If nullopt, no navigation is in progress and find bar follows default
  //   close behavior.
  // Set in DidStartNavigation and cleared in NavigationEntryCommitted and
  // DidFinishNavigation.
  std::optional<bool> close_find_bar_on_navigation_commit_;

  raw_ptr<chrome::BrowserCommandController> browser_command_controller_ =
      nullptr;
};

#endif  // CHROME_BROWSER_UI_FIND_BAR_FIND_BAR_CONTROLLER_H_
