//
//  Player_Slider.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/3/29.
//

import UIKit

///音量控制Slider
class MP_PlayerSlider: UISlider {
    //滑块图片
    var thumbImage:UIImage!
    //滑轨槽高度
    var trackHeight: CGFloat = 5
    //进度渐变色
    var minTrackColors: [UIColor]!
    //槽位渐变色
    var maxTrackColors:[UIColor]!
    //进度渐变色定位列表(与渐变色数量保持一致)
    var minTrackLocations:[CGFloat]!
    //槽位渐变色定位列表(与渐变色数量保持一致)
    var maxTrackLocations:[CGFloat]!

    //初始化
    init(frame:CGRect, trackHeight: CGFloat, minTrackColors: [UIColor]!, maxTrackColors: [UIColor]!, minTrackLocations: [CGFloat]!, maxTrackLocations: [CGFloat]!) {
        super.init(frame: frame)
        self.trackHeight = trackHeight
        self.minTrackColors = minTrackColors
        self.maxTrackColors = maxTrackColors
        self.minTrackLocations = minTrackLocations
        self.maxTrackLocations = maxTrackLocations
        setUpLayout()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        trackHeight = bounds.size.height
        setUpLayout()
    }
    private func setUpLayout() {
        layer.masksToBounds = false
        
    }
    //重写绘制方法
    override func draw(_ rect: CGRect) {
        //设置滑块图片
        if thumbImage != nil {
            setThumbImage(thumbImage, for: .normal)
        }
        //设置进度图片
        let minTrackImg = makeTrackImage(rect: rect, colors: minTrackColors, locations: minTrackLocations)
        setMinimumTrackImage(minTrackImg, for: .normal)
        // 设置槽位
        let maxTrackImg = makeTrackImage(rect: rect, colors: maxTrackColors, locations: maxTrackLocations)
        setMaximumTrackImage(maxTrackImg, for: .normal)
    }
    
    /// 滑块设置
    /// - Parameters:
    ///   - rect: 滑块大小
    ///   - color: 滑块颜色
    /// - Returns: 返回的滑块图片
    private func makeThumbImage(rect: CGRect, color:UIColor) -> UIImage {
        let lineWidth: CGFloat = 2
        //开始绘制
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        //填充内部颜色
        color.setFill()
        //曲线路径
        let oval = UIBezierPath(ovalIn: rect)
        //填充路径
        oval.fill()
        //设置路径颜色为白色
        UIColor.white.setStroke()
        //路径宽度
        oval.lineWidth = lineWidth
        oval.stroke()
        //转为图片
        let thumbImg = UIGraphicsGetImageFromCurrentImageContext()!
        //转化结束
        UIGraphicsEndImageContext()
        return thumbImg
    }
    
    
    /// 生成滑轨图片
    /// - Parameters:
    ///   - rect: 滑轨大小
    ///   - colors: 颜色列表
    ///   - locations: 位置分布列表
    /// - Returns: 生成的图片
    private func makeTrackImage(rect: CGRect, colors:[UIColor], locations:[CGFloat]) -> UIImage {
        let rect = CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: self.trackHeight)
        //开始绘制
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        let ctx = UIGraphicsGetCurrentContext()!
        // 创建并设置路径
        let cornerRadius: CGFloat = rect.height * 0.5
        let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
        // 添加路径到图形上下文
        ctx.addPath(path)
        ctx.clip()
        // 使用rgb颜色空间
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        //获取cgcolors
        var cgColors:[CGColor] = []
        colors.forEach { item in
            cgColors.append(item.cgColor)
        }
        // 定义渐变色
        let gradient:CGGradient = CGGradient(colorsSpace: colorSpace, colors: cgColors as CFArray, locations: locations)!
        // 渐变开始位置
        let start = CGPoint(x: self.bounds.minX, y: self.bounds.minY)
        // 渐变结束位置
        let end = CGPoint(x: self.bounds.maxX, y: self.bounds.minY)
        // 绘制渐变
        ctx.drawLinearGradient(gradient, start: start, end: end, options: .drawsBeforeStartLocation)
        //转为图片
        let trackImg = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return trackImg
    }
    // 重写【thumb】显示区域 方法
    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
        let rect = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
        // 设置thumb投影效果
        // (这样设置的投影效果，如果调用setValue(_:animated:)的时候，animated参数为true，会有阴影和thumb不同步的问题，目前还不知到怎么解决)
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: 0)
        self.layer.shadowOpacity = 0.15
        self.layer.shadowRadius = 3
        self.layer.shadowPath = UIBezierPath(cgPath: CGPath(ellipseIn: rect.insetBy(dx: 3, dy: 3), transform: nil)).cgPath
        return rect
    }
    // 重写【track】显示区域 方法
    override func trackRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: 0, y: 0, width: bounds.width, height: self.trackHeight)
    }
}
