import React from 'react';
import { values, keys } from 'lodash';
import { Form } from 'antd';
import { mergeObject } from '../../../../utils';
import { plugin } from '../../../jscss';
import Button from '../../../index';
import Group from '../../../../components/group';

const notCoverPaths = {
  props: ['text'],
  style: ['left', 'top', 'xAlign', 'yAlign', 'width', 'height', 'zIndex'],
};
export default ({
  config = {},
  propKey,
  node = {},
  propValue,
  onChange = () => {},
  dispatch,
}) => {
  const defaultGroup = {
    label: config.label,
  };
  const { group = defaultGroup } = config;
  const ns_button = {
    props: { text: '日产' },
    style: { backgroundColor: '#c3002f' },
  };
  const ns_ghost_button = {
    props: { text: '日产-边框按钮' },
    style: {
      color: '#c3002f',
      borderColor: '#c3002f',
      borderWidth: [1, 1, 1, 1],
      borderStyle: 'solid',
      backgroundColor: 'transparent',
      hover: {
        color: '#c3002f',
        backgroundColor: 'transparent',
      },
    },
  };
  const ns_ghost_defatul_button = {
    props: { text: '日产-边框按钮' },
    style: {
      color: '#333',
      borderColor: '#333',
      borderWidth: [1, 1, 1, 1],
      borderStyle: 'solid',
      backgroundColor: 'transparent',
      hover: {
        color: '#fff',
        borderColor: '#c3002f',
        backgroundColor: '#c3002f',
      },
    },
  };
  const ns_icon_button = {
    props: {
      text: '日产-图标按钮',
      icon: { type: 'icon-jia', fontSize: 14, position: 'auto-right' },
    },
    style: {
      width: 160,
      color: '#333',
      borderColor: '#333',
      borderWidth: [1, 1, 1, 1],
      borderStyle: 'solid',
      backgroundColor: 'transparent',
      hover: {
        color: '#fff',
        borderColor: '#c3002f',
        backgroundColor: '#c3002f',
      },
    },
  };

  const ns_app_icon_button = {
    props: {
      text: '日产-图标按钮-跳转',
      icon: { type: 'icon-right', fontSize: 14, position: 'auto-right' },
    },
    style: {
      width: 160,
      color: '#0092FB',
      borderWidth: 0,
      backgroundColor: 'transparent',
      hover: {
        color: '#0092FB',
        backgroundColor: 'transparent',
      },
    },
  };

  const handleClick = data => {
    const keysArry = keys(data);
    const valuesArry = values(data);
    keysArry.forEach((key, index) => {
      if (propKey === key) {
        notCoverPaths[key].forEach(pathKey => {
          valuesArry[index][pathKey] = propValue[pathKey];
        });
      } else {
        notCoverPaths[key].forEach(pathKey => {
          if (node[key]) {
            valuesArry[index][pathKey] = node[key][pathKey];
          }
        });
      }
    });
    keysArry.forEach((key, index) => {
      valuesArry[index] = mergeObject(plugin[key], valuesArry[index]);
    });
    onChange(keysArry, valuesArry, true);
    dispatch({
      type: 'drawer/forceRefresh',
      payload: { id: node.id },
    });
  };

  return (
    <Group group={group}>
      <Form.Item>
        <div className="cell-row">
          <div style={{ display: 'flex', flexFlow: 'row wrap' }}>
            <Button data={ns_button} onClick={() => handleClick(ns_button)} />
            <Button
              data={ns_ghost_button}
              onClick={() => handleClick(ns_ghost_button)}
            />
            <Button
              data={ns_ghost_defatul_button}
              onClick={() => handleClick(ns_ghost_defatul_button)}
            />
            <Button
              data={ns_icon_button}
              onClick={() => handleClick(ns_icon_button)}
            />
            <Button
              data={ns_app_icon_button}
              onClick={() => handleClick(ns_app_icon_button)}
            />
          </div>
        </div>
      </Form.Item>
    </Group>
  );
};
