import React from 'react';
import PropTypes from 'prop-types';
import Box from '@material-ui/core/Box';
import { makeStyles } from '@material-ui/core/styles';
import { blueGrey } from '@material-ui/core/colors';

const useStyles = makeStyles({
  root: {
    left: 0,
    height: 125,
    width: '100%',
    overflow: 'hidden',
    fill: (props) => props.fill,
    transform: (props) =>
      props.invert ? 'scale(-1)' : 'none',
    marginBottom: (props) =>
      props.gutterBottom ? 48 : -10,
    '&>svg': {
      height: '100%',
      width: '100%',
    },
  },
});

const Curve = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 1000 100"
    preserveAspectRatio="none"
  >
    <path
      d="M421.9,6.5c22.6-2.5,51.5,0.4,75.5,5.3c23.6,4.9,70.9,23.5,100.5,35.7c75.8,32.2,133.7,44.5,192.6,49.7
 c23.6,2.1,48.7,3.5,103.4-2.5c54.7-6,106.2-25.6,106.2-25.6V0H0v30.3c0,0,72,32.6,158.4,30.5c39.2-0.7,92.8-6.7,134-22.4
 c21.2-8.1,52.2-18.2,79.7-24.2C399.3,7.9,411.6,7.5,421.9,6.5z"
    />
  </svg>
);

const Swoop = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 100 100"
    preserveAspectRatio="none"
  >
    <path d="M0 0 C 50 100 80 100 100 0 Z" />
  </svg>
);

const Brush = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 283.5 27.8"
    preserveAspectRatio="none"
  >
    <path d="M283.5,9.7c0,0-7.3,4.3-14,4.6c-6.8,0.3-12.6,0-20.9-1.5c-11.3-2-33.1-10.1-44.7-5.7	s-12.1,4.6-18,7.4c-6.6,3.2-20,9.6-36.6,9.3C131.6,23.5,99.5,7.2,86.3,8c-1.4,0.1-6.6,0.8-10.5,2c-3.8,1.2-9.4,3.8-17,4.7	c-3.2,0.4-8.3,1.1-14.2,0.9c-1.5-0.1-6.3-0.4-12-1.6c-5.7-1.2-11-3.1-15.8-3.7C6.5,9.2,0,10.8,0,10.8V0h283.5V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.1c-0.1,0.3,0.3,0.5,1.9-0.2s2.6-1.3,2.2-1.2s-0.9,0.4-2.5,0.8C65.3,10.9,65.5,10.8,65.4,11.1	z M34.5,12.4c-0.2,0,2.1,0.8,3.3,0.9c1.2,0.1,2,0.1,2-0.2c0-0.3-0.1-0.5-1.6-0.4C36.6,12.8,34.7,12.4,34.5,12.4z M152.2,21.1	c-0.1,0.1-2.4-0.3-7.5-0.3c-5,0-13.6-2.4-17.2-3.5c-3.6-1.1,10,3.9,16.5,4.1C150.5,21.6,152.3,21,152.2,21.1z" />
    <path d="M269.6,18c-0.1-0.1-4.6,0.3-7.2,0c-7.3-0.7-17-3.2-16.6-2.9c0.4,0.3,13.7,3.1,17,3.3	C267.7,18.8,269.7,18,269.6,18z" />
    <path d="M227.4,9.8c-0.2-0.1-4.5-1-9.5-1.2c-5-0.2-12.7,0.6-12.3,0.5c0.3-0.1,5.9-1.8,13.3-1.2	S227.6,9.9,227.4,9.8z" />
    <path d="M204.5,13.4c-0.1-0.1,2-1,3.2-1.1c1.2-0.1,2,0,2,0.3c0,0.3-0.1,0.5-1.6,0.4	C206.4,12.9,204.6,13.5,204.5,13.4z" />
    <path d="M201,10.6c0-0.1-4.4,1.2-6.3,2.2c-1.9,0.9-6.2,3.1-6.1,3.1c0.1,0.1,4.2-1.6,6.3-2.6	S201,10.7,201,10.6z" />
    <path d="M154.5,26.7c-0.1-0.1-4.6,0.3-7.2,0c-7.3-0.7-17-3.2-16.6-2.9c0.4,0.3,13.7,3.1,17,3.3	C152.6,27.5,154.6,26.8,154.5,26.7z" />
    <path d="M41.9,19.3c0,0,1.2-0.3,2.9-0.1c1.7,0.2,5.8,0.9,8.2,0.7c4.2-0.4,7.4-2.7,7-2.6	c-0.4,0-4.3,2.2-8.6,1.9c-1.8-0.1-5.1-0.5-6.7-0.4S41.9,19.3,41.9,19.3z" />
    <path d="M75.5,12.6c0.2,0.1,2-0.8,4.3-1.1c2.3-0.2,2.1-0.3,2.1-0.5c0-0.1-1.8-0.4-3.4,0	C76.9,11.5,75.3,12.5,75.5,12.6z" />
    <path d="M15.6,13.2c0-0.1,4.3,0,6.7,0.5c2.4,0.5,5,1.9,5,2c0,0.1-2.7-0.8-5.1-1.4	C19.9,13.7,15.7,13.3,15.6,13.2z" />
  </svg>
);

