//
//  ComposeReplyContentView.swift
//  Tusker
//
//  Created by Shadowfacts on 8/22/20.
//  Copyright © 2020 Shadowfacts. All rights reserved.
//

import SwiftUI
import Pachyderm

struct ComposeReplyContentView: UIViewRepresentable {
    typealias UIViewType = ComposeReplyContentTextView
    
    let status: any StatusProtocol
    let mastodonController: MastodonController
    
    let heightChanged: (CGFloat) -> Void
    
    func makeUIView(context: Context) -> UIViewType {
        let view = ComposeReplyContentTextView()
        view.isUserInteractionEnabled = false
        // scroll needs to be enabled, otherwise the text view never reports a contentSize greater than 1 line
        view.isScrollEnabled = true
        view.backgroundColor = .clear
        view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        
        view.adjustsFontForContentSizeCategory = true
        view.defaultFont = TimelineStatusCollectionViewCell.contentFont
        view.monospaceFont = TimelineStatusCollectionViewCell.monospaceFont
        
        view.overrideMastodonController = mastodonController
        view.setTextFrom(status: status)
        
        return view
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
        uiView.heightChanged = heightChanged
    }
}

class ComposeReplyContentTextView: StatusContentTextView {
    var heightChanged: ((CGFloat) -> Void)?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        heightChanged?(contentSize.height)
    }
    
}
