//
//  StatusEditPollView.swift
//  Tusker
//
//  Created by Shadowfacts on 5/11/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import UIKit
import Pachyderm

class StatusEditPollView: UIStackView, StatusContentPollView {
    
    private var titleLabels: [EmojiLabel] = []
    
    init() {
        super.init(frame: .zero)
        
        axis = .vertical
        alignment = .leading
        spacing = 4
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(poll: StatusEdit.Poll?, emojis: [Emoji]) {
        arrangedSubviews.forEach { $0.removeFromSuperview() }
        titleLabels = []
        
        for option in poll?.options ?? [] {
            // the edit poll doesn't actually include the multiple value
            let icon = PollOptionCheckboxView()
            icon.readOnly = false // this is a lie, but it's only used for stylistic changes
            let label = EmojiLabel()
            label.text = option.title
            label.setEmojis(emojis, identifier: Optional<String>.none)
            titleLabels.append(label)
            let stack = UIStackView(arrangedSubviews: [
                icon,
                label,
            ])
            stack.axis = .horizontal
            stack.alignment = .center
            stack.spacing = 8
            addArrangedSubview(stack)
        }
    }
    
    func estimateHeight(effectiveWidth: CGFloat) -> CGFloat {
        var height: CGFloat = 0
        height += CGFloat(arrangedSubviews.count - 1) * 4
        let labelWidth = effectiveWidth /* checkbox size: */ - 20 /* spacing: */ - 8
        for titleLabel in titleLabels {
            height += titleLabel.sizeThatFits(CGSize(width: labelWidth, height: UIView.layoutFittingCompressedSize.height)).height
        }
        return height
    }

}