const Angle = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 1000 100"
    preserveAspectRatio="none"
  >
    <path d="M0,6V0h1000v100L0,6z" />
  </svg>
);

const Lines = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 2600 131.1"
    preserveAspectRatio="none"
  >
    <path d="M0 0L2600 0 2600 69.1 0 0z" />
    <path
      style={{ opacity: 0.5 }}
      d="M0 0L2600 0 2600 69.1 0 69.1z"
    />
    <path
      style={{ opacity: 0.25 }}
      d="M2600 0L0 0 0 130.1 2600 69.1z"
    />
  </svg>
);

const Mountains = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 1000 100"
    preserveAspectRatio="none"
  >
    <path d="M761.9,44.1L643.1,27.2L333.8,98L0,3.8V0l1000,0v3.9" />
  </svg>
);

const Wave = () => (
  <svg
    viewBox="0 0 1280 140"
    xmlns="http://www.w3.org/2000/svg"
    preserveAspectRatio="none"
  >
    <g>
      <path
        d="M504.854,80.066c7.812,0,14.893,0.318,21.41,0.879 c-25.925,22.475-56.093,40.852-102.946,40.852c-20.779,0-37.996-2.349-52.898-6.07C413.517,107.295,434.056,80.066,504.854,80.066z M775.938,51.947c19.145,18.596,39.097,35.051,77.956,35.051c46.907,0,62.299-14.986,80.912-24.98 c-21.357-15.783-46.804-28.348-85.489-28.348C816.829,33.671,794.233,41.411,775.938,51.947z"
        fillOpacity=".3"
      />
      <path
        d="M1200.112,46.292c39.804,0,59.986,22.479,79.888,39.69v16.805 c-19.903-10.835-40.084-21.777-79.888-21.777c-72.014,0-78.715,43.559-147.964,43.559c-56.84,0-81.247-35.876-117.342-62.552 c9.309-4.998,19.423-8.749,34.69-8.749c55.846,0,61.99,39.617,115.602,39.617C1143.177,92.887,1142.618,46.292,1200.112,46.292z M80.011,115.488c-40.006,0-60.008-12.206-80.011-29.506v16.806c20.003,10.891,40.005,21.782,80.011,21.782 c80.004,0,78.597-30.407,137.669-30.407c55.971,0,62.526,24.026,126.337,24.026c9.858,0,18.509-0.916,26.404-2.461 c-57.186-14.278-80.177-48.808-138.66-48.808C154.698,66.919,131.801,115.488,80.011,115.488z M526.265,80.945 c56.848,4.902,70.056,28.726,137.193,28.726c54.001,0,73.43-35.237,112.48-57.724C751.06,27.782,727.548,0,665.691,0 C597.381,0,567.086,45.555,526.265,80.945z"
        fillOpacity=".5"
      />
      <path d="M0,0v85.982c20.003,17.3,40.005,29.506,80.011,29.506c51.791,0,74.688-48.569,151.751-48.569 c58.482,0,81.473,34.531,138.66,48.808c43.096-8.432,63.634-35.662,134.433-35.662c7.812,0,14.893,0.318,21.41,0.879 C567.086,45.555,597.381,0,665.691,0c61.856,0,85.369,27.782,110.246,51.947c18.295-10.536,40.891-18.276,73.378-18.276 c38.685,0,64.132,12.564,85.489,28.348c9.309-4.998,19.423-8.749,34.69-8.749c55.846,0,61.99,39.617,115.602,39.617 c58.08,0,57.521-46.595,115.015-46.595c39.804,0,59.986,22.479,79.888,39.69V0H0z" />
    </g>
  </svg>
);

const Wave2 = () => (
  <svg viewBox="0 0 1000 279.24" preserveAspectRatio="none">
    <path d="M1000 0S331.54-4.18 0 279.24h1000z" />
    <path
      d="M1000 279.24s-339.56-44.3-522.95-109.6S132.86 23.76 0 25.15v254.09z"
      opacity=".65"
    />
  </svg>
);

