//
//  ageView.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/7/9.

import UIKit

class AgePickerView: UIView {
    
    private let pickerView = UIPickerView()
    private let ageRange = Array(0...100)
    private let leftImageView = UIImageView(image: UIImage(named: "leftImage"))
    private let rightImageView = UIImageView(image: UIImage(named: "rightImage"))
    
    var selectedAge: Int = 0 {
        didSet {
            pickerView.selectRow(selectedAge, inComponent: 0, animated: true)
        }
    }
    
    var onAgeSelected: ((Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupPickerView()
        setupImageViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupPickerView()
        setupImageViews()
    }
    
    private func setupPickerView() {
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        pickerView.delegate = self
        pickerView.dataSource = self
        addSubview(pickerView)
        
        NSLayoutConstraint.activate([
            pickerView.leadingAnchor.constraint(equalTo: leadingAnchor),
            pickerView.trailingAnchor.constraint(equalTo: trailingAnchor),
            pickerView.topAnchor.constraint(equalTo: topAnchor),
            pickerView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }
    
    private func setupImageViews() {
        leftImageView.translatesAutoresizingMaskIntoConstraints = false
        rightImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(leftImageView)
        addSubview(rightImageView)
        
        NSLayoutConstraint.activate([
            leftImageView.centerYAnchor.constraint(equalTo: pickerView.centerYAnchor),
            leftImageView.trailingAnchor.constraint(equalTo: pickerView.centerXAnchor, constant: -60),
            leftImageView.widthAnchor.constraint(equalToConstant: 18),
            leftImageView.heightAnchor.constraint(equalToConstant: 18),
            
            rightImageView.centerYAnchor.constraint(equalTo: pickerView.centerYAnchor),
            rightImageView.leadingAnchor.constraint(equalTo: pickerView.centerXAnchor, constant: 60),
            rightImageView.widthAnchor.constraint(equalToConstant: 18),
            rightImageView.heightAnchor.constraint(equalToConstant: 18)
        ])
    }
}

extension AgePickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return ageRange.count
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 60
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = UILabel()
        label.text = "\(ageRange[row])"
        label.textAlignment = .center
        
        if row == pickerView.selectedRow(inComponent: 0) {
            label.font = UIFont.boldSystemFont(ofSize: 36)
            label.textColor = .black
        } else {
            label.font = UIFont.systemFont(ofSize: 36)
            label.textColor = .gray
        }
        
        return label
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedAge = row
        pickerView.reloadAllComponents()
        onAgeSelected?(selectedAge)
    }
}


