import React, { Component } from 'react';
import { Icon, Segment, Dropdown } from 'semantic-ui-react'

import PropTypes from 'prop-types';
import _ from 'lodash';

import Dropzone from 'react-dropzone';


export default class Uploads extends Component {
    //camera

    static propTypes = {
        appState: PropTypes.object.isRequired,

        serverSubmit: PropTypes.func.isRequired,

        options: PropTypes.object,
    };

    constructor(props) {
 
         super(props);
         this.state = {
            uploading: false,
         };

         this.devMode = props.appState.devMode;

         this.fileInputRef = React.createRef(); 
     }


      componentWillUnmount() {
        console.log('components/Uploads  componentWillUnmount');
    }

    
    txt = (text) => {
      //handle text translations from config
      const { props } = this;
      if (text in props.appState.config.text) return props.appState.config.text[text];
  
      return text;
    }


    onDrop = (files) => {

       const {props} = this;

       var params = new FormData(); 
                
         params.append('uploadMode', 'VideoWhisper' );

        var filelist = [];
        files.forEach(file => {
          params.append(file.name,  file );
          filelist.push(file.name);
        });

        params.append('files[]', filelist);
        params.append('filesCount', filelist.length );

        props.serverSubmit(params);

        console.log('components/Uploads onDrop', filelist);

        this.setState({uploading: true});

      }
      

    render() {
      //  console.log('components/Files  render');
        const {props, state, txt} = this;

        var menu;    

       menu = (<Dropzone onDrop={this.onDrop}>
  {({getRootProps, getInputProps}) => (
    <Segment className={props.appState.className}>
      <div {...getRootProps()}>
        <input {...getInputProps()} />
        <p><Icon name='upload' loading={state.uploading}/> {state.uploading? txt('Uploading. Please wait...'): txt("Upload: Drag and drop files here, or click to select files")}</p>
      </div>
    </Segment>
  )}
</Dropzone>
          );

        return menu;
    }
}