/* eslint-disable no-param-reassign */
import React, { useState, useEffect } from 'react';
import { Form, InputNumber, Radio, Slider, Switch } from 'antd';
import produce from 'immer';
import Group from '../../components/group';
import Colors from '../colors';
import { findValuesByNamePath, isEqual } from '../../utils';

const styleOptions = [
  { label: '无', value: 'none' },
  { label: '实线', value: 'solid' },
  { label: '虚线', value: 'dashed' },
];

export default ({ node, propKey, config, onChangePathValue }) => {
  const defaultGroup = {
    label: config.label,
  };
  const { name, group = defaultGroup } = config;
  const monitor = node[propKey];
  const [state, setState] = useState();
  const [inputValue, setInputValue] = useState(0);
  const [custom, setCustom] = useState(0);

  const changeState = param => {
    setState({ ...state, ...param });
  };

  const onTypeChange = e => {
    let { value } = e.target;
    if (!value) {
      value = 'none';
    }
    changeState({ [name.borderStyle]: value });
    onChangePathValue({ path: name.borderStyle, value });
  };

  const onWidthChange = ({ index, value }) => {
    const nextBorderWidth = produce(state.borderWidth, draft => {
      draft[index] = value;
    });
    changeState({ [name.borderWidth]: nextBorderWidth });
    onChangePathValue({ path: name.borderWidth, value: nextBorderWidth });
  };

  const onSliderChange = e => {
    setInputValue(e);
    const value = [e, e, e, e];
    changeState({ [name.borderWidth]: value });
    onChangePathValue({ path: name.borderWidth, value });
  };

  useEffect(() => {
    const {
      borderWidth = [0, 0, 0, 0],
      borderColor,
      borderStyle,
    } = findValuesByNamePath(name, monitor);

    setState({
      [name.borderWidth]: borderWidth,
      [name.borderColor]: borderColor,
      [name.borderStyle]: borderStyle,
    });

    const eqValue = borderWidth[0];
    const eq = isEqual(borderWidth);
    setCustom(!eq);
    if (eq) {
      setInputValue(eqValue);
    }
  }, [monitor]);

  if (!state) {
    return <></>;
  }

  return (
    <Group group={group}>
      <Form.Item>
        <div className="cell">
          <div className="cell-row">
            <div className="cell-row__lable">边框样式</div>
            <Radio.Group
              size="small"
              options={styleOptions}
              onChange={onTypeChange}
              value={state[name.borderStyle]}
              optionType="button"
            />
          </div>
          <div className="cell-row">
            <div className="cell-row__lable">高级设置</div>
            <Switch
              checked={custom}
              onChange={e => {
                setCustom(e);
              }}
            />
          </div>
          {custom ? (
            <div className="cell-row">
              {state[name.borderWidth].map((item, index) => (
                <div className="input-number-wrap">
                  <InputNumber
                    min={0}
                    value={item}
                    onChange={e => {
                      onWidthChange({ index, value: e });
                    }}
                  />
                </div>
              ))}
            </div>
          ) : (
            <div className="cell-row">
              <div className="cell-row__lable">边框大小</div>
              <Slider
                min={0}
                max={20}
                onChange={onSliderChange}
                value={inputValue}
                style={{ width: '140px' }}
              />
              <InputNumber
                min={0}
                max={20}
                value={inputValue}
                onChange={onSliderChange}
              />
            </div>
          )}
          <div className="cell-row">
            <div className="cell-row__lable">边框颜色</div>
            <Colors
              name={name.borderColor}
              value={state[name.borderColor]}
              onChangePathValue={onChangePathValue}
            />
          </div>
        </div>
      </Form.Item>
    </Group>
  );
};
