import UIKit

/// unit header view delegate protocol
protocol JednotkaHeaderViewDelegate: AnyObject {
    func osobyTap()
    func registraceTap()
}

/// unit header view
class JednotkaHeaderView: UIView {
    weak var delegate: JednotkaHeaderViewDelegate?
    
    let title: UILabel = {
        let view = UILabel()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.font = UIFont.systemFont(ofSize: 24)
        view.textColor = scoutBlue
        view.textAlignment = .center
        return view
    }()
    
    private let icons: UIStackView = {
        let view = UIStackView()
        view.axis = .horizontal
        view.translatesAutoresizingMaskIntoConstraints = false
        view.spacing = 60
        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)
        addSubview(title)
        let osoby = setupIcons(image: UIImage(named: "people")!, title: "Členové")
        osoby.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(osobyTap)))
        let registrace = setupIcons(image: UIImage(named: "chart")!, title: "Registrace")
        registrace.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(registraceTap)))
        icons.addArrangedSubview(osoby)
        icons.addArrangedSubview(registrace)
        addSubview(icons)
        addSubview(separator)
        NSLayoutConstraint.activate([
            title.topAnchor.constraint(equalTo: topAnchor, constant: 8),
            title.centerXAnchor.constraint(equalTo: centerXAnchor),
            
            icons.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 12),
            icons.centerXAnchor.constraint(equalTo: centerXAnchor),
            
            separator.topAnchor.constraint(equalTo: icons.bottomAnchor, constant: 12),
            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 for setting up stack view
    /// - Parameters:
    ///   - image: image for stack view
    ///   - title: title for stack view
    /// - Returns: configured stack view
    private func setupIcons(image: UIImage, title: String) -> UIStackView {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.alignment = .center
        let icon = UIImageView(image: image.withTintColor(scoutOrange!))
        let label = UILabel()
        label.textColor = scoutOrange
        label.text = title
        label.font = UIFont.systemFont(ofSize: 12)
        stackView.addArrangedSubview(icon)
        stackView.addArrangedSubview(label)
        return stackView
    }
    
    /// function that gets called when user taps on members button
    @objc private func osobyTap() {
        delegate?.osobyTap()
    }
    
    /// function that gets called when user taps on registration button
    @objc private func registraceTap() {
        delegate?.registraceTap()
    }
}
