import UIKit

/// registration header view delegate
protocol RegistraceHeaderViewDelegate: AnyObject {
    func yearTap()
}

/// registration header view
class RegistraceHeaderView: UIView {
    weak var delegate: RegistraceHeaderViewDelegate?
    
    let title: UILabel = {
        let view = UILabel()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.font = UIFont.systemFont(ofSize: 16)
        view.textColor = scoutBlue
        view.textAlignment = .center
        view.text = "Rok registrace"
        return view
    }()
    
    let subtitle: UILabel = {
        let view = UILabel()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.font = UIFont.systemFont(ofSize: 16)
        view.textColor = .black
        view.textAlignment = .center
        return view
    }()
    
    private let icon: UIImageView = {
        let view = UIImageView()
        view.translatesAutoresizingMaskIntoConstraints = false
        let icon = UIImage(named: "arrowDown")!
        view.image = icon.withTintColor(scoutOrange!)
        view.isUserInteractionEnabled = true
        return view
    }()
    
    private let separator: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        view.translatesAutoresizingMaskIntoConstraints = false
        view.heightAnchor.constraint(equalToConstant: 1).isActive = true
        return view
    }()
    
    /// initialization function
    /// - Parameter frame: frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        icon.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(yearTap)))
        addSubview(title)
        addSubview(subtitle)
        addSubview(icon)
        addSubview(separator)
        NSLayoutConstraint.activate([
            title.topAnchor.constraint(equalTo: topAnchor, constant: 12),
            title.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
            
            icon.topAnchor.constraint(equalTo: topAnchor, constant: 4),
            icon.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -4),
            
            subtitle.topAnchor.constraint(equalTo: topAnchor, constant: 12),
            subtitle.trailingAnchor.constraint(equalTo: icon.leadingAnchor),

            separator.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
            separator.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12),
            separator.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// function that gets called when user taps on year
    @objc private func yearTap() {
        delegate?.yearTap()
    }
}
