


//
//  MLSliderRuler.swift
//  MLSilderRulerView
//
//  Created by Macro on 2018/1/25.
//  Copyright © 2018年 CodeMarco. All rights reserved.
//

import UIKit

protocol MLSliderRulerViewDelegate: AnyObject {
    func sliderRulerView(ruler: UIView, rulervalue: CGFloat)
}

class MLSliderRulerView: UIView {
    
    weak var delegate: MLSliderRulerViewDelegate?
    
    var valueLabelFont = UIFont.boldSystemFont(ofSize: 14) {
        didSet {
            valueLabel.font = valueLabelFont
        }
    }
    
    var rulerHeight: CGFloat = 50.0 {
        didSet {
            rulerScrollView.frame = CGRect(x: 0, y: frame.size.height - rulerHeight, width: frame.size.width, height: rulerHeight)
            rulerScrollView.drawRuler()
        }
    }
    
    public var rulerScrollView = MLSliderRulerScrollView()
    public let markLine = CALayer()
    public let valueLabel = UILabel()
    public let valueImageV = UIImageView()
    public let TriangleimagV = UIImageView()
    
    
//    var rulerValue: CGFloat {
//         return rulerScrollView.rulerValue
//     }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        rulerScrollView.frame = CGRect(x: 0, y: frame.size.height - rulerHeight, width: frame.size.width, height: rulerHeight)
        rulerScrollView.showsHorizontalScrollIndicator = false
        rulerScrollView.delegate = self
        addSubview(rulerScrollView)

        
        valueImageV.image = UIImage(named: "valueImagV")
        valueImageV.frame = CGRect(x: center.x - 65, y: rulerScrollView.frame.minY - 60, width: 130, height: 62)
        addSubview(valueImageV)
          
        
        TriangleimagV.image = UIImage(named: "Triangle")
        TriangleimagV.frame = CGRect(x: center.x - 11, y: rulerScrollView.frame.minY + 50, width: 22, height: 12)
        addSubview(TriangleimagV)
        
        valueLabel.font = UIFont.boldSystemFont(ofSize: 24)
        valueLabel.textAlignment = .center
        valueLabel.textColor = UIColor.hexSting(color: "#62BFFF", alpha: 1)
        valueLabel.frame = CGRect(x: 0, y: rulerScrollView.frame.minY - 42, width: bounds.width, height: 20)
        valueLabel.text = "160 ml"
        addSubview(valueLabel)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        rulerScrollView.drawRuler()
    }
    
    func animationRebound() {
        // Method intentionally left empty to remove snapping effect
    }
}

extension MLSliderRulerView: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offSetX = scrollView.contentOffset.x + bounds.size.width / 2 - DISTANCELEFTANDRIGHT
              let ruleValue = (offSetX / DISTANCEVALUE) * rulerScrollView.rulerAverage
              let valuetext = "\(round(ruleValue) + rulerScrollView.minValue) ml"
              if valuetext != valueLabel.text {
                  valueLabel.text = valuetext
              }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        // animationRebound() removed to prevent snapping
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        // animationRebound() removed to prevent snapping
    }
}
