/**
 * Created by baoyinghai on 11/17/16.
 */
import React from 'react';
import { autobind } from 'core-decorators';
import { VIEW, EDIT } from '../../constant';
import ValidateTypes from '../../formValidator/validateTypes';
import { Input } from '../../../../../components/mxa';


const MAX = '99999999999'; // 一千亿
const numeral = require('numeral');

class MyInputNumber extends React.Component {

  @autobind
  _onChange(e) {
    /* eslint-disable */
    const { onChange, max, format, magnitude, min } = this.props;
    const pointNum = this.getPointNum(e.target.value);
    const a = parseInt('1' + '0'.repeat(pointNum));
    if ((e && e.target) && (isNaN && !isNaN(e.target.value) || e.target.value === '-') && parseFloat((e.target.value && e.target.value !== '-') ? e.target.value : 0) < parseFloat(max) && parseFloat((e.target.value && e.target.value !== '-') ? e.target.value : 0) > parseFloat(min) && this.getPointNum(format) >= pointNum) {
      this.inputValue = e.target.value + '';
      const res = e.target.value === '' ? undefined : numeral(numeral(e.target.value).format(format)).value() * a / (magnitude * a);
      onChange && onChange(res);
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { addonBefore, magnitude, value, format } = this.props;
    if (nextProps.addonBefore !== addonBefore) {
      // nextProps.value = parseFloat(nextProps.value, 10) ? numeral(magnitude * nextProps.value).format(format) : nextProps.value;
      return true;
    }

    if (nextProps.value === (this.inputValue ? this.inputValue : value)) {
      return false;
    }
    return true;
  }

  @autobind
  _onFocus(e) {
    if (e.target.value && e.target.value.length > 0) {
      e.target.value = numeral(e.target.value).value();
    }
  }

  @autobind
  _onBlur(e) {
    if (parseFloat(e.target.value)) {
      e.target.value = numeral(e.target.value).format(this.props.format);
    }
  }

  @autobind
  getPointNum(value) {
    return (value && value.indexOf('.') !== -1) ? value.substring(value.indexOf('.'), value.length).length : 1;
  }

  render() {
    const { value, magnitude, format, addonAfter, addonBefore, disabled } = this.props;
    const renderValue = this.inputValue ? this.inputValue : (parseFloat(value, 10) ? numeral(magnitude * value).format(format) : value);
    this.inputValue= null;
    return (
      <Input value={renderValue} addonBefore={addonBefore || ''} addonAfter={addonAfter || ''} onFocus={this._onFocus} onBlur={this._onBlur} onChange={this._onChange} disabled={disabled} />
    );
  }
}

const compRender = config => {
  const { record, props } = config;

  const formValidate = record.formValidates && record.formValidates.filter((item) => item.validateType === ValidateTypes.ISAMOUNT)[0];

  const format = formValidate && formValidate.format;
  let max = formValidate && formValidate.max;
  let min = formValidate && formValidate.min;
  // TODO: min
  if (min || min === 0) {
    const magnitude = record.displayComponent.magnitude;
    if (magnitude) {
      min = magnitude * min;
    }
  } else {
    min = -100000000000;
  }

  if (max || max === 0) {
    const magnitude = record.displayComponent.magnitude;
    if (magnitude) {
      max = magnitude * max;
    }
  } else {
    max = 100000000000;
  }
  return (
    <MyInputNumber
      disabled={record.isReadonly || (props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly)}
      format={format || '0,0.0000'}
      addonAfter={record.displayComponent.addOnAfter}
      addonBefore={record.displayComponent.addOnBefore}
      magnitude={record.displayComponent.magnitude || 1}
      max={max}
      min={min}
    />
  );
};

class StaticDisplay extends React.Component {
  render() {
    const value = this.props.value;
    const format = this.props.format ? this.props.format : '0,0.0000';
    return (
      <span>{value && numeral(value).format(format)}</span>
    );
  }
}

export default {
  [VIEW]: config => {
    const { record } = config;

    const formValidate = record.formValidates && record.formValidates.filter((item) => item.validateType === ValidateTypes.ISAMOUNT)[0];

    const format = formValidate && formValidate.format;

    return (
      <StaticDisplay format={format} />
    );
  },
  [EDIT]: compRender
};
