//
//  CircularProgressBarView.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/7/10.
//

import Foundation
import UIKit

class CircularProgressBarView: UIView {

    private var progressLayer = CAShapeLayer()
    private var trackLayer = CAShapeLayer()

    var progressColor: UIColor = .blue {
        didSet {
            progressLayer.strokeColor = progressColor.cgColor
        }
    }
    
    var trackColor: UIColor = .lightGray {
        didSet {
            trackLayer.strokeColor = trackColor.cgColor
        }
    }

    var progress: CGFloat = 0 {
        didSet {
            progressLayer.strokeEnd = progress
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupLayers()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupLayers()
    }
    
    private func setupLayers() {
        let circularPath = UIBezierPath(arcCenter: center, radius: bounds.width / 2, startAngle: -CGFloat.pi / 2, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
        
        trackLayer.path = circularPath.cgPath
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.strokeColor = trackColor.cgColor
        trackLayer.lineWidth = 10
        trackLayer.lineCap = .round
        layer.addSublayer(trackLayer)
        
        progressLayer.path = circularPath.cgPath
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = progressColor.cgColor
        progressLayer.lineWidth = 10
        progressLayer.strokeEnd = progress
        progressLayer.lineCap = .round
        layer.addSublayer(progressLayer)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        trackLayer.frame = bounds
        progressLayer.frame = bounds
        setupLayers()
    }
}

func getTodayWeekday() -> String {
    // 获取当前日期
    let currentDate = Date()

    // 获取当前日历
    let calendar = Calendar.current

    // 获取当前日期的星期几（1表示星期日，2表示星期一，以此类推）
    let weekday = calendar.component(.weekday, from: currentDate)

    // 将星期几转换为中文
    let weekdays = ["日", "一", "二", "三", "四", "五", "六"]
    let weekdayString = weekdays[weekday - 1]

    return "\(weekdayString)"
}

func getImageName(for value: Int) -> String {
    switch value {
    case 1...15:
        return "15"
    case 16...25:
        return "25"
    case 26...45:
        return "45"
    case 46...65:
        return "65"
    case 66...85:
        return "85"
    case 86...99:
        return "90"
    case 100...10000000:
        return "100"
    default:
        return "0"
    }
}
