//
//  PublicTimelineDescriptionCollectionViewCell.swift
//  Tusker
//
//  Created by Shadowfacts on 10/1/22.
//  Copyright © 2022 Shadowfacts. All rights reserved.
//

import UIKit

class PublicTimelineDescriptionCollectionViewCell: UICollectionViewCell {
    
    weak var mastodonController: MastodonController!
    
    var local = false {
        didSet {
            updateLabel()
        }
    }
    var didDismiss: (() -> Void)?
    
    private let label = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .tintColor
        label.font = .preferredFont(forTextStyle: .body)
        label.numberOfLines = 0
        label.textColor = .white
        label.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(label)
        NSLayoutConstraint.activate([
            label.leadingAnchor.constraint(equalToSystemSpacingAfter: contentView.leadingAnchor, multiplier: 1),
            contentView.trailingAnchor.constraint(equalToSystemSpacingAfter: label.trailingAnchor, multiplier: 1),
            label.topAnchor.constraint(equalToSystemSpacingBelow: contentView.topAnchor, multiplier: 1),
            contentView.bottomAnchor.constraint(equalToSystemSpacingBelow: label.bottomAnchor, multiplier: 1),
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func updateLabel() {
        let str = NSMutableAttributedString()
        let instanceStr = NSAttributedString(string: mastodonController.instanceURL.host!, attributes: [
            .font: UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).withSymbolicTraits(.traitBold)!, size: 0)
        ])
        if local {
            str.append(NSAttributedString(string: "The local timeline shows public posts from only "))
            str.append(instanceStr)
            str.append(NSAttributedString(string: "."))
        } else {
            str.append(NSAttributedString(string: "The federated timeline shows public posts from all users that "))
            str.append(instanceStr)
            str.append(NSAttributedString(string: " knows about."))
        }
        label.attributedText = str
    }
    
}
