//
//  ProfileNoContentCollectionViewCell.swift
//  Tusker
//
//  Created by Shadowfacts on 2/22/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import UIKit

class ProfileNoContentCollectionViewCell: UICollectionViewListCell {
    
    weak var delegate: TuskerNavigationDelegate?
    private var accountURL: URL?
    
    private var button: UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        var config = UIBackgroundConfiguration.listPlainCell()
        config.backgroundColor = .appBackground
        backgroundConfiguration = config
        
        let title = UILabel()
        title.text = "There's nothing here"
        title.adjustsFontForContentSizeCategory = true
        title.font = .preferredFont(forTextStyle: .headline)
        title.numberOfLines = 0
        title.textAlignment = .center
        title.textColor = .secondaryLabel
        
        let body = UILabel()
        body.text = "Your instance may not show all of the posts from accounts on other instances."
        body.adjustsFontForContentSizeCategory = true
        body.font = .preferredFont(forTextStyle: .body)
        body.numberOfLines = 0
        body.textAlignment = .center
        body.textColor = .secondaryLabel
        
        button = UIButton(configuration: .plain(), primaryAction: UIAction(handler: { [unowned self] _ in
            if let delegate = self.delegate,
               let accountURL = self.accountURL {
                delegate.selected(url: accountURL)
            }
        }))
        
        let stack = UIStackView(arrangedSubviews: [
            title,
            body,
            button,
        ])
        stack.axis = .vertical
        stack.alignment = .center
        stack.spacing = 4
        stack.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(stack)
        NSLayoutConstraint.activate([
            stack.leadingAnchor.constraint(equalToSystemSpacingAfter: contentView.leadingAnchor, multiplier: 1),
            contentView.trailingAnchor.constraint(equalToSystemSpacingAfter: stack.trailingAnchor, multiplier: 1),
            stack.topAnchor.constraint(equalToSystemSpacingBelow: contentView.topAnchor, multiplier: 1),
            contentView.bottomAnchor.constraint(equalToSystemSpacingBelow: stack.bottomAnchor, multiplier: 1),
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(accountURL: URL) {
        self.accountURL = accountURL
        
        var title: AttributedString = "View on "
        var host = AttributedString(accountURL.host!)
        host.font = .preferredFont(forTextStyle: .body).withTraits(.traitBold)
        title += host
        
        var config = UIButton.Configuration.plain()
        config.attributedTitle = title
        config.image = UIImage(systemName: "safari")
        config.imagePadding = 4
        button.configuration = config
    }
    
}
