import * as React from 'react';
import * as _ from 'lodash';
import LexSingleInput from './LexSingleInput';
import {
  Image,
  Input,
  Card,
  Button
} from 'semantic-ui-react';
const { Box } = require('reflexbox');

export class LexEdit extends React.Component<any, any> {
  imageRoot = '/png/';
  constructor(props: any) {
    super(props);
    this.state = { lexItem: this.props.lexItem };
  }
  public render() {
    let li = this.state.lexItem;
    let editLang = _.get<any>(li, this.props.fieldMetaMap.lang.lens, 'en');
    let langSelOpts = _.get<any>(this.props.selectionMeta, editLang, []);
    let lexFields = _.keys(this.props.fieldMetaMap).map(field => {
      let lens = this.props.fieldMetaMap[field].lens;
      let defaultText = _.get<any>(li, lens, '');
      let sh = (e: any) => {
        let eventData = {};
        eventData[field] = e.target.value;
        this.handleOnChange(eventData);
      };
      if (this.props.fieldMetaMap[field].type === 'text') {
        return (
          <LexSingleInput key={field} labelText={_.capitalize(field)}>
            <Input
              onChange={sh}
              defaultValue={defaultText}
              placeholder={field}
              type="text"
              dir="auto"
              style={{ width: '10em' }}
            />
          </LexSingleInput>
        );
      } else if (this.props.fieldMetaMap[field].type === 'select') {
        let staticOpts = this.props.fieldMetaMap[field].options;
        let fieldOpts = _.get<any>(langSelOpts, field, []);
        let selOpts = staticOpts ? staticOpts : fieldOpts;
        return (
          <LexSingleInput key={field} labelText={_.capitalize(field)}>
            <select
              onChange={sh}
              style={{ width: '10em' }}
              defaultValue={defaultText}
            >
              {selOpts.map((k: any, i: any, c: any) => {
                return <option key={i} value={k}> {k}</option>;
              })}
            </select>
          </LexSingleInput>
        );
      } else if (this.props.fieldMetaMap[field].type === 'preview' && defaultText !== '') {
        let imageSrc = this.imageRoot + defaultText;
        return (
          <LexSingleInput key={field} labelText={_.capitalize(field)}>
            <Image src={imageSrc} size="tiny" bordered={true} />
          </LexSingleInput>
        );
      } else {
        return null;
      }
    });
    return (
      <Box m={2}>
        <Card raised={true}>
          <Card.Content>
            <Card.Header>
              {_.get<any>(li, 'label', '')}
            </Card.Header>
            <Card.Meta>
              language: {_.get<any>(li, '$.id', '')}
            </Card.Meta>
            <Card.Description>
              {lexFields}
            </Card.Description>
          </Card.Content>
          <Card.Content extra={true}>
            <Button
              basic={true}
              fluid={true}
              color="green"
              onClick={(e, d) => this.handleOnSave(d)}
            >
              Save
            </Button>
          </Card.Content>
        </Card>
      </Box>
    );
  }

  private handleOnChange(event: any) {
    let type = _.keys(event)[0];
    let value = _.values(event)[0];
    let lens = this.props.fieldMetaMap[type].lens;
    let { lexItem: lexItem } = this.state;
    _.set(lexItem, lens, value);
    this.setState(lexItem);
  }

  private handleOnSave(event: any) {
    this.props.save(this.state.lexItem);
    console.log('saving object :', this.state.lexItem);
  }
}
