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

import UIKit

class LocationViewCell: UITableViewCell {
    
    // MARK: Properties
    
    static let identifier = "LocationViewCell"
    
    // MARK: Outlets
    
    private lazy var icon = {
        let view = UIImageView()
        
        view.contentMode = .top
        view.tintColor = .red
        
        return view
    }()
    
    private lazy var latitudeTitle = {
        let label = UILabel()
        
        label.font = .preferredFont(forTextStyle: .subheadline)
        label.numberOfLines = 1
        label.text = "• Latitude"
        label.textAlignment = .natural
        label.textColor = .secondaryLabel
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }()
    
    private lazy var latitudeValue = {
        let label = UILabel()
        
        label.font = .preferredFont(forTextStyle: .subheadline)
        label.numberOfLines = 1
        label.textAlignment = .natural
        label.textColor = .secondaryLabel
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }()
    
    private lazy var longitudeTitle = {
        let label = UILabel()
        
        label.font = .preferredFont(forTextStyle: .subheadline)
        label.numberOfLines = 1
        label.text = "• Longitude"
        label.textAlignment = .natural
        label.textColor = .secondaryLabel
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }()
    
    private lazy var longitudeValue = {
        let label = UILabel()
        
        label.font = .preferredFont(forTextStyle: .subheadline)
        label.numberOfLines = 1
        label.textAlignment = .natural
        label.textColor = .secondaryLabel
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }()
    
    private lazy var name = {
        let label = UILabel()
        
        label.font = .preferredFont(forTextStyle: .headline)
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.text = "Untitled"
        label.textAlignment = .natural
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }()
    
    private lazy var stack = {
        let stack = UIStackView()
        
        stack.alignment = .center
        stack.axis = .horizontal
        stack.distribution = .fill
        stack.spacing = 12
        stack.translatesAutoresizingMaskIntoConstraints = false
        
        return stack
    }()
    
    private lazy var stackData = {
        let stack = UIStackView()
        
        stack.alignment = .leading
        stack.axis = .vertical
        stack.distribution = .fill
        stack.spacing = 8
        stack.translatesAutoresizingMaskIntoConstraints = false
        
        return stack
    }()
    
    private lazy var stackCoordinates = {
        let stack = UIStackView()
        
        stack.alignment = .leading
        stack.axis = .vertical
        stack.distribution = .fill
        stack.spacing = 2
        stack.translatesAutoresizingMaskIntoConstraints = false
        
        return stack
    }()
    
    private lazy var stackLatitude = {
        let stack = UIStackView()
        
        stack.alignment = .leading
        stack.axis = .horizontal
        stack.distribution = .fill
        stack.spacing = 4
        stack.translatesAutoresizingMaskIntoConstraints = false
        
        return stack
    }()
    
    private lazy var stackLongitude = {
        let stack = UIStackView()
        
        stack.alignment = .leading
        stack.axis = .horizontal
        stack.distribution = .fill
        stack.spacing = 4
        stack.translatesAutoresizingMaskIntoConstraints = false
        
        return stack
    }()
    
    // MARK: Initialisers
    
    override init(
        style: UITableViewCell.CellStyle,
        reuseIdentifier: String?
    ) {
        super.init(
            style: style,
            reuseIdentifier: reuseIdentifier
        )
        
        setupCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: Functions
    
    func update(
        iconName: String,
        name: String?,
        latitude: Float,
        longitude: Float
    ) {
        self.icon.image = .init(systemName: iconName)
        self.name.text = name ?? "Untitled"
        self.latitudeValue.text = "\(latitude)"
        self.longitudeValue.text = "\(longitude)"
    }
    
}

// MARK: - Helpers

private extension LocationViewCell {
    
    // MARK: Functions
    
    func setupCell() {
        accessoryType = .disclosureIndicator
        backgroundColor = .clear
        
        addSubview(stack)
        
        stack.addArrangedSubview(icon)
        stack.addArrangedSubview(stackData)
        
        stackData.addArrangedSubview(name)
        stackData.addArrangedSubview(stackCoordinates)
        
        stackCoordinates.addArrangedSubview(stackLatitude)
        stackCoordinates.addArrangedSubview(stackLongitude)
        
        stackLatitude.addArrangedSubview(latitudeTitle)
        stackLatitude.addArrangedSubview(latitudeValue)
        
        stackLongitude.addArrangedSubview(longitudeTitle)
        stackLongitude.addArrangedSubview(longitudeValue)

        NSLayoutConstraint.activate([
            bottomAnchor.constraint(equalTo: stack.bottomAnchor, constant: 8),
            leadingAnchor.constraint(equalTo: stack.leadingAnchor, constant: -20),
            topAnchor.constraint(equalTo: stack.topAnchor, constant: -8),
            trailingAnchor.constraint(equalTo: stack.trailingAnchor),
            icon.bottomAnchor.constraint(equalTo: stack.bottomAnchor),
            icon.topAnchor.constraint(equalTo: stack.topAnchor),
            icon.widthAnchor.constraint(equalToConstant: 24),
        ])
    }

}
