//
//  MoreTrendsFooterCollectionViewCell.swift
//  Tusker
//
//  Created by Shadowfacts on 2/9/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import UIKit

class MoreTrendsFooterCollectionViewCell: UICollectionViewCell {
    
    weak var delegate: TuskerNavigationDelegate?
    
    private var button = UIButton()
    
    private var kind: Kind!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(button)
        NSLayoutConstraint.activate([
            button.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            button.topAnchor.constraint(equalTo: contentView.topAnchor),
            button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        ])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(_ kind: Kind) {
        guard self.kind != kind else {
            return
        }
        self.kind = kind
        
        var config = UIButton.Configuration.plain()
        var title: AttributedString
        switch kind {
        case .hashtags:
            title = "More Trending Hashtags"
        case .links:
            title = "More Trending Links"
        case .profileSuggestions:
            title = "More Suggested Accounts"
        }
        title.font = .preferredFont(forTextStyle: .body).withTraits(.traitBold)!
        config.attributedTitle = title
        config.image = UIImage(systemName: "chevron.right")
        config.imagePlacement = .trailing
        config.buttonSize = .mini
        button.configuration = config
    }
    
    @objc private func buttonPressed() {
        guard let delegate else {
            return
        }
        switch kind {
        case nil:
            return
        case .hashtags:
            delegate.show(TrendingHashtagsViewController(mastodonController: delegate.apiController))
        case .links:
            delegate.show(TrendingLinksViewController(mastodonController: delegate.apiController))
        case .profileSuggestions:
            delegate.show(SuggestedProfilesViewController(mastodonController: delegate.apiController))
        }
    }
    
}

extension MoreTrendsFooterCollectionViewCell {
    enum Kind {
        case hashtags
        case links
        case profileSuggestions
    }
}
