/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_a11y_DocAccessible_inl_h_
#define mozilla_a11y_DocAccessible_inl_h_

#include "DocAccessible.h"
#include "LocalAccessible-inl.h"
#include "nsAccessibilityService.h"
#include "NotificationController.h"
#include "States.h"
#include "mozilla/dom/DocumentInlines.h"

#ifdef A11Y_LOG
#  include "Logging.h"
#endif

namespace mozilla {
namespace a11y {

inline LocalAccessible* DocAccessible::AccessibleOrTrueContainer(
    nsINode* aNode, bool aNoContainerIfPruned) const {
  LocalAccessible* container =
      GetAccessibleOrContainer(aNode, aNoContainerIfPruned);
  return container;
}

inline bool DocAccessible::IsContentLoaded() const {
  // eDOMLoaded flag check is used for error pages as workaround to make this
  // method return correct result since error pages do not receive 'pageshow'
  // event and as consequence Document::IsShowing() returns false.
  return mDocumentNode && mDocumentNode->IsVisible() &&
         (mDocumentNode->IsShowing() || HasLoadState(eDOMLoaded));
}

inline bool DocAccessible::IsHidden() const { return mDocumentNode->Hidden(); }

inline void DocAccessible::FireDelayedEvent(AccEvent* aEvent) {
#ifdef A11Y_LOG
  if (logging::IsEnabled(logging::eDocLoad)) logging::DocLoadEventFired(aEvent);
#endif

  mNotificationController->QueueEvent(aEvent);
}

inline void DocAccessible::FireDelayedEvent(uint32_t aEventType,
                                            LocalAccessible* aTarget) {
  auto event = MakeRefPtr<AccEvent>(aEventType, aTarget);
  FireDelayedEvent(event);
}

template <class Class, class... Args>
inline void DocAccessible::HandleNotification(
    Class* aInstance, typename TNotification<Class, Args...>::Callback aMethod,
    Args*... aArgs) {
  if (mNotificationController) {
    mNotificationController->HandleNotification<Class, Args...>(
        aInstance, aMethod, aArgs...);
  }
}

inline void DocAccessible::UpdateText(nsIContent* aTextNode) {
  NS_ASSERTION(mNotificationController, "The document was shut down!");
  MOZ_ASSERT(aTextNode->IsText());

  // Ignore the notification if initial tree construction hasn't been done yet.
  if (!mNotificationController || !HasLoadState(eTreeConstructed)) {
    return;
  }
  nsINode* parent = aTextNode->GetParent();
  if (parent && parent->IsGeneratedContentContainerForMarker()) {
    // This is the text of a bullet. Accessibility handles bullets differently
    // to normal text. Instead of creating a TextLeafAccessible and caching the
    // text on it, HTMLListBulletAccessible retrieves the text directly from
    // layout, so any update is reflected immediately. That means we need to
    // invalidate cached HyperText offsets immediately.
    LocalAccessible* bullet = mDoc->GetAccessible(parent);
    if (!bullet) {
      return;
    }
    mDoc->QueueCacheUpdate(bullet, CacheDomain::Text);
    HyperTextAccessible* container =
        bullet->LocalParent() ? bullet->LocalParent()->AsHyperText() : nullptr;
    if (container) {
      int32_t offset =
          container->GetChildOffset(bullet, /* aInvalidateAfter */ true);
      nsAutoString text;
      bullet->AppendTextTo(text);
      auto event =
          MakeRefPtr<AccTextChangeEvent>(container, offset, text, true);
      mDoc->FireDelayedEvent(event);
    }
    return;
  }
  mNotificationController->ScheduleTextUpdate(aTextNode);
}

inline void DocAccessible::NotifyOfLoad(uint32_t aLoadEventType) {
  mLoadState |= eDOMLoaded;
  mLoadEventType = aLoadEventType;

  // If the document is loaded completely then network activity was presumingly
  // caused by file loading. Fire busy state change event.
  if (HasLoadState(eCompletelyLoaded) && IsLoadEventTarget()) {
    auto stateEvent =
        MakeRefPtr<AccStateChangeEvent>(this, states::BUSY, false);
    FireDelayedEvent(stateEvent);
  }
}

inline void DocAccessible::MaybeNotifyOfValueChange(
    LocalAccessible* aAccessible) {
  if (aAccessible->IsCombobox() || aAccessible->IsPassword() ||
      aAccessible->Role() == roles::ENTRY ||
      aAccessible->Role() == roles::SPINBUTTON) {
    FireDelayedEvent(nsIAccessibleEvent::EVENT_TEXT_VALUE_CHANGE, aAccessible);
  }
}

inline LocalAccessible* DocAccessible::GetAccessibleEvenIfNotInMapOrContainer(
    nsINode* aNode) const {
  LocalAccessible* acc = GetAccessibleEvenIfNotInMap(aNode);
  return acc ? acc : GetContainerAccessible(aNode);
}

inline void DocAccessible::CreateSubtree(LocalAccessible* aChild) {
  // If a focused node has been shown then it could mean its frame was recreated
  // while the node stays focused and we need to fire focus event on
  // the accessible we just created. If the queue contains a focus event for
  // this node already then it will be suppressed by this one.
  LocalAccessible* focusedAcc = nullptr;
  CacheChildrenInSubtree(aChild, &focusedAcc);

#ifdef A11Y_LOG
  if (logging::IsEnabled(logging::eVerbose)) {
    logging::Tree("TREE", "Created subtree", aChild);
  }
#endif

  // XXX: do we really want to send focus to focused DOM node not taking into
  // account active item?
  if (focusedAcc) {
    FocusMgr()->DispatchFocusEvent(this, focusedAcc);
    SelectionMgr()->SetControlSelectionListener(
        focusedAcc->GetNode()->AsElement());
  }
}

inline DocAccessible::AttrRelProviders* DocAccessible::GetRelProviders(
    dom::Element* aElement, nsAtom* aID) const {
  DependentIDsHashtable* hash = mDependentIDsHashes.Get(
      aElement->GetUncomposedDocOrConnectedShadowRoot());
  if (hash) {
    return hash->Get(aID);
  }
  return nullptr;
}

inline DocAccessible::AttrRelProviders* DocAccessible::GetOrCreateRelProviders(
    dom::Element* aElement, nsAtom* aID) {
  // TODO (bug 1983819): need to update when reference targets change
  dom::DocumentOrShadowRoot* docOrShadowRoot =
      aElement->GetUncomposedDocOrConnectedShadowRoot();
  DependentIDsHashtable* hash =
      mDependentIDsHashes.GetOrInsertNew(docOrShadowRoot);

  return hash->GetOrInsertNew(aID);
}

inline void DocAccessible::RemoveRelProvidersIfEmpty(dom::Element* aElement,
                                                     nsAtom* aID) {
  dom::DocumentOrShadowRoot* docOrShadowRoot =
      aElement->GetUncomposedDocOrConnectedShadowRoot();
  DependentIDsHashtable* hash = mDependentIDsHashes.Get(docOrShadowRoot);
  if (hash) {
    AttrRelProviders* providers = hash->Get(aID);
    if (providers && providers->Length() == 0) {
      hash->Remove(aID);
      if (mDependentIDsHashes.IsEmpty()) {
        mDependentIDsHashes.Remove(docOrShadowRoot);
      }
    }
  }
}

}  // namespace a11y
}  // namespace mozilla

#endif
