import { UniDriver } from '@wix/wix-ui-test-utils/unidriver';
import { baseUniDriverFactory } from '../../unidriver';
import {
  EmptyStateUniDriver,
  TextButtonUniDriver,
  BoxUniDriver,
  TextUniDriver,
} from '@wix/design-system/dist/testkit/unidriver';

export default (base: UniDriver, body: UniDriver) => {
  const errorState = () =>
    EmptyStateUniDriver(base.$(`[data-hook="error-card-state"]`), body);

  const emptyState = () =>
    EmptyStateUniDriver(base.$(`[data-hook="empty-state"]`), body);

  const tryAgainButton = () =>
    TextButtonUniDriver(
      base.$(`[data-hook="error-card-state-action-button"]`),
      body,
    );

  const getCustomFieldLabels = () =>
    base.$$(`[data-hook^="custom-field-label"]`).map(async (label) => {
      const text = await label.text();
      return text.substring(0, text.length - 1);
    });

  const getCustomFieldValues = () =>
    base.$$(`[data-hook^="custom-field-value"]`).map((value) => value.text());

  const getSectionTitles = () =>
    base
      .$$('[data-hook^="custom-field-section-title"]')
      .map((value) => value.text());

  const getCustomFieldLabelsForNamespace = (namespace: string) =>
    base
      .$$(`[data-hook^="custom-field-label-${namespace}"]`)
      .map(async (label) => {
        const text = await label.text();
        return text.substring(0, text.length - 1);
      });

  const getCustomFieldValuesForNamespace = (namespace: string) =>
    base
      .$$(`[data-hook^="custom-field-value-${namespace}"]`)
      .map((value) => value.text());

  const getCustomFieldLabel = async (namespace: string, fieldId: string) => {
    const wholeLabel = await base
      .$(`[data-hook="custom-field-label-${namespace}-${fieldId}"]`)
      .text();

    return wholeLabel.substring(0, wholeLabel.length - 1);
  };

  const getCustomFieldValue = (namespace: string, fieldId: string) =>
    base.$(`[data-hook="custom-field-value-${namespace}-${fieldId}"]`).text();

  const sectionTitleForNamespaceExists = (namespace: string) =>
    base.$(`[data-hook="custom-field-section-title-${namespace}"]`).exists();

  const getSectionTitleForNamespace = (namespace: string) =>
    base.$(`[data-hook="custom-field-section-title-${namespace}"]`).text();

  const getFileCard = (mediaId: string) => {
    return BoxUniDriver(base.$(`[data-hook="file-card-${mediaId}"]`), body);
  };
  const getFileThumbnail = ({
    mediaId,
    mediaType,
  }: {
    mediaId: string;
    mediaType: string;
  }) => {
    return BoxUniDriver(
      base.$(`[data-hook="file-thumbnail-${mediaType}-${mediaId}"]`),
      body,
    );
  };

  const getFileDownloadButton = (mediaId: string) => {
    return BoxUniDriver(
      base.$(`[data-hook="download-button-${mediaId}"]`),
      body,
    );
  };

  const getFileDetails = (mediaId: string) => {
    return BoxUniDriver(base.$(`[data-hook="file-details-${mediaId}"]`), body);
  };

  const getFileDetailsSize = (mediaId: string) => {
    return TextUniDriver(
      base.$(`[data-hook="file-details-size-${mediaId}"]`),
      body,
    );
  };

  const getFileDetailsName = (mediaId: string) => {
    return TextUniDriver(
      base.$(`[data-hook="file-details-name-${mediaId}"]`),
      body,
    );
  };

  const getTitleElement = () => base.$(`[data-hook="title"]`);

  return {
    getFileCard,
    getFileThumbnail,
    getFileDownloadButton,
    getFileDetails,
    getFileDetailsName,
    getFileDetailsSize,
    /** Returns title text */
    getTitle: () => getTitleElement().text(),
    getTitleElement,
    /** Indicates whether the widget loading state shows */
    loadingStateExists: () =>
      base.$(`[data-hook="widget-inline-loading"]`).exists(),
    fieldsContentExists: () => base.$(`[data-hook="fields-content"]`).exists(),
    /** Returns the error state title */
    getErrorTitle: () => errorState().getTitleText(),
    /** Returns the error state subtitle */
    getErrorSubtitle: () => errorState().getSubtitleText(),
    /** Returns the empty state title */
    getEmptyTitle: () => emptyState().getTitleText(),
    /** Returns the empty state subtitle */
    getEmptySubtitle: () => base.$(`[data-hook="empty-state-subtitle"]`).text(),
    /** Clicks on try again button when error state visible */
    clickTryAgain: () => tryAgainButton().click(),
    /** Gets try again button text */
    getTryAgainContent: () => tryAgainButton().getButtonTextContent(),
    /** Returns all custom field labels */
    getCustomFieldLabels,
    /** Returns custom field labels for specific namespace */
    getCustomFieldLabelsForNamespace,
    /** Returns specific custom field label by namespace and field id */
    getCustomFieldLabel,
    /** Returns the custom field label by index */
    getCustomFieldLabelByIndex: async (index: number) =>
      (await getCustomFieldLabels())[index],
    /** Returns all custom field values */
    getCustomFieldValues,
    /** Returns custom field values for specific namespace */
    getCustomFieldValuesForNamespace,
    /** Returns specific custom field value by namespace and field id */
    getCustomFieldValue,
    /** Returns the custom field value by index */
    getCustomFieldValueByIndex: async (index: number) =>
      (await getCustomFieldValues())[index],
    /** Return all section titles */
    getSectionTitles,
    /** Return specific section title for namespace */
    getSectionTitleForNamespace,
    /** Return the section title by index */
    getSectionTitleByIndex: async (index: number) =>
      (await getSectionTitles())[index],
    sectionTitleForNamespaceExists,
    ...baseUniDriverFactory(base),
  };
};
