 import UIKit
 
 @IBDesignable open class DDSpiderChartView: UIView {
-
+    
     public var axes: [DrawableString] = [] {
         didSet {
             // When categories change, data sets should be cleaned

        }
    }
    var views: [DDSpiderChartDataSetView] = [] // DDSpiderChartDataSetView's currently being presented

    @IBInspectable public var color: UIColor = .gray {
        didSet {
            setNeedsDisplay()
        }
    }

    @IBInspectable public var circleCount: Int = 10 {
        didSet {
            views.forEach {

            let y = center.y + (circleRadius + circleGap) * sin(angle)
            linePath.addLine(to: CGPoint(x: x, y: y))
            linePath.stroke()
            
            var circleCenter = CGPoint(x: center.x + (circleRadius) * cos(angle), y: center.y + (circleRadius) * sin(angle))
            if endLineCircles {
                // Draw circle at the end the line
                circleCenter = CGPoint(x: center.x + (circleRadius + circleGap * 3/2) * cos(angle), y: center.y + (circleRadius + circleGap * 3/2) * sin(angle))
                let circlePath = UIBezierPath(arcCenter: circleCenter, radius: circleGap/2, startAngle: 0, endAngle: CGFloat(2 * Float.pi), clockwise: true)
                circlePath.stroke()
            }
            
            // Draw axes label
            let isOnTop = sin(angle) < 0 // we should draw text on top of the circle when circle is on the upper half. (and vice versa)
            let isOnLeft = cos(angle) < 0 // we should draw text on left of the circle when circle is on the left half. (and vice versa)
            
            let categoryStringSize = axis.size()
            let categoryStringPadding = circleGap/2 + 5
            if endLineCircles {
                let categoryStringOrigin = CGPoint(x: circleCenter.x - categoryStringSize.width/2, y: circleCenter.y+(isOnTop ? (-(categoryStringSize.height+categoryStringPadding)) : (categoryStringPadding)))
                axis.drawDrawable(with: .init(origin: categoryStringOrigin, size: categoryStringSize))
            }
            else {
                let categoryStringOrigin = CGPoint(x: circleCenter.x-(index == 0 ? categoryStringSize.width/2 : (isOnLeft ? categoryStringSize.width+categoryStringPadding : -categoryStringPadding)), y: circleCenter.y+(isOnTop ? (-(categoryStringSize.height+categoryStringPadding)) : (categoryStringPadding)))
                axis.drawDrawable(with: .init(origin: categoryStringOrigin, size: categoryStringSize))
            }
        }
        
    }
}

// MARK: Computational properties & helper methods
extension DDSpiderChartView {
    
    fileprivate var circleRadius: CGFloat {
        return CGFloat(circleCount) * circleGap
    }
    
    override open var intrinsicContentSize: CGSize {
        let len = 2 * circleRadius + 100 // +100 for text
        return .init(width: len, height: len)
    }
    
    override open func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        axes = ["Axis 1", "Axis 2", "Axis 3", "Axis 4", "Axis 5"]
        _ = addDataSet(values: [0.7, 0.5, 0.6, 0.9, 1.0], color: .white)
    }
    
}

