import { css } from '@emotion/css';
import { useState } from 'react';

import { CoreApp, type GrafanaTheme2 } from '@grafana/data';
import { Components, selectors } from '@grafana/e2e-selectors';
import { Trans, t } from '@grafana/i18n';
import { config } from '@grafana/runtime';
import { useFlagQueryHistoryRecentQueriesUI } from '@grafana/runtime/internal';
import { ToolbarButton, useTheme2 } from '@grafana/ui';
import { contextSrv } from 'app/core/services/context_srv';
import { AccessControlAction } from 'app/types/accessControl';

import { useQueriesDrawerContext } from './QueriesDrawer/QueriesDrawerContext';
import { useQueryLibraryContext } from './QueryLibrary/QueryLibraryContext';
import { type OnSelectQueriesType, type OnSelectQueryType } from './QueryLibrary/types';
import { RecentQueriesModal } from './RecentQueries/RecentQueriesModal';

type Props = {
  addQueryRowButtonDisabled?: boolean;
  addQueryRowButtonHidden?: boolean;
  queryInspectorButtonActive?: boolean;

  onClickAddQueryRowButton: () => void;
  onClickQueryInspectorButton: () => void;
  onSelectQueryFromLibrary: OnSelectQueryType;
  onSelectQueriesFromLibrary: OnSelectQueriesType;
  onReplaceQueriesFromLibrary: OnSelectQueriesType;
};

const getStyles = (theme: GrafanaTheme2) => {
  return {
    containerMargin: css({
      display: 'flex',
      flexWrap: 'wrap',
      gap: theme.spacing(1),
      marginTop: theme.spacing(2),
    }),
  };
};

export function SecondaryActions({
  addQueryRowButtonDisabled,
  addQueryRowButtonHidden,
  onClickAddQueryRowButton,
  onClickQueryInspectorButton,
  onSelectQueryFromLibrary,
  onSelectQueriesFromLibrary,
  onReplaceQueriesFromLibrary,
  queryInspectorButtonActive,
}: Props) {
  const theme = useTheme2();
  const styles = getStyles(theme);
  const { queryLibraryEnabled, openDrawer: openQueryLibraryDrawer } = useQueryLibraryContext();
  const { drawerOpened, setDrawerOpened } = useQueriesDrawerContext();
  const recentQueriesUI = useFlagQueryHistoryRecentQueriesUI();
  const [recentQueriesOpen, setRecentQueriesOpen] = useState(false);
  const canReadQueries = config.featureToggles.savedQueriesRBAC
    ? contextSrv.hasPermission(AccessControlAction.QueriesRead)
    : contextSrv.isSignedIn;

  return (
    <div className={styles.containerMargin}>
      {!addQueryRowButtonHidden && (
        <>
          <ToolbarButton
            variant="canvas"
            aria-label={t('explore.secondary-actions.query-add-button-aria-label', 'Add query')}
            onClick={onClickAddQueryRowButton}
            disabled={addQueryRowButtonDisabled}
            icon="plus"
          >
            <Trans i18nKey="explore.secondary-actions.query-add-button">Add query</Trans>
          </ToolbarButton>
          {queryLibraryEnabled && canReadQueries && (
            <ToolbarButton
              data-testid={selectors.pages.Explore.General.addFromQueryLibrary}
              aria-label={t('explore.secondary-actions.add-from-query-library', 'Add from saved queries')}
              variant="canvas"
              onClick={() =>
                openQueryLibraryDrawer({
                  onSelectQuery: onSelectQueryFromLibrary,
                  onSelectQueries: onSelectQueriesFromLibrary,
                  options: { context: CoreApp.Explore },
                })
              }
              icon={recentQueriesUI ? 'book-open' : 'plus'}
              disabled={addQueryRowButtonDisabled}
            >
              <Trans i18nKey="explore.secondary-actions.add-from-query-library">Add from saved queries</Trans>
            </ToolbarButton>
          )}

          {!queryLibraryEnabled && recentQueriesUI && (
            <>
              <ToolbarButton
                variant="canvas"
                aria-label={t('explore.secondary-actions.recent-queries-button-aria-label', 'Recent queries')}
                onClick={() => setRecentQueriesOpen(true)}
                icon="history"
              >
                <Trans i18nKey="explore.secondary-actions.recent-queries-button">Recent queries</Trans>
              </ToolbarButton>
              <RecentQueriesModal
                isOpen={recentQueriesOpen}
                onClose={() => setRecentQueriesOpen(false)}
                onSelectQuery={onReplaceQueriesFromLibrary}
              />
            </>
          )}
        </>
      )}
      {/* Keep the separate Query history entry point available during the deprecation period,
          even when the recentQueriesUI flag is enabled. */}
      <ToolbarButton
        key="query-history"
        variant={drawerOpened ? 'active' : 'canvas'}
        aria-label={t('explore.secondary-actions.query-history-button-aria-label', 'Query history')}
        onClick={() => setDrawerOpened(!drawerOpened)}
        data-testid={Components.QueryTab.queryHistoryButton}
        icon="history"
      >
        <Trans i18nKey="explore.secondary-actions.query-history-button">Query history</Trans>
      </ToolbarButton>
      <ToolbarButton
        variant={queryInspectorButtonActive ? 'active' : 'canvas'}
        aria-label={t('explore.secondary-actions.query-inspector-button-aria-label', 'Query inspector')}
        onClick={onClickQueryInspectorButton}
        icon="info-circle"
      >
        <Trans i18nKey="explore.secondary-actions.query-inspector-button">Query inspector</Trans>
      </ToolbarButton>
    </div>
  );
}
