// Original SwityCircle : https://github.com/BilalReffas/SwiftyCircle

//Customized with a label in center

import UIKit

@IBDesignable public class SwiftyCircle : UIView {
    
    public struct Constants {
        let higherCircle = CAShapeLayer()
        let lowerCircle = CAShapeLayer()
        var label = UILabel() //customized
    }
    
    private var constants = Constants()
    
        var text: NSAttributedString? {
            didSet {
                self.constants.label.attributedText = text
            }
        }

    var textColor : UIColor = UIColor(red:0.13, green:0.44, blue:0.63, alpha:1){
        didSet {
            self.constants.label.textColor = textColor
        }
    }
    
    @IBInspectable public var progress : CGFloat = 0.2{
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var roundCap: Bool = true {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var centerFillColor : UIColor = UIColor.whiteColor(){
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var strokeColor : UIColor = UIColor.whiteColor(){
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var strokeWidth : CGFloat = 7.0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var lowerCirlceStrokeColor : UIColor = UIColor.grayColor(){
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var lowerCirlceStrokeWidth : CGFloat = 3.0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var lowerCirlceProgress : CGFloat = 1.0{
        didSet {
            setNeedsDisplay()
        }
    }
    
    @IBInspectable public var drawlowerCirlce : Bool = false{
        didSet {
            setNeedsDisplay()
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
 
    
    public override func drawRect(rect: CGRect) {
        super.drawRect(rect)
        
        self.layoutIfNeeded()
        self.backgroundColor = UIColor.clearColor()
        
        let frameSize = self.frame.size
        let arcCenter : CGPoint = CGPoint(x: frameSize.width / 2.0, y: frameSize.height / 2.0)
        let radius : CGFloat = (frameSize.width - 10)/2
        let startAngel : CGFloat = CGFloat(-M_PI_2)
        let endAngel : CGFloat = CGFloat((M_PI * 2.0) - M_PI_2)
        
        self.constants.label = UILabel(frame: CGRectMake(0,0,110,200))
        self.constants.label.attributedText = self.text
        self.constants.label.textColor = self.textColor
        self.constants.label.textAlignment = .Center
        self.constants.label.center = arcCenter
        self.constants.label.numberOfLines = 0
        self.constants.label.lineBreakMode = .ByWordWrapping
//        self.constants.label.font = UIFont.systemFontOfSize(30)
        
        /**Draw the HigherCircle**/
        self.constants.higherCircle.path = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: startAngel, endAngle: endAngel , clockwise: true).CGPath
        self.constants.higherCircle.lineWidth = self.strokeWidth
        self.constants.higherCircle.strokeStart = 0.0
        self.constants.higherCircle.strokeEnd = self.progress
        self.constants.higherCircle.speed = 0.1
        self.constants.higherCircle.lineCap = self.roundCap == true ? kCALineCapRound  : kCALineCapButt
        self.constants.higherCircle.fillColor = self.centerFillColor.CGColor
        self.constants.higherCircle.strokeColor = self.strokeColor.CGColor
        self.animateCircle(0.2, sender : true)
        
        
        if self.drawlowerCirlce == true {
            /**Draw the LowerCirlcle**/
            self.constants.lowerCircle.path = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: startAngel, endAngle: endAngel , clockwise: true).CGPath
            self.constants.lowerCircle.lineWidth = self.lowerCirlceStrokeWidth
                self.constants.lowerCircle.strokeStart = 0.0
            self.constants.lowerCircle.strokeEnd = self.lowerCirlceProgress
            self.constants.lowerCircle.speed = 0.1
            self.constants.lowerCircle.lineCap = self.roundCap == true ? kCALineCapRound  : kCALineCapButt
            self.constants.lowerCircle.fillColor = UIColor.clearColor().CGColor
            self.constants.lowerCircle.strokeColor = self.lowerCirlceStrokeColor.CGColor
            self.layer.addSublayer(self.constants.lowerCircle)
        }
            self.layer.addSublayer(self.constants.higherCircle)
        self.addSubview(self.constants.label)
        
        
    }
    
    func animateCircle(duration: NSTimeInterval, sender : Bool) {
        let animation = CABasicAnimation(keyPath: "strokeEnd")
                animation.duration = duration
                animation.fromValue = 0
        animation.toValue = sender == true ? self.progress : self.lowerCirlceProgress
                animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        
        self.constants.higherCircle.strokeEnd =  self.progress
        
        self.constants.higherCircle.addAnimation(animation, forKey: "animateHigherCircle")

    }
}