import { storiesOf } from "@storybook/react";
import React from "react";
import { AttachmentAudio } from "..";
import { Attachment } from "../../../util/attrs";
import { MockAttachmentService } from "../../Shared/__stories__/MockAttachmentService";
import { AttachmentDownloadUrl } from "../../../AttachmentService";
import { AttachmentPreview, AttachmentUpload } from "../../../..";

const ATTACHMENT_MP3: Attachment = {
  hidePreview: false,
  id: "1",
  name: "Elephant’s Dream – The Wires",
  type: "audio/mpeg"
};

storiesOf(`Attachments/${AttachmentAudio.name}`, module)
  .add("Default", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "available", url: "https://storage.googleapis.com/media-session/elephants-dream/the-wires.mp3" },
          preview: { type: "unavailable" },
          upload: { type: "complete" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Active", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "available", url: "https://storage.googleapis.com/media-session/elephants-dream/the-wires.mp3" },
          preview: { type: "unavailable" },
          upload: { type: "complete" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
      style="active"
    />
  ))
  .add("Selected", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "available", url: "https://storage.googleapis.com/media-session/elephants-dream/the-wires.mp3" },
          preview: { type: "unavailable" },
          upload: { type: "complete" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
      style="selected"
    />
  ))
  .add("Not Editable", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "available", url: "https://storage.googleapis.com/media-session/elephants-dream/the-wires.mp3" },
          preview: { type: "unavailable" },
          upload: { type: "complete" }
        })
      }
      editable={false}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Download pending", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "pending" },
          preview: { type: "unavailable" },
          upload: { type: "complete" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Download unavailable", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "unavailable" },
          preview: { type: "unavailable" },
          upload: { type: "complete" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Uploading", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "pending" },
          preview: { type: "unavailable" },
          upload: { bytesUploaded: 50000, bytesTotal: 200000, type: "uploading" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Uploading pending", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "pending" },
          preview: { type: "unavailable" },
          upload: { type: "queued" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Uploading unknown", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "pending" },
          preview: { type: "unavailable" },
          upload: { type: "unknown" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Uploading failed", () => (
    <AttachmentAudio
      attachment={ATTACHMENT_MP3}
      attachmentService={
        new MockAttachmentService({
          downloadUrl: { type: "pending" },
          preview: { type: "unavailable" },
          upload: { type: "failed" }
        })
      }
      editable={true}
      onShowPreview={() => {}}
      onRemove={() => {}}
    />
  ))
  .add("Stateful", () => {
    interface State {
      downloadUrl: AttachmentDownloadUrl;
      preview: AttachmentPreview;
      upload: AttachmentUpload;
    }

    class Example extends React.PureComponent<{}, State> {
      public state: State = {
        downloadUrl: { type: "pending" },
        preview: { type: "unavailable" },
        upload: { bytesUploaded: 50000, bytesTotal: 200000, type: "uploading" }
      };

      public componentDidMount() {
        if (window !== undefined) {
          window.setTimeout(() => {
            this.setState({
              downloadUrl: { type: "pending" },
              upload: { type: "complete" }
            });
          }, 2000);

          window.setTimeout(() => {
            this.setState({
              downloadUrl: {
                type: "available",
                url: "https://storage.googleapis.com/media-session/elephants-dream/the-wires.mp3"
              }
            });
          }, 5000);
        }
      }

      public render() {
        const { downloadUrl, preview, upload } = this.state;

        return (
          <AttachmentAudio
            attachment={ATTACHMENT_MP3}
            attachmentService={
              new MockAttachmentService({
                downloadUrl,
                preview,
                upload
              })
            }
            editable={true}
            onShowPreview={() => {}}
            onRemove={() => {}}
          />
        );
      }
    }

    return <Example />;
  });
