//
//  PQFollowButton.swift
//  PQSpeed
//
//  Created by lieyunye on 2020/6/18.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import Foundation
import SnapKit

public class PQFollowButton: UIButton {
    public  let bgLayer = CAShapeLayer()

    public  var attenBtn: UIButton = {
        let attenBtn = UIButton(type: .custom)
        attenBtn.isUserInteractionEnabled = false
        attenBtn.setTitle("", for: .selected)
        attenBtn.setImage(UIImage.init().BF_Image(named: "icon_oder"), for: .selected)
        attenBtn.setTitle("+", for: .normal)
        attenBtn.setImage(nil, for: .normal)
        attenBtn.setTitleColor(UIColor.white, for: .normal)
        attenBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        attenBtn.layer.cornerRadius = 10
//        attenBtn.layer.masksToBounds = true
        attenBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#EE0051")
        attenBtn.tag = 2
        attenBtn.titleEdgeInsets = UIEdgeInsets(top: -3, left: 0, bottom: 0, right: 0)
        return attenBtn
    }()

    override public  init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(attenBtn)
    }

    required public  init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func layoutSubviews() {
        super.layoutSubviews()
        attenBtn.snp.makeConstraints { make in
            make.edges.equalTo(self).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }

    public  func reset() {
        attenBtn.isHidden = false
        attenBtn.layer.removeAllAnimations()
        bgLayer.removeAllAnimations()
        bgLayer.sublayers?.forEach {
            $0.removeAllAnimations()
            $0.removeFromSuperlayer()
        }
        bgLayer.removeFromSuperlayer()
    }

    public  func start() {
        let bounds = self.bounds

        bgLayer.frame = bounds
        bgLayer.cornerRadius = bounds.height / 2.0
        bgLayer.masksToBounds = true
        layer.addSublayer(bgLayer)

        let bgScale = CABasicAnimation(keyPath: "transform.scale")
        bgScale.fromValue = 1
        bgScale.toValue = 1.2
        bgScale.duration = 0.2
        attenBtn.layer.add(bgScale, forKey: nil)

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
            self.attenBtn.isHidden = true
            let opacity = CAKeyframeAnimation(keyPath: "backgroundColor")
            opacity.values = [UIColor.red.cgColor, UIColor.white.cgColor]
            opacity.duration = 0.5
            opacity.fillMode = .forwards
            opacity.isRemovedOnCompletion = false
            self.bgLayer.add(opacity, forKey: nil)

            let bgLayer1 = CAShapeLayer()
            let frame = CGRect(x: (bounds.size.width - bounds.size.height) / 2.0, y: 0, width: bounds.size.height, height: bounds.size.height)
            bgLayer1.frame = frame
            self.bgLayer.addSublayer(bgLayer1)

            let linePath = UIBezierPath()

            let radius: CGFloat = frame.size.width / 2.0
            linePath.move(to: CGPoint(x: radius * 0.45, y: radius * 1.0))
            linePath.addLine(to: CGPoint(x: radius * 0.84, y: radius * 1.32))
            linePath.addLine(to: CGPoint(x: radius * 1.48, y: radius * 0.68))

            let layer = CAShapeLayer()

            layer.path = linePath.cgPath
            layer.fillColor = UIColor.clear.cgColor
            layer.strokeColor = UIColor.hexColor(hexadecimal: "#EE0051").cgColor
            layer.lineWidth = 2.5
            bgLayer1.addSublayer(layer)

            let animation = CABasicAnimation()
            animation.duration = 0.5
            animation.keyPath = "strokeEnd"
            animation.fromValue = 0
            animation.toValue = 1
            animation.fillMode = .forwards
            animation.isRemovedOnCompletion = false

            layer.add(animation, forKey: "strokeEnd")

            let scale = CABasicAnimation(keyPath: "transform.scale")
            scale.fromValue = 1
            scale.toValue = 0
            scale.duration = 0.2
            scale.beginTime = CACurrentMediaTime() + 1.5
            scale.fillMode = .forwards
            scale.isRemovedOnCompletion = false
            self.bgLayer.add(scale, forKey: nil)
        }
    }
}
