import * as React from 'react';
import * as _ from 'lodash';
import { Label, Form } from 'semantic-ui-react';
const { Flex, Box } = require('reflexbox');
import {
  Input,
  Dropdown,
  Image
} from 'semantic-ui-react';

class LexSingleInput extends React.Component<any, any> {
  public render() {
    return (
      <div>
        <Form>
          <Form.Group as={Flex} inline={true} style={{ margin: '0 0 5px 5px' }}>
            <Form.Field as={Box} w={2 / 5} style={{ textAlign: 'right' }}>
              {this.props.label}
            </Form.Field>
            <Form.Field as={Box} w={3 / 5} >
              {this.props.children}
            </Form.Field>
          </Form.Group>
        </Form>
      </div>
    );
  }
}

function changedLabel(changed: boolean, text: string) {
  let labelClass = changed ? 'olive' : '';
  return (
    <Label
      pointing="right"
      className={labelClass}
    >
      {text}
    </Label>
  );
}

function textInput(params: any) {
  let { field, sh, value, changed } = params;
  return (
    <LexSingleInput
      label={changedLabel(changed, field)}
      key={field}
    >
      <Input
        onChange={(e, d) => { sh(d.value); }}
        value={value}
        placeholder={field}
        type="text"
        dir="auto"
        style={{ width: '10em' }}
      />
    </LexSingleInput>
  );
}

function selectInput(params: any) {
  let { field, sh, value, options, langSelOpts, changed } = params;
  let staticOpts = options;
  let fieldOpts = _.get<any>(langSelOpts, field, []);
  let selOpts = staticOpts ? staticOpts : fieldOpts;
  let dropOptions = selOpts.map((k: any, i: any, c: any) => {
    return { key: i, value: k, text: k };
  });
  return (
    <LexSingleInput key={field} label={changedLabel(changed, field)}>
      <Dropdown
        options={dropOptions}
        onChange={(e, d) => { sh(d.value); }}
        value={value}
        compact={true}
        selection={true}
        search={true}
        style={{ width: '10em' }}
      />
    </LexSingleInput>
  );
}

function imagePreview(params: any) {
  let { field, value, changed } = params;
  let imageSrc = '/png/' + value;
  return value !== '' ? (
    <LexSingleInput key={field} label={changedLabel(changed, field)}>
      <Image src={imageSrc} size="large" bordered={true} />
    </LexSingleInput>
  ) : textInput(params);
}

function listInput(params: any) {
  let { field, sh, langSelOpts, value, changed } = params;
  let fieldOpts = _.get<any>(langSelOpts, field, []);
  let renderLabel = (label: any) => ({
    content: `${label.value}`,
  });
  let dropOptions = fieldOpts.map((k: any, i: any, c: any) => {
    return {
      key: i,
      value: k,
      text: k,
    };
  });
  return (
    <LexSingleInput key={field} label={changedLabel(changed, field)}>
      <Dropdown
        options={dropOptions}
        onChange={(e, d) => {
          sh(d.value);
        }}
        value={value}
        compact={true}
        selection={true}
        search={true}
        multiple={true}
        fluid={true}
        style={{ width: '10em' }}
        renderLabel={renderLabel}
      />
    </LexSingleInput>
  );
}

export function componentForType(type: string, params: any) {
  switch (type) {
    case 'text': {
      return textInput(params);
    }
    case 'select': {
      return selectInput(params);
    }
    case 'preview': {
      return imagePreview(params);
    }
    case 'list': {
      // return (<ListInput params={params} />);
      return listInput(params);
    }
    default: {
      // console.log('type discarded :', type);
      // console.log('values discarded :', fieldMeta.get(li));
      return null;
    }
  }
}
