 
 import UIKit
 
-/**
-    ABProgressButton provides functionality for creating custom animation of UIButton during processing some task.
-    Should be created in IB as custom class UIButton to prevent title of the button appearing.
-*/
+///    ABProgressButton provides functionality for creating custom animation of UIButton during processing some task.
+///    Should be created in IB as custom class UIButton to prevent title of the button appearing.
+///    Provides mechanism for color inverting on highlitening and using tintColor for textColor(default behavior for system button)
 @IBDesignable @objc class ABProgressButton: UIButton {
-    
+
+    /// **UI configuration. IBInspectable.** Allows change corner radius on default button state. Has default value of 5.0
     @IBInspectable var cornerRadius: CGFloat = 5.0
+
+    
+    /// **UI configuration. IBInspectable.** Allows change border width on default button state. Has default value of 3.0
     @IBInspectable var borderWidth: CGFloat = 3.0
+    
+    /// **UI configuration. IBInspectable.** Allows change border color on default button state. Has default value of control tintColor
     @IBInspectable lazy var borderColor: UIColor = {
         return self.tintColor
     }()
+    
+    /// **UI configuration. IBInspectable.** Allows change circle radius on processing button state. Has default value of 20.0
+    @IBInspectable var circleRadius: CGFloat = 20.0
+    
+    /// **UI configuration. IBInspectable.** Allows change circle border width on processing button state. Has default value of 3.0
+    @IBInspectable var circleBorderWidth: CGFloat = 3.0
+    
+    /// **UI configuration. IBInspectable.** Allows change circle border color on processing button state. Has default value of control tintColor
     @IBInspectable lazy var circleBorderColor: UIColor = {
         return self.tintColor
     }()
-    @IBInspectable var circleBorderWidth: CGFloat = 3.0
-    @IBInspectable var circleRadius: CGFloat = 20.0
+    
+    /// **UI configuration. IBInspectable.** Allows change circle background color on processing button state. Has default value of UIColor.whiteColor()
+    @IBInspectable var circleBackgroundColor: UIColor = UIColor.whiteColor()
+    
+    /// **UI configuration. IBInspectable.** Allows change circle cut angle on processing button state. 
+    /// Should have value between 0 and 360 degrees. Has default value of 45 degrees
     @IBInspectable var circleCutAngle: CGFloat = 45.0
-    @IBInspectable var circleBackgroundColor: UIColor = UIColor.whiteColor()
-    private lazy var shapeBackgroundColor: UIColor = {
-        return self.backgroundColor ?? self.circleBackgroundColor
-    }()
-
+    
+    
+    /// **UI configuration. IBInspectable.** 
+    /// If true, colors of content and background will be inverted on button highlightening. Image should be used as template for correct image color inverting.
+    /// If false you should use default mechanism for text and images highlitening. 
+    /// Has default value of true
+    @IBInspectable var invertColorsOnHighlight: Bool = true
+    
+    /// **UI configuration. IBInspectable.** 
+    /// If true, tintColor whould be used for text, else value from UIButton.textColorForState() would be used.
+    /// Has default value of true
+    @IBInspectable var useTintColorForText: Bool = true
+
+    
+    /** **Buttons states enum**
+    - .Default: Default state of button with border line.
+    - .Progressing: State of button without content, button has the form of circle with cut angle with rotation animation. 
+    */
     enum State {
         case Default, Progressing
     }
+    
+    /** **State changing**
+    Should be used to change state of button from one state to another. All transitions between states would be animated. To update progress indicator use `progress` value
+    */
     var progressState: State = .Default {
         didSet {
             if(progressState == .Default) { self.updateToDefaultStateAnimated(true)}

            self.shapeLayer.addAnimation(secondStepAnimation, forKey: "second step animation")
            
            let delay = secondStepAnimationTime + firstStepAnimationTime
            UIView.animateWithDuration(textAppearingAnimationTime, delay:delay, options: UIViewAnimationOptions.BeginFromCurrentState,
                animations: { () -> Void in
                    self.titleLabel?.alpha = 1.0
                }) { (complete) -> Void in }
        }
    }
    private func animateProgressingState(layer: CAShapeLayer) {

        let timeSincePause = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
        layer.beginTime = timeSincePause
    }

// MARK : Content images hiding
// The only available way to make images hide is to set the object to nil
// If you now any way else please help me here
    
    private var imageForNormalState: UIImage?
    private var imageForHighlitedState: UIImage?
    private var imageForDisabledState: UIImage?
    
    private func hideContentImage() {
        self.imageForNormalState = self.imageForState(UIControlState.Normal)
        self.setImage(UIImage(), forState: UIControlState.Normal)
        self.imageForHighlitedState = self.imageForState(UIControlState.Highlighted)
        self.setImage(UIImage(), forState: UIControlState.Highlighted)
        self.imageForDisabledState = self.imageForState(UIControlState.Disabled)
        self.setImage(UIImage(), forState: UIControlState.Disabled)
    }
    
    private func showContentImage() {
        if self.imageForNormalState != nil {
            self.setImage(self.imageForNormalState, forState: UIControlState.Normal);
            self.imageForNormalState = nil
        }
        if self.imageForHighlitedState != nil {
            self.setImage(self.imageForHighlitedState, forState: UIControlState.Highlighted)
            self.imageForHighlitedState = nil
        }
        if self.imageForDisabledState != nil {
            self.setImage(self.imageForDisabledState, forState: UIControlState.Disabled)
            self.imageForDisabledState = nil
        }
    }
}
