import React from 'react'
import { useMeasure } from 'react-use'
import { AppProps, ExtensionType } from 'src/types'
import 'src/style.css'

import {
  getRotateAngleConfig,
  getSizeConfig,
  getBasicConfig,
  getShadowConfig,
  getStrokeConfig
} from 'src/utils'

import {
  Large_Canvas_Width
} from 'src/contants'

const Ellipse: React.FC<AppProps> = (props) => {
  const {
    properties,
    theme
  } = props
  const [containerRef, { width: offsetWidth, height: offsetHeight }] = useMeasure()

  const rotateAngle = React.useMemo(() => {
    return getRotateAngleConfig(properties)
  }, [properties])

  const {
    backgroundColor,
  } = React.useMemo(() => {
    return getBasicConfig(properties, theme, ExtensionType.Ellipse)
  }, [properties, theme])

  const shadow = React.useMemo(() => {
    return getShadowConfig(properties, theme)
  }, [properties, theme])

  const {
    containerWidth,
    containerHeight,
    w,
    h
  } = React.useMemo(() => {
    return getSizeConfig(offsetWidth, offsetHeight, rotateAngle)
  }, [offsetWidth, offsetHeight, rotateAngle])

  const {
    strokeDasharray,
    strokeLinecap,
    stroke,
    strokeWidth
  } = React.useMemo(() => {
    return getStrokeConfig(properties, theme, offsetWidth, Large_Canvas_Width, ExtensionType.Ellipse)
  }, [properties, theme, offsetWidth])

  return (
    <div className='graphicsContainer' ref={containerRef}>
      <svg
        className="graphics"
        version="1.1"
        xmlns="http://www.w3.org/2000/svg"
        height={containerHeight}
        width={containerWidth}
        viewBox={`0 0 ${containerWidth} ${containerHeight}`}
        style={{ filter: shadow }}
      >
        {/* 椭圆 */}
        <ellipse
          cx={containerWidth / 2}
          cy={containerHeight / 2}
          rx={w / 2 - strokeWidth / 2}
          ry={h / 2 - strokeWidth / 2}
          strokeDasharray={strokeDasharray}
          strokeLinecap={strokeLinecap}
          stroke={stroke}
          fill={backgroundColor}
          strokeWidth={strokeWidth}
          transform={`rotate(${rotateAngle}, ${containerWidth / 2}, ${containerHeight / 2})`}
        />
      </svg>
    </div >
  )
}

export default Ellipse
