import React, { useState, useEffect } from 'react';
import { Slider, InputNumber, Row, Col, Radio } from 'antd';
import { presets } from 'react-motion';
import { PREFIX_CLS } from '../../../config';
import './index.less';

const prefixCls = `${PREFIX_CLS}-animate-trigger`;
const animationOptions = [
  { key: 'noWobble' },
  { key: 'gentle' },
  { key: 'wobbly' },
  { key: 'stiff' },
  { key: '自定义' },
];
export default ({ animation = {}, onChange }) => {
  const [stateAnimation, setStateAnimation] = useState(animation);
  const {
    animationMode = 'noWobble',
    stiffness,
    damping,
    event,
    scale,
  } = stateAnimation;
  const handleChange = params => {
    const nextAnimation = { ...stateAnimation, ...params };
    setStateAnimation(nextAnimation);
    onChange(nextAnimation);
  };
  const handleModeChange = e => {
    handleChange({ ...presets[e], animationMode: e });
  };
  useEffect(() => {
    setStateAnimation(animation);
  }, [animation]);
  return (
    <>
      <Row className={prefixCls}>
        <Col className={`${prefixCls}__title`}>
          <span>触发方式</span>
        </Col>
        <Col>
          <Radio.Group
            defaultValue="init"
            value={event}
            onChange={e => handleChange({ event: e.target.value })}
            buttonStyle="solid"
          >
            <Radio.Button value="init">入场</Radio.Button>
            <Radio.Button value="hover">鼠标聚焦</Radio.Button>
          </Radio.Group>
        </Col>
      </Row>
      <Row className={prefixCls}>
        <Col className={`${prefixCls}__title`}>
          <div>大小变化</div>
        </Col>
        <Col span={12}>
          <Slider
            min={0}
            max={5}
            step={0.1}
            onChange={e => handleChange({ scale: e })}
            value={typeof scale === 'number' ? scale : 0}
          />
        </Col>
        <Col span={4}>
          <InputNumber
            min={0}
            max={5}
            step={0.1}
            style={{ width: 60 }}
            value={typeof scale === 'number' ? scale : 0}
            onChange={e => handleChange({ scale: e })}
          />
        </Col>
      </Row>
      <Row className={prefixCls}>
        <Col className={`${prefixCls}__title`}>
          <span>动画形态</span>
        </Col>
        <Col>
          <Radio.Group
            defaultValue="init"
            value={animationMode}
            onChange={e => handleModeChange(e.target.value)}
            buttonStyle="solid"
            size="small"
          >
            {animationOptions.map(item => {
              return <Radio.Button value={item.key}>{item.key}</Radio.Button>;
            })}
          </Radio.Group>
        </Col>
      </Row>
      {animationMode === '自定义' && (
        <>
          <Row className={prefixCls}>
            <Col className={`${prefixCls}__title`}>
              <div>动画强度</div>
            </Col>
            <Col span={12}>
              <Slider
                min={0}
                max={300}
                step={0.1}
                onChange={e => handleChange({ stiffness: e })}
                value={typeof stiffness === 'number' ? stiffness : 0}
              />
            </Col>
            <Col span={4}>
              <InputNumber
                min={0}
                max={300}
                step={0.1}
                style={{ width: 60 }}
                value={typeof stiffness === 'number' ? stiffness : 0}
                onChange={e => {
                  handleChange({ stiffness: e });
                }}
              />
            </Col>
          </Row>
          <Row className={prefixCls}>
            <Col className={`${prefixCls}__title`}>
              <div>动画硬度</div>
            </Col>
            <Col span={12}>
              <Slider
                min={0}
                max={110}
                step={0.1}
                onChange={e => handleChange({ damping: e })}
                value={typeof damping === 'number' ? damping : 0}
              />
            </Col>
            <Col span={4}>
              <InputNumber
                min={0}
                max={110}
                step={0.1}
                style={{ width: 60 }}
                value={typeof damping === 'number' ? damping : 0}
                onChange={e => {
                  handleChange({ damping: e });
                }}
              />
            </Col>
          </Row>
        </>
      )}
    </>
  );
};
