 //
 //  Created by Developer on 2016/11/7.
 //  Copyright © 2016年 Beijing Haitao International Travel Service Co., Ltd. All rights reserved.
-//
+//  a swich for  password security enter
 
 import UIKit
 
+let rcSwitchFontSize: CGFloat = 12
+
 class RCSwitch: UIControl, CAAnimationDelegate {
 
-    let duration: CFTimeInterval = 0.2
-    let fontSize: CGFloat = 12
-    var onText: String!
-    private var onTextFrame: CGRect!
+    // the text showing when isOn == true
+    var onText: String = "" {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+
+    // the text showing when isOn == false, default show ...
+    var offText: String = "" {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+
+    //  shadowLayer show on superview  or not, default is false
+    var isShadowShowing: Bool = false
+
+    //  onText background color
+    var onColor: UIColor =  UIColor(red:243/255.0, green: 68/255.0, blue:107/255.0, alpha: 1) {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+
+    //  offText background color
+    var offColor: UIColor = UIColor.white {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    // textlayer show onText string
     private var onTextLayer: CATextLayer!
-
+    // textlayer show offText string
+    private var offTextLayer: CATextLayer?
+    // switch max length
     private let maxLength: CGFloat = 60
-    private var lightLayer: CAShapeLayer!
-    private var whiteLayer: CAShapeLayer!
+    // thumb offset the border
+    private let margin: CGFloat = 2
+    // leftcorner / right corner radius
+    private let minRadius:CGFloat = 3
+    // thumb radius  #
+    private var thumbRadius: CGFloat = 0
+    // animation duration
+    private let rcswitchAnimationDuration: CFTimeInterval = 0.2
+    // on  backgound layer fill with onColor
+    private var onLayer: CAShapeLayer!
+    // off background layer fill with offColor
+    private var offLayer: CAShapeLayer!
+    // round thumb layer
     private var thumbLayer: CAShapeLayer!
-    private let margin: CGFloat = 2
-
-
+    // switch shadow color
+    private var shadowLayer: CAShapeLayer!
+    // if offText is not nil, offLayer show three dot
+    private var dots = [CAShapeLayer]()
+    // path of whole bounds
     private var fullPath: CGPath!
-    private var thumbRadius: CGFloat! 
+    // left thumb center when isOn == true
     private var leftArcCenter: CGPoint!
+    // right thumb center when isOn == false
     private var rightArcCenter: CGPoint!
-    private let minRadius:CGFloat = 3
-    private var thumbAnimation: CABasicAnimation!
-    private var lightAnimation: CABasicAnimation!
-    private var whiteAnimation: CABasicAnimation!
-
-    var shadowLayer: CAShapeLayer!
-    // 高亮颜色
-    var lightColor: UIColor =  UIColor(red:243/255.0, green: 68/255.0, blue:107/255.0, alpha: 1)
-
 
     var isOn: Bool = false {
         didSet {

        self.backgroundColor = .white
        layer.masksToBounds = true
        layer.cornerRadius  = frame.height/2
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

            let lightMinPath = UIBezierPath(arcCenter: leftArcCenter, radius: minRadius , startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true).cgPath
            let rightMinPath = UIBezierPath(arcCenter: rightArcCenter, radius: minRadius , startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true).cgPath
            if self.isOn {
                let thumbAnimation = pathAnimation(from: leftPath, to: rightPath)
                thumbAnimation.delegate = self
                thumbLayer.add(thumbAnimation, forKey: "thumb")
                let onAnimation = pathAnimation(from: lightMinPath, to: fullPath)
                onLayer.add(onAnimation, forKey: "light")
                let offAnimation = pathAnimation(from: fullPath, to: rightMinPath)
                offLayer.add(offAnimation, forKey: "white")
                onTextLayer.isHidden = false
                offTextLayer?.isHidden = true
            } else {
                let thumbAnimation = pathAnimation(from: rightPath, to: leftPath)
                thumbAnimation.delegate = self
                thumbLayer.add(thumbAnimation, forKey: "thumb")
                let onAnimation = pathAnimation(from: fullPath, to: lightMinPath)
                onLayer.add(onAnimation, forKey: "light")
                let offAnimation =  pathAnimation(from: rightMinPath, to: fullPath)
                offLayer.add(offAnimation, forKey: "white")
                onTextLayer.isHidden = true
                offTextLayer?.isHidden = false
            }
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let height: CGFloat = self.frame.height
        let width: CGFloat = self.frame.width
        leftArcCenter = CGPoint(x: height/2, y: height/2)
        rightArcCenter = CGPoint(x: width-height/2, y: height/2)
        thumbRadius = frame.height/2-margin


        onTextLayer.frame = onTextFrame()
        onTextLayer.string = onText

        fullPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: frame.height/2).cgPath

        thumbLayer.path = UIBezierPath(arcCenter: leftArcCenter, radius: thumbRadius, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true).cgPath

        onLayer.path = UIBezierPath(arcCenter: leftArcCenter, radius: minRadius, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true).cgPath

        offLayer.path = UIBezierPath(arcCenter: rightArcCenter, radius: minRadius, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true).cgPath

        if offText != "" {
            if offTextLayer == nil {
                offTextLayer = textLayer()
                offTextLayer!.foregroundColor = UIColor.lightGray.cgColor
                offTextLayer!.isHidden = isOn
                offLayer.addSublayer(offTextLayer!)
            }
            offTextLayer?.frame = offTextFrame()
            offTextLayer?.string = offText
            removeDots()
        } else {
            offTextLayer?.removeFromSuperlayer()
            setDots()
        }

        shadowLayer.frame = self.frame
        shadowLayer.path = fullPath
        self.superview!.layer.insertSublayer(shadowLayer, below: layer)
    }

    // MARK: - init
    override init(frame: CGRect) {
        var rect = frame
        rect.size.width = min(frame.width, maxLength)
