//
//  TimePickerViewController.swift
//  Girly
//
//  Created by 100Years on 2025/1/10.
//

class TimePickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    private let pickerView = UIPickerView()
    
    // 数据源：小时和分钟
    private let hours = Array(0...23).map { "\($0) hours" }
    private let minutes = Array(0...59).map { "\($0) min" }
    
    // 回调选中的时间
    var onTimeSelected: ((Int, Int) -> Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .black
        
        // 设置 UIPickerView
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        pickerView.backgroundColor = .black
        
        view.addSubview(pickerView)
        
        // 设置约束
        NSLayoutConstraint.activate([
            pickerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            pickerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            pickerView.widthAnchor.constraint(equalTo: view.widthAnchor),
            pickerView.heightAnchor.constraint(equalToConstant: 200)
        ])
    }
    
    // MARK: - 设置默认选中时间
    func setDefaultTime(hour: Int, minute: Int) {
        let validHour = min(max(hour, 0), 23)  // 确保小时在 0~23 范围内
        let validMinute = min(max(minute, 0), 59)  // 确保分钟在 0~59 范围内
        
        pickerView.selectRow(validHour, inComponent: 0, animated: true)
        pickerView.selectRow(validMinute, inComponent: 1, animated: true)
        
        onTimeSelected?(validHour, validMinute)
    }
    
    // MARK: - UIPickerViewDataSource
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2 // 两列：小时和分钟
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return component == 0 ? hours.count : minutes.count
    }
    
    // MARK: - UIPickerViewDelegate
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return component == 0 ? hours[row] : minutes[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let selectedHour = pickerView.selectedRow(inComponent: 0)
        let selectedMinute = pickerView.selectedRow(inComponent: 1)
        
        // 回调选中的时间
        onTimeSelected?(selectedHour, selectedMinute)
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return pickerView.frame.width / 2
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 48
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = UILabel()
        label.text = component == 0 ? hours[row] : minutes[row]
        label.textColor = .white
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        return label
    }
}
