//
//  CustomPickerView.swift
//  Funny_sounds
//
//  Created by 忆海16 on 2024/8/16.
//

import UIKit
import SnapKit

class CustomPickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {

    private let pickerValues = [5, 10,15,20,30,35,45,55,60,70,80,90]
    private var selectedValue: Int = 15
    
    private let pickerView = UIPickerView()
    private let cancelButton = UIButton()
    private let okButton = UIButton()
    
    var onValueSelected: ((Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    
    private func setupView() {
        self.backgroundColor = .white
        self.layer.cornerRadius = 10
        self.clipsToBounds = true
        
        setupPickerView()
        setupButtons()
    }
    
    private func setupPickerView() {
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(pickerView)
        
        NSLayoutConstraint.activate([
            pickerView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            pickerView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
            pickerView.widthAnchor.constraint(equalTo: self.widthAnchor),
            pickerView.heightAnchor.constraint(equalTo: self.heightAnchor, constant: -50) // Adjust for buttons
        ])
        
        pickerView.selectRow(pickerValues.firstIndex(of: selectedValue)!, inComponent: 0, animated: false)
    }
    
    private func setupButtons() {
        
        var topView = UIView()
        topView.backgroundColor = UIColor.hexSting(color: "#4EDB96", alpha: 1)
        self.addSubview(topView)
        
        topView.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.height.equalTo(46)
            make.top.equalTo(self.snp.top)
        }
        
        
        
        cancelButton.setTitle("Cancel", for: .normal)
        cancelButton.titleLabel?.font = UIFont(name: "PaytoneOne-Regular", size: 18)
        cancelButton.setTitleColor(.white, for: .normal)
        cancelButton.translatesAutoresizingMaskIntoConstraints = false
        topView.addSubview(cancelButton)
        
        cancelButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
        
        okButton.setTitle("OK", for: .normal)
        okButton.setTitleColor(.white, for: .normal)
        okButton.titleLabel?.font = UIFont(name: "PaytoneOne-Regular", size: 18)
        okButton.translatesAutoresizingMaskIntoConstraints = false
        topView.addSubview(okButton)
        
        okButton.addTarget(self, action: #selector(okButtonTapped), for: .touchUpInside)
        
        
        cancelButton.snp.makeConstraints { make in
            make.left.equalTo(topView.snp.left).offset(20)
            make.centerY.equalTo(topView)
           
        }
        
        okButton.snp.makeConstraints { make in
            make.right.equalTo(topView.snp.right).offset(-20)
            make.centerY.equalTo(topView)
           
        }
        
    }
    
    @objc private func cancelButtonTapped() {
        dismissView()
    }
    
    @objc private func okButtonTapped() {
        onValueSelected?(selectedValue)
        dismissView()
    }
    
    private func dismissView() {
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0
            self.transform = CGAffineTransform(translationX: 0, y: self.frame.height)
        }) { _ in
            self.removeFromSuperview()
        }
    }
    
    func showInView(_ parentView: UIView) {
        self.frame = CGRect(x: 0, y: parentView.bounds.height, width: parentView.bounds.width, height: 262)
        parentView.addSubview(self)
        
        UIView.animate(withDuration: 0.3) {
            self.alpha = 1
            self.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
        }
    }
    
    // Picker View Delegate & Data Source methods
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerValues.count
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "\(pickerValues[row])s"
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = BorderedLabel()
        label.textAlignment = .center
        label.text = "\(pickerValues[row]) s"
        
        if row == pickerView.selectedRow(inComponent: component) {
            label.font = UIFont.systemFont(ofSize: 24)
            label.textColor = UIColor.black
        } else {
            label.font = UIFont.systemFont(ofSize: 18)
            label.textColor = UIColor.white
        }
        
        return label
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedValue = pickerValues[row]
        pickerView.reloadAllComponents()
    }

    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
}

