import React from 'react';
import {View} from 'react-native';

import {
  Circle,
  Defs,
  LinearGradient,
  RadialGradient,
  Stop,
  Svg,
} from 'react-native-svg';

type CircularProgressProps = {
  size: number;
  strokeWidth: number;
  progressPercent: number;
};

const CircularProgress = (props: CircularProgressProps) => {
  const {size, strokeWidth} = props;
  const radius = (size - strokeWidth) / 2;
  const circumference = radius * 2 * Math.PI;
  const svgProgress = 100 - props.progressPercent;

  return (
    <View style={{margin: 10}}>
      <Svg width={size} height={size}>
        <Defs>
          <LinearGradient id="gradient2" x1="0%" y1="0%" x2="100%" y2="0%">
            <Stop offset="0%" stopColor="#3b5998" />
            <Stop offset="100%" stopColor="#5bc0de" />
          </LinearGradient>
          <RadialGradient id="myGradient">
            <Stop offset="100%" stopColor="gold" />
            <Stop offset="50%" stopColor="red" />
          </RadialGradient>
        </Defs>

        <Circle
          stroke="url(#myGradient)"
          cx={size / 2}
          cy={size / 2}
          r={radius}
          strokeWidth={strokeWidth}
        />

        <Circle
          stroke="url(#gradient2)"
          fill="none"
          cx={size / 2}
          cy={size / 2}
          r={radius}
          strokeDasharray={`${circumference} ${circumference}`}
          strokeDashoffset={radius * Math.PI * 2 * (svgProgress / 100)}
          strokeLinecap="round"
          transform={`rotate(-90, ${size / 2}, ${size / 2})`}
          strokeWidth={strokeWidth}
        />
      </Svg>
    </View>
  );
};

export default () => {
  return (
    <View style={{justifyContent: 'center', alignItems: 'center', flex: 1}}>
      <CircularProgress progressPercent={10} size={300} strokeWidth={30} />
    </View>
  );
};
