//
//  LoadingSpinnerView.swift
//  Locations
//
//  Created by Javier Cicchelli on 12/04/2023.
//  Copyright © 2023 Röck+Cöde. All rights reserved.
//

import UIKit

class LoadingSpinnerView: UIView {
    
    // MARK: Outlets
    
    private lazy var stack = {
        let stack = UIStackView()

        stack.alignment = .center
        stack.axis = .vertical
        stack.distribution = .fill
        stack.spacing = 8
        stack.translatesAutoresizingMaskIntoConstraints = false
        
        return stack
    }()
    
    private lazy var spinner = {
        let activity = UIActivityIndicatorView(style: .large)
        
        activity.translatesAutoresizingMaskIntoConstraints = false
        
        activity.startAnimating()
        
        return activity
    }()
    
    private lazy var title = {
        let label = UILabel()

        label.font = .preferredFont(forTextStyle: .headline)
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.text = "Loading..."
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }()
    
    // MARK: Initialisers
    
    init() {
        super.init(frame: .zero)
        
        setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: - Helpers

private extension LoadingSpinnerView {
    
    // MARK: Functions
    
    func setupView() {
        backgroundColor = .clear
        translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(stack)
        
        stack.addArrangedSubview(spinner)
        stack.addArrangedSubview(title)

        NSLayoutConstraint.activate([
            topAnchor.constraint(equalTo: stack.topAnchor),
            leadingAnchor.constraint(equalTo: stack.leadingAnchor),
            trailingAnchor.constraint(equalTo: stack.trailingAnchor),
            bottomAnchor.constraint(equalTo: stack.bottomAnchor),
        ])
    }

}
