//
//  ConfirmReblogStatusPreviewView.swift
//  Tusker
//
//  Created by Shadowfacts on 9/17/22.
//  Copyright © 2022 Shadowfacts. All rights reserved.
//

import UIKit

class ConfirmReblogStatusPreviewView: UIView {
    
    private var avatarTask: Task<Void, Error>?
    
    init(status: StatusMO) {
        super.init(frame: .zero)
        
        let hStack = UIStackView()
        hStack.axis = .horizontal
        hStack.spacing = 8
        hStack.alignment = .leading
        hStack.translatesAutoresizingMaskIntoConstraints = false
        addSubview(hStack)
        NSLayoutConstraint.activate([
            hStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
            hStack.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
            hStack.topAnchor.constraint(equalTo: topAnchor),
            hStack.bottomAnchor.constraint(equalTo: bottomAnchor),
        ])
        
        let avatarSize: CGFloat = 30
        let avatarImageView = UIImageView()
        avatarImageView.layer.masksToBounds = true
        avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * avatarSize
        hStack.addArrangedSubview(avatarImageView)
        NSLayoutConstraint.activate([
            avatarImageView.widthAnchor.constraint(equalToConstant: avatarSize),
            avatarImageView.heightAnchor.constraint(equalToConstant: avatarSize),
        ])
        if let avatar = status.account.avatar {
            avatarTask = Task {
                let (_, image) = await ImageCache.avatars.get(avatar)
                try Task.checkCancellation()
                avatarImageView.image = image
            }
        }
        
        let vStack = UIStackView()
        vStack.axis = .vertical
        vStack.spacing = 2
        vStack.alignment = .fill
        vStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
        hStack.addArrangedSubview(vStack)
        
        let displayNameLabel = AccountDisplayNameLabel()
        displayNameLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .caption1).addingAttributes([.traits: [UIFontDescriptor.TraitKey.weight: UIFont.Weight.semibold]]), size: 0)
        displayNameLabel.adjustsFontSizeToFitWidth = true
        displayNameLabel.adjustsFontForContentSizeCategory = true
        displayNameLabel.updateForAccountDisplayName(account: status.account)
        vStack.addArrangedSubview(displayNameLabel)
        
        let contentView = StatusContentTextView()
        contentView.defaultFont = .preferredFont(forTextStyle: .caption2)
        contentView.monospaceFont = UIFontMetrics(forTextStyle: .caption2).scaledFont(for: .monospacedSystemFont(ofSize: 17, weight: .regular))
        contentView.isUserInteractionEnabled = false
        contentView.isScrollEnabled = false
        contentView.backgroundColor = nil
        contentView.textContainerInset = .zero
        contentView.adjustsFontForContentSizeCategory = true
        // remove the extra line spacing applied by StatusContentTextView because, since we're using a smaller font, the regular 2pt looks big
        contentView.paragraphStyle = .default
        // TODO: line limit
        contentView.setTextFrom(status: status)
        contentView.translatesAutoresizingMaskIntoConstraints = false
        vStack.addArrangedSubview(contentView)
        NSLayoutConstraint.activate([
            contentView.heightAnchor.constraint(lessThanOrEqualToConstant: 200),
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        avatarTask?.cancel()
    }

}
