//
//  InstanceTableViewCell.swift
//  Tusker
//
//  Created by Shadowfacts on 9/15/19.
//  Copyright © 2019 Shadowfacts. All rights reserved.
//

import UIKit
import Pachyderm

class InstanceTableViewCell: UITableViewCell {

    @IBOutlet weak var thumbnailImageView: UIImageView!
    @IBOutlet weak var domainLabel: UILabel!
    @IBOutlet weak var adultLabel: UILabel!
    @IBOutlet weak var descriptionTextView: ContentTextView!
    
    var instance: Instance?
    var selectorInstance: InstanceSelector.Instance?
    
    var thumbnailURL: URL?
    var thumbnailRequest: ImageCache.Request?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        thumbnailImageView.layer.masksToBounds = true
        thumbnailImageView.layer.cornerRadius = 5
        
        domainLabel.font = UIFontMetrics(forTextStyle: .title1).scaledFont(for: .systemFont(ofSize: 22, weight: .bold))
        domainLabel.adjustsFontForContentSizeCategory = true
        
        adultLabel.layer.masksToBounds = true
        adultLabel.layer.cornerRadius = 0.5 * adultLabel.bounds.height
        
        descriptionTextView.defaultFont = .preferredFont(forTextStyle: .body)
        descriptionTextView.monospaceFont = UIFontMetrics.default.scaledFont(for: .monospacedSystemFont(ofSize: 17, weight: .regular))
        descriptionTextView.adjustsFontForContentSizeCategory = true
    }
    
    override func updateConfiguration(using state: UICellConfigurationState) {
        backgroundConfiguration = .appListGroupedCell(for: state)
    }
    
    func updateUI(instance: InstanceSelector.Instance) {
        self.selectorInstance = instance
        self.instance = nil
        
        domainLabel.text = instance.domain
        adultLabel.isHidden = instance.category != "adult"
        descriptionTextView.setTextFromHtml(instance.description)
        updateThumbnail(url: instance.proxiedThumbnailURL)
    }
    
    func updateUI(instance: Instance) {
        self.instance = instance
        self.selectorInstance = nil

        domainLabel.text = URLComponents(string: instance.uri)?.host ?? instance.uri
        adultLabel.isHidden = true
        descriptionTextView.setTextFromHtml(instance.shortDescription ?? instance.description)
        
        if let thumbnail = instance.thumbnail {
            updateThumbnail(url: thumbnail)
        } else {
            thumbnailImageView.image = nil
        }
    }
    
    private func updateThumbnail(url: URL) {
        thumbnailImageView.image = nil
        thumbnailURL = url
        thumbnailRequest = ImageCache.attachments.get(url) { [weak self] (_, image) in
            guard let self = self, self.thumbnailURL == url, let image = image else { return }
            self.thumbnailRequest = nil
            DispatchQueue.main.async {
                self.thumbnailImageView.image = image
            }
        }
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        
        thumbnailRequest?.cancel()
        instance = nil
        selectorInstance = nil
    }

}
