import Centered from './Centered'
import DropZone from './DropZone'
import ProgressBar from './ProgressBar'
import React from 'react'
import Spinner from './Spinner'
import Tempalink from './Tempalink'
import UploadActions from '../actions/UploadActions'
import UploadStore from '../stores/UploadStore'
import socket from 'filepizza-socket'

export default class UploadPage extends React.Component {

  constructor() {
    this.state = UploadStore.getState()

    this._onChange = () => {
      this.setState(UploadStore.getState())
    }

    this._onDownload = (peerID) => {
      UploadActions.sendToDownloader(peerID)
    }
  }

  componentDidMount() {
    UploadStore.listen(this._onChange)
    socket.on('download', this._onDownload)
  }

  componentDidUnmount() {
    UploadStore.unlisten(this._onChange)
    socket.removeListener('download', this._onDownload)
  }

  uploadFile(file) {
    UploadActions.uploadFile(file)
  }

  render() {
    switch (this.state.status) {
      case 'ready':

        return <DropZone onDrop={this.uploadFile.bind(this)}>
          <Centered ver>

            <Spinner dir="up" />

            <h1>FilePizza</h1>
            <p>Your files, delivered.</p>
            <p>Drag the file into this window to get started.</p>
            <p>Or
            <label className="myLabel">
              <input type="file" required/>
              <span>select a file</span>
            </label>
            .
            </p>
          </Centered>
        </DropZone>

      case 'processing':
        return <Centered ver>

          <Spinner dir="up" animated />

          <h1>FilePizza</h1>
          <p>Processing...</p>

        </Centered>

      case 'uploading':
        var keys = Object.keys(this.state.peerProgress)
        keys.reverse()
        return <Centered ver>

          <h1>FilePizza</h1>
          <Spinner dir="up" animated {...this.state.file} />

          <p>Send someone this link to download.</p>
          <p>This link will work as long as this page is open.</p>
          <Tempalink token={this.state.token} />

          {keys.length > 0 ? <p>Download Progress</p> : null}
          <div className="data">
            {keys.map((key) => {
              return <ProgressBar small value={this.state.peerProgress[key]} />
            })}
          </div>

        </Centered>
    }
  }

}

@@ -927,16 +927,163 @@
 e)%0A  %7D%0A%0A
+  handleSelectedFile(event) %7B%0A    let files = event.target.files;%0A    if (files.length %3E 0) %7B%0A      UploadActions.uploadFile(files%5B0%5D);%0A    %7D%0A  %7D%0A%0A
   render
@@ -1492,16 +1492,51 @@
 e=%22file%22
+ onChange=%7Bthis.handleSelectedFile%7D
  require

Handle onChange event and upload file.