import Foundation
import UIKit

/// delegate protocol for intro view
protocol IntroViewDelegate: AnyObject {
    func touchSignIn()
}

/// view for introduction
class IntroView: UIView {
    weak var delegate: IntroViewDelegate?
    
    private let button: UIButton = {
        let button = UIButton()
        button.setTitle("Prihlasit se", for: .normal)
        button.setTitleColor(.blue, for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()
    
    private let image: UIImageView = {
        let view = UIImageView(image: UIImage(named: "logo"))
        view.translatesAutoresizingMaskIntoConstraints = false
        view.heightAnchor.constraint(equalToConstant: 200).isActive = true
        view.widthAnchor.constraint(equalToConstant: 200).isActive = true
        return view
    }()
    
    /// initialization function
    /// - Parameter frame: frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(image)
        addSubview(button)
        backgroundColor = .white
        button.addTarget(self, action: #selector(touch), for: .touchUpInside)
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// function for setting up constraints for view elements
    private func setupConstraints() {
        NSLayoutConstraint.activate([
            image.centerYAnchor.constraint(equalTo: centerYAnchor),
            image.centerXAnchor.constraint(equalTo: centerXAnchor),
            button.centerXAnchor.constraint(equalTo: centerXAnchor),
            button.topAnchor.constraint(equalTo: image.bottomAnchor, constant: 12),
            button.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -12)
        ])
    }
    
    /// function that gets called when user touches signin button
    @objc private func touch() {
        delegate?.touchSignIn()
    }
}