const Wave3 = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 1000 458.89"
    preserveAspectRatio="none"
  >
    <path
      d="M394.87 433.4C488.07 402 572.38 322.71 656.53 241c-73.83 19-145.79 48.57-216.67 77.31-98.09 39.78-199.68 78.93-304.4 86.55 84.78 42.95 173.24 57.58 259.41 28.54zM656.53 241c45.78-11.75 92.27-19.4 139.69-20.19 70.57-1.16 138.4 12.7 203.78 36.37V0c-59.88 17.86-118.67 47.58-174.92 89.39C767.3 132.33 712 187.19 656.53 241zM135.46 404.86C88.86 381.25 43.38 349.08 0 310.9v82.75a378.35 378.35 0 0 0 81.63 12.23 485.13 485.13 0 0 0 53.83-1.02z"
      opacity="0.65"
    />
    <path d="M1000 458.89V257.18c-65.38-23.67-133.21-37.53-203.78-36.37-47.42.79-93.91 8.44-139.69 20.19-84.15 81.71-168.46 161-261.66 192.4-86.17 29-174.63 14.41-259.41-28.54a485.13 485.13 0 0 1-53.83 1A378.35 378.35 0 0 1 0 393.65v65.24z" />
  </svg>
);

const Wave4 = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 1000 206.86"
    preserveAspectRatio="none"
  >
    <path
      d="M475.75 65c85.1-33.38 176.3-53 268-48.16a485.87 485.87 0 0 1 122.69 22.3A620.49 620.49 0 0 0 769 11.3c-166-32.36-329.9 9.06-482 69.91-98.73 39.51-191.5 86.25-287 125.65h167c65.37-30.67 129.71-65 197.67-94.61C400.93 96.47 438 79.79 475.75 65z"
      opacity="0.3"
    />
    <path d="M741.62 52.76c-129.82-27.54-258 7.7-376.92 59.49-68 29.59-132.3 63.94-197.67 94.61h833v-9.09C930.63 126.88 832.81 72.1 741.62 52.76z" />
    <path
      d="M866.44 39.14a485.87 485.87 0 0 0-122.66-22.31C652.05 12 560.85 31.61 475.75 65c-37.73 14.8-74.82 31.48-111 47.26 118.93-51.79 247.1-87 376.92-59.49 91.19 19.34 189 74.12 258.38 145v-84.5a329.47 329.47 0 0 0-50-36.65 723 723 0 0 0-83.61-37.48z"
      opacity="0.5"
    />
  </svg>
);

const Triangle = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 100 102"
    preserveAspectRatio="none"
  >
    <path d="M0 0 L50 100 L100 0 Z" />
  </svg>
);

const Fade = ({ fill }) => (
  <svg
    viewBox="0 0 1280 140"
    preserveAspectRatio="none"
    xmlns="http://www.w3.org/2000/svg"
  >
    <defs>
      <linearGradient
        id="fadeDown1"
        x1="0"
        x2="0"
        y1="0"
        y2="1"
      >
        <stop
          offset="0%"
          stopColor={`var(--bottom-divider-color, ${fill})`}
        />
        <stop
          offset="100%"
          stopColor={`var(--bottom-divider-color, ${fill})`}
          stopOpacity="0"
        />
      </linearGradient>
    </defs>
    <path
      d="M 0 0 L 0 140 L 1280 140 L 1280 0 Z"
      fill="url(#fadeDown1)"
    />
  </svg>
);

Fade.propTypes = {
  fill: PropTypes.string,
};

Fade.defaultProps = {
  fill: '#999',
};

const Divider = ({ variant, ...rest }) => {
  const { root } = useStyles(rest);
  const getPattern = () => {
    switch (variant) {
      case 'wave':
        return <Wave />;
      case 'wave2':
        return <Wave2 />;
      case 'wave3':
        return <Wave3 />;
      case 'wave4':
        return <Wave4 />;
      case 'brush':
        return <Brush />;
      case 'curve':
        return <Curve />;
      case 'mountains':
        return <Mountains />;
      case 'lines':
        return <Lines />;
      case 'angle':
        return <Angle />;
      case 'triangle':
        return <Triangle />;
      case 'swoop':
        return <Swoop />;
      case 'fade':
        return <Fade fill={rest.fill} />;
      default:
        return null;
    }
  };

  return <Box className={root}>{getPattern()}</Box>;
};

Divider.propTypes = {
  variant: PropTypes.string.isRequired,
  fill: PropTypes.string,
  invert: PropTypes.bool,
  gutterBottom: PropTypes.bool,
};

Divider.defaultProps = {
  fill: blueGrey[50],
  invert: false,
  gutterBottom: false,
};

export default Divider;
