import React, { useState, useEffect } from 'react';
import { Form, Input, Radio, Switch } from 'antd';
import Group from '../../components/group';
import { findKeyByValue, findValuesByNamePath } from '../../utils';
import Colors from '../colors';
import ImageChoose from '../../image/editor/form/selector';
import { PREFIX_CLS } from '../../config';
import './index.less';

const prefixCls = `${PREFIX_CLS}-background-editor`;

const repeatOptions = [
  { label: '不平铺', value: 'no-repeat' },
  { label: '平铺-x', value: 'repeat-x' },
  { label: '平铺-y', value: 'repeat-y' },
];
const sizeOptions = [
  { label: '铺满', value: 'cover' },
  { label: '自适应', value: 'contain' },
];
const positionOption = [
  { label: '上', value: 'top' },
  { label: '中', value: 'center' },
  { label: '下', value: 'bottom' },
];

export default ({ node, propKey, config, onChangePathValue }) => {
  const defaultGroup = {
    label: config.label,
  };
  const { name, group = defaultGroup } = config;
  const [more, setMore] = useState(false);
  const [state, setState] = useState();

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

  const handleChange = (value, key) => {
    const statePath = findKeyByValue(name, key);
    changeState({ [statePath]: value });
    onChangePathValue({ path: key, value });
  };

  const handleInputChange = ({ target: { value } }) => {
    changeState({ backgroundImage: value });
    onChangePathValue({ path: name.backgroundImage, value });
  };

  useEffect(() => {
    const pathValues = findValuesByNamePath(config.name, node[propKey]);
    setState({
      backgroundRepeat: pathValues.backgroundRepeat,
      backgroundSize: pathValues.backgroundSize,
      backgroundPosition: pathValues.backgroundPosition,
      backgroundColor: pathValues.backgroundColor,
      backgroundImage: pathValues.backgroundImage,
    });
  }, [node[propKey]]);

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

  return (
    <div className={prefixCls}>
      <Group group={group}>
        <Form.Item>
          <div className="cell-row">
            <div className="cell-row__lable">颜色</div>
            <Colors
              name={name.backgroundColor}
              value={state.backgroundColor}
              onChangePathValue={onChangePathValue}
            />
          </div>
          {name.backgroundImage && (
            <>
              <div className="cell-row">
                <div className="cell-row__lable">图片</div>
                <ImageChoose
                  src={state.backgroundImage}
                  onChange={e => handleInputChange({ target: { value: e } })}
                />
                <div className="image-cell__input">
                  <Input
                    value={state.backgroundImage}
                    onChange={handleInputChange}
                  />
                </div>
              </div>
              {state.backgroundImage && (
                <div className="cell-row">
                  <div className="cell-row__lable">高级设置</div>
                  <Switch defaultChecked={false} onChange={e => setMore(e)} />
                </div>
              )}
            </>
          )}
          {more && (
            <>
              <div className="cell-row">
                <div className="cell-row__lable">平铺</div>
                <Radio.Group
                  size="small"
                  options={repeatOptions}
                  onChange={e => {
                    handleChange(e.target.value, name.backgroundRepeat);
                  }}
                  value={state.backgroundRepeat}
                  optionType="button"
                />
              </div>
              <div className="cell-row">
                <div className="cell-row__lable">背景尺寸</div>
                <Radio.Group
                  size="small"
                  options={sizeOptions}
                  onChange={e => {
                    handleChange(e.target.value, name.backgroundSize);
                  }}
                  value={state.backgroundSize}
                  optionType="button"
                />
              </div>
              <div className="cell-row">
                <div className="cell-row__lable">背景位置</div>
                <Radio.Group
                  size="small"
                  options={positionOption}
                  onChange={e => {
                    handleChange(e.target.value, name.backgroundPosition);
                  }}
                  value={state.backgroundPosition}
                  optionType="button"
                />
              </div>
            </>
          )}
        </Form.Item>
      </Group>
    </div>
  );
};
