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

#ifndef COMPONENTS_USER_EDUCATION_WEBUI_HELP_BUBBLE_WEBUI_H_
#define COMPONENTS_USER_EDUCATION_WEBUI_HELP_BUBBLE_WEBUI_H_

#include <memory>

#include "components/user_education/common/help_bubble/help_bubble.h"
#include "components/user_education/common/help_bubble/help_bubble_factory.h"
#include "components/user_education/common/help_bubble/help_bubble_params.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/safe_castable.h"

namespace content {
class WebContents;
}

namespace user_education {

class HelpBubbleHandlerBase;

// This is a thin wrapper around HelpBubbleHandler that implements the
// HelpBubble interaface.
class HelpBubbleWebUI : public HelpBubble {
 public:
  ~HelpBubbleWebUI() override;

  // Retrieves the `WebContents` that hosts this help bubble, if any, or null if
  // none. Will return null if the bubble is closed.
  content::WebContents* GetWebContents();

  // HelpBubble:
  bool ToggleFocusForAccessibility() override;
  gfx::Rect GetBoundsInScreen() const override;
  ui::ElementContext GetContext() const override;

  DECLARE_SAFE_CAST_TARGET()

 private:
  friend class HelpBubbleHandlerBase;

  HelpBubbleWebUI(HelpBubbleHandlerBase* handler,
                  ui::ElementIdentifier anchor_id,
                  const std::string& secondary_id);

  // HelpBubble:
  bool Close(CloseReason reason) override;

  const raw_ptr<HelpBubbleHandlerBase> handler_;
  const ui::ElementIdentifier anchor_id_;
  const std::string secondary_id_;
};

// This factory uses HelpBubbleHandler to show a help bubble and create a
// HelpBubbleWebUI wrapper.
class HelpBubbleFactoryWebUI : public HelpBubbleFactory {
 public:
  HelpBubbleFactoryWebUI();
  ~HelpBubbleFactoryWebUI() override;

  DECLARE_SAFE_CAST_TARGET()

  // HelpBubbleFactory:
  std::unique_ptr<HelpBubble> CreateBubble(ui::TrackedElement* element,
                                           HelpBubbleParams params) override;
  bool CanBuildBubbleForTrackedElement(
      const ui::TrackedElement* element) const override;
};

}  // namespace user_education

#endif  // COMPONENTS_USER_EDUCATION_WEBUI_HELP_BUBBLE_WEBUI_H_
