// 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 type { PropsType } from './DialogUpdate.dom.js';
import { DialogUpdate } from './DialogUpdate.dom.js';
import { DialogType } from '../types/Dialogs.std.js';
import { WidthBreakpoint } from './_util.std.js';
import { SECOND } from '../util/durations/index.std.js';
import { FakeLeftPaneContainer } from '../test-helpers/FakeLeftPaneContainer.dom.js';

const { i18n } = window.SignalContext;

const defaultProps = {
  containerWidthBreakpoint: WidthBreakpoint.Wide,
  dismissDialog: action('dismiss-dialog'),
  downloadSize: 116504357,
  downloadedSize: 61003110,
  i18n,
  showEventsCount: 0,
  snoozeUpdate: action('snooze-update'),
  startUpdate: action('start-update'),
  version: 'v7.7.7',
};

export default {
  title: 'Components/DialogUpdate',
  argTypes: {},
  args: {},
} satisfies Meta<PropsType>;

export function KnobsPlayground(): React.JSX.Element {
  const containerWidthBreakpoint = WidthBreakpoint.Wide;
  const dialogType = DialogType.AutoUpdate;

  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={containerWidthBreakpoint}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={containerWidthBreakpoint}
        dialogType={dialogType}
        currentVersion="5.24.0"
      />
    </FakeLeftPaneContainer>
  );
}

export function UpdateWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        dialogType={DialogType.AutoUpdate}
        currentVersion="5.24.0"
      />
    </FakeLeftPaneContainer>
  );
}

export function DownloadedWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        dialogType={DialogType.DownloadedUpdate}
        currentVersion="5.24.0"
      />
    </FakeLeftPaneContainer>
  );
}

export function DownloadReadyWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0"
        dialogType={DialogType.DownloadReady}
        downloadSize={30123456}
      />
    </FakeLeftPaneContainer>
  );
}

export function FullDownloadReadyWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0"
        dialogType={DialogType.FullDownloadReady}
        downloadSize={300123456}
      />
    </FakeLeftPaneContainer>
  );
}

export function DownloadingWide(): React.JSX.Element {
  const [downloadedSize, setDownloadedSize] = React.useState(0);

  const { downloadSize } = defaultProps;

  React.useEffect(() => {
    const interval = setInterval(() => {
      setDownloadedSize(x => {
        const newValue = x + 0.25 * downloadSize;
        if (newValue > downloadSize) {
          return 0;
        }
        return newValue;
      });
    }, SECOND);

    return () => clearInterval(interval);
  }, [downloadSize]);

  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        downloadedSize={downloadedSize}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0"
        dialogType={DialogType.Downloading}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0"
        dialogType={DialogType.Cannot_Update}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateBetaWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0-beta.1"
        dialogType={DialogType.Cannot_Update}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateRequireManualWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0"
        dialogType={DialogType.Cannot_Update_Require_Manual}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateRequireManualBetaWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0-beta.1"
        dialogType={DialogType.Cannot_Update_Require_Manual}
      />
    </FakeLeftPaneContainer>
  );
}

export function MacOSReadOnlyWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0"
        dialogType={DialogType.MacOS_Read_Only}
      />
    </FakeLeftPaneContainer>
  );
}

export function UnsupportedOSWide(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Wide}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Wide}
        currentVersion="5.24.0"
        dialogType={DialogType.UnsupportedOS}
      />
    </FakeLeftPaneContainer>
  );
}

export function UpdateNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        dialogType={DialogType.AutoUpdate}
        currentVersion="5.24.0"
      />
    </FakeLeftPaneContainer>
  );
}

export function DownloadedNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        dialogType={DialogType.DownloadedUpdate}
        currentVersion="5.24.0"
      />
    </FakeLeftPaneContainer>
  );
}

export function DownloadReadyNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0"
        dialogType={DialogType.DownloadReady}
        downloadSize={30123456}
      />
    </FakeLeftPaneContainer>
  );
}

export function FullDownloadReadyNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0"
        dialogType={DialogType.FullDownloadReady}
        downloadSize={300123456}
      />
    </FakeLeftPaneContainer>
  );
}

export function DownloadingNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0"
        dialogType={DialogType.Downloading}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0"
        dialogType={DialogType.Cannot_Update}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateBetaNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0-beta.1"
        dialogType={DialogType.Cannot_Update}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateRequireManualNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0"
        dialogType={DialogType.Cannot_Update_Require_Manual}
      />
    </FakeLeftPaneContainer>
  );
}

export function CannotUpdateRequireManualBetaNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0-beta.1"
        dialogType={DialogType.Cannot_Update_Require_Manual}
      />
    </FakeLeftPaneContainer>
  );
}

export function MacOSReadOnlyNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0"
        dialogType={DialogType.MacOS_Read_Only}
      />
    </FakeLeftPaneContainer>
  );
}

export function UnsupportedOSNarrow(): React.JSX.Element {
  return (
    <FakeLeftPaneContainer containerWidthBreakpoint={WidthBreakpoint.Narrow}>
      <DialogUpdate
        {...defaultProps}
        containerWidthBreakpoint={WidthBreakpoint.Narrow}
        currentVersion="5.24.0"
        dialogType={DialogType.UnsupportedOS}
      />
    </FakeLeftPaneContainer>
  );
}
