import * as React from 'react';
import Switch from '@material-ui/core/Switch';
import TextField from '@material-ui/core/TextField';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import { ImageUpload } from 'ory-editor-ui';
import Typography from '@material-ui/core/Typography';
import { ImageLoaded, ImageUploaded } from 'ory-editor-ui/lib/ImageUpload';
import { BackgroundProps } from '../../types/component';

export interface ImageComponentProps {
  ensureModeOn: () => void;
  onImageLoaded: (image: ImageLoaded) => void;
  onImageUploaded: () => void;
}

class ImageComponent extends React.Component<
BackgroundProps & ImageComponentProps
> {
  handleChangeBackground = (e: React.ChangeEvent<HTMLInputElement>) => {
    this.props.ensureModeOn();
    this.props.onChange({ background: e.target.value });
  }

  handleChangeIsParallax = (e: React.ChangeEvent<HTMLInputElement>) => {
    this.props.ensureModeOn();
    this.props.onChange({
      isParallax:
        this.props.state.isParallax === undefined
          ? false
          : !this.props.state.isParallax,
    });
  }

  handleImageLoaded = (image: ImageLoaded) => {
    this.props.ensureModeOn();
    this.props.onImageLoaded(image);
  }

  handleImageUploaded = (resp: ImageUploaded) => {
    this.props.onImageUploaded();
    this.props.onChange({ background: resp.url });
  }

  render() {
    const {
      state: { isParallax = true, background = '' },
    } = this.props;
    return (
      <div>
        <div>
          {this.props.imageUpload && (
            <React.Fragment>
              <ImageUpload
                imageUpload={this.props.imageUpload}
                imageLoaded={this.handleImageLoaded}
                imageUploaded={this.handleImageUploaded}
              />
              {/* <Typography
                variant="body1"
                style={{ marginLeft: '20px', marginRight: '20px' }}
              >
                OR
              </Typography> */}
            </React.Fragment>
          )}
          <label className='webbuilder_input_label' htmlFor="">
            {this.props.imageUpload ? 'I have a URL' : 'Image URL'}
          </label>
          <input 
            type="text"
            className='webbuilder_input_box'
            placeholder="http://example.com/image.png"
            value={background}
            onChange={this.handleChangeBackground}
          
          />
          {/* <TextField
            placeholder="http://example.com/image.png"
            label={this.props.imageUpload ? 'I have a URL' : 'Image URL'}
            style={{ width: '100%' }}
            value={background}
            onChange={this.handleChangeBackground}
          /> */}
        </div>
        <br />
        {/* <div>
          <FormControlLabel
            control={
              <Switch
                onChange={this.handleChangeIsParallax}
                checked={isParallax}
              />
            }
            label="Is parallax"
          />
        </div> */}
      </div>
    );
  }
}

export default ImageComponent;
