// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import * as React from 'react';
import { action } from '@storybook/addon-actions';
import type { Meta } from '@storybook/react';
import { pngUrl } from '../../storybook/Fixtures.std.ts';
import type { Props } from './Image.dom.tsx';
import { CurveType, Image } from './Image.dom.tsx';
import { IMAGE_PNG } from '../../types/MIME.std.ts';
import type { ThemeType } from '../../types/Util.std.ts';
import { StorybookThemeContext } from '../../../.storybook/StorybookThemeContext.std.ts';

import { fakeAttachment } from '../../test-helpers/fakeAttachment.std.ts';

const { i18n } = window.SignalContext;

export default {
  title: 'Components/Conversation/Image',
} satisfies Meta<Props>;

const createProps = (overrideProps: Partial<Props> = {}): Props => ({
  alt: overrideProps.alt || '',
  attachment:
    overrideProps.attachment ||
    fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      url: pngUrl,
    }),
  blurHash: overrideProps.blurHash || '',
  bottomOverlay: overrideProps.bottomOverlay || false,
  closeButton: overrideProps.closeButton || false,
  curveBottomLeft: overrideProps.curveBottomLeft || CurveType.None,
  curveBottomRight: overrideProps.curveBottomRight || CurveType.None,
  curveTopLeft: overrideProps.curveTopLeft || CurveType.None,
  curveTopRight: overrideProps.curveTopRight || CurveType.None,
  darkOverlay: overrideProps.darkOverlay || false,
  height: overrideProps.height || 200,
  i18n,
  noBackground: overrideProps.noBackground || false,
  noBorder: overrideProps.noBorder || false,
  showVisualAttachment: action('showVisualAttachment'),
  startDownload: action('startDownload'),
  cancelDownload: action('cancelDownload'),
  onClickClose: action('onClickClose'),
  onError: action('onError'),
  overlayText: overrideProps.overlayText || '',
  playIconOverlay: overrideProps.playIconOverlay || false,
  tabIndex: overrideProps.tabIndex || 0,
  theme: overrideProps.theme || ('light' as ThemeType),
  url: 'url' in overrideProps ? overrideProps.url || '' : pngUrl,
  width: overrideProps.width || 300,
});

export function UrlWithHeightWidth(): React.JSX.Element {
  const props = createProps();

  return <Image {...props} />;
}

export function Caption(): React.JSX.Element {
  const defaultProps = createProps();
  const props = {
    ...defaultProps,
    attachment: {
      ...defaultProps.attachment,
      caption: '<Saxophone Pun>',
    },
  };

  return <Image {...props} />;
}

export function PlayIcon(): React.JSX.Element {
  const props = createProps({
    playIconOverlay: true,
  });

  return <Image {...props} />;
}

export function CloseButton(): React.JSX.Element {
  const props = createProps({
    closeButton: true,
  });

  return <Image {...props} />;
}

export function NoBorderOrBackground(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      url: pngUrl,
    }),
    noBackground: true,
    noBorder: true,
    url: pngUrl,
  });

  return (
    <div style={{ backgroundColor: '#999' }}>
      <Image {...props} />
    </div>
  );
}

export function NotDownloadedNotIncrementalNotPending(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      size: 5300000,
    }),
    url: undefined,
    blurHash: 'thisisafakeblurhashthatwasmadeup',
  });

  return <Image {...props} />;
}

export function PendingWDownloadQueuedNotIncremental(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      pending: true,
      size: 5300000,
    }),
    url: undefined,
    blurHash: 'thisisafakeblurhashthatwasmadeup',
  });

  return <Image {...props} />;
}

export function PendingWDownloadProgress(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      pending: true,
      size: 5300000,
      totalDownloaded: 1230000,
    }),
    blurHash: 'thisisafakeblurhashthatwasmadeup',
    url: undefined,
  });

  return <Image {...props} />;
}

export function NotPendingWDownloadProgress(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      size: 5300000,
      totalDownloaded: 1230000,
    }),
    blurHash: 'thisisafakeblurhashthatwasmadeup',
    url: undefined,
  });

  return <Image {...props} />;
}

export function PendingIncrementalNoProgress(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      pending: true,
      size: 5300000,
      incrementalMac: 'something',
      chunkSize: 100,
    }),
    playIconOverlay: true,
    blurHash: 'thisisafakeblurhashthatwasmadeup',
    url: undefined,
  });

  return <Image {...props} />;
}

export function PendingIncrementalDownloadProgress(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      pending: true,
      size: 5300000,
      totalDownloaded: 1230000,
      incrementalMac: 'something',
      chunkSize: 100,
    }),
    playIconOverlay: true,
    blurHash: 'thisisafakeblurhashthatwasmadeup',
    url: undefined,
  });

  return <Image {...props} />;
}

export function NotPendingIncrementalNoProgress(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      size: 5300000,
      incrementalMac: 'something',
      chunkSize: 100,
    }),
    playIconOverlay: true,
    blurHash: 'thisisafakeblurhashthatwasmadeup',
    url: undefined,
  });

  return <Image {...props} />;
}

export function NotPendingIncrementalWProgress(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
      path: undefined,
      size: 5300000,
      totalDownloaded: 1230000,
      incrementalMac: 'something',
      chunkSize: 100,
    }),
    playIconOverlay: true,
    blurHash: 'thisisafakeblurhashthatwasmadeup',
    url: undefined,
  });

  return <Image {...props} />;
}

export function CurvedCorners(): React.JSX.Element {
  const props = createProps({
    curveBottomLeft: CurveType.Normal,
    curveBottomRight: CurveType.Normal,
    curveTopLeft: CurveType.Normal,
    curveTopRight: CurveType.Normal,
  });

  return <Image {...props} />;
}

export function SmallCurveTopLeft(): React.JSX.Element {
  const props = createProps({
    curveTopLeft: CurveType.Small,
  });

  return <Image {...props} />;
}

export function SoftCorners(): React.JSX.Element {
  const props = createProps({
    curveBottomLeft: CurveType.Tiny,
    curveBottomRight: CurveType.Tiny,
    curveTopLeft: CurveType.Tiny,
    curveTopRight: CurveType.Tiny,
  });

  return <Image {...props} />;
}

export function BottomOverlay(): React.JSX.Element {
  const props = createProps({
    bottomOverlay: true,
  });

  return <Image {...props} />;
}

export function FullOverlayWithText(): React.JSX.Element {
  const props = createProps({
    darkOverlay: true,
    overlayText: 'Honk!',
  });

  return <Image {...props} />;
}

export function Blurhash(): React.JSX.Element {
  const props = createProps({
    attachment: fakeAttachment({
      contentType: IMAGE_PNG,
      fileName: 'sax.png',
    }),
    blurHash: 'thisisafakeblurhashthatwasmadeup',
    url: undefined,
  });

  return <Image {...props} />;
}

function UndefinedBlurHashWrapper() {
  const theme = React.useContext(StorybookThemeContext);
  const props = createProps({
    blurHash: undefined,
    theme,
    url: undefined,
  });

  return <Image {...props} />;
}

export function UndefinedBlurHash(): React.JSX.Element {
  return <UndefinedBlurHashWrapper />;
}

export function MissingImage(): React.JSX.Element {
  const props = createProps({
    attachment: undefined,
    url: 'random',
  });

  return <Image {...props} />;
}
