//
//  PollOptionView.swift
//  Tusker
//
//  Created by Shadowfacts on 4/25/21.
//  Copyright © 2021 Shadowfacts. All rights reserved.
//

import UIKit
import Pachyderm

class PollOptionView: UIView {
    
    private let unselectedBackgroundColor = UIColor(white: 0.5, alpha: 0.25)
    
    let checkbox: PollOptionCheckboxView
    
    init(poll: Poll, option: Poll.Option, mastodonController: MastodonController) {
        checkbox = PollOptionCheckboxView(multiple: poll.multiple)
        
        super.init(frame: .zero)
        
        let minHeight: CGFloat = 35
        layer.cornerRadius = 0.1 * minHeight
        layer.cornerCurve = .continuous
        backgroundColor = unselectedBackgroundColor
        
        checkbox.translatesAutoresizingMaskIntoConstraints = false
        addSubview(checkbox)
        
        let label = EmojiLabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.numberOfLines = 0
        label.font = .preferredFont(forTextStyle: .callout)
        label.text = option.title
        label.setEmojis(poll.emojis, identifier: poll.id)
        addSubview(label)
        
        let percentLabel = UILabel()
        percentLabel.translatesAutoresizingMaskIntoConstraints = false
        percentLabel.text = "100%"
        percentLabel.font = label.font
        percentLabel.isHidden = true
        percentLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        percentLabel.setContentHuggingPriority(.required, for: .horizontal)
        addSubview(percentLabel)
        
        accessibilityLabel = option.title
        
        if (poll.voted ?? false) || poll.effectiveExpired,
           let optionVotes = option.votesCount {
            let frac: CGFloat
            if poll.multiple,
               let votersCount = poll.votersCount,
               mastodonController.instanceFeatures.pollVotersCount {
                frac = votersCount == 0 ? 0 : CGFloat(optionVotes) / CGFloat(votersCount)
            } else {
                frac = poll.votesCount == 0 ? 0 : CGFloat(optionVotes) / CGFloat(poll.votesCount)
            }
            let percent = String(format: "%.0f%%", frac * 100)
            
            percentLabel.isHidden = false
            percentLabel.text = percent
            
            let fillView = UIView()
            fillView.translatesAutoresizingMaskIntoConstraints = false
            fillView.backgroundColor = .tintColor.withAlphaComponent(0.6)
            fillView.layer.zPosition = -1
            fillView.layer.cornerRadius = layer.cornerRadius
            fillView.layer.cornerCurve = .continuous
            addSubview(fillView)
            
            NSLayoutConstraint.activate([
                fillView.leadingAnchor.constraint(equalTo: leadingAnchor),
                fillView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: frac),
                fillView.topAnchor.constraint(equalTo: topAnchor),
                fillView.bottomAnchor.constraint(equalTo: bottomAnchor),
            ])
            
            accessibilityLabel! += ", \(percent)"
        }
        
        let minHeightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
        // on the first layout, something is weird and this becomes ambiguous even though it's fine on subsequent layouts
        // this keeps autolayout from complaining
        minHeightConstraint.priority = .required - 1
        
        NSLayoutConstraint.activate([
            minHeightConstraint,
            
            checkbox.centerYAnchor.constraint(equalTo: centerYAnchor),
            checkbox.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
            
            label.topAnchor.constraint(equalTo: topAnchor, constant: 4),
            label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -4),
            label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: 8),
            label.trailingAnchor.constraint(equalTo: percentLabel.leadingAnchor, constant: -4),
            
            percentLabel.topAnchor.constraint(equalTo: topAnchor),
            percentLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
            percentLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
        ])
        
        isAccessibilityElement = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
