//
//  LargeAccountDetailViewController.swift
//  Tusker
//
//  Created by Shadowfacts on 1/6/19.
//  Copyright © 2019 Shadowfacts. All rights reserved.
//

import UIKit
import Pachyderm

class LargeAccountDetailView: UIView {

    var avatarImageView = UIImageView()
    var displayNameLabel = AccountDisplayNameLabel()
    var usernameLabel = UILabel()
    
    var avatarRequest: ImageCache.Request?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        avatarImageView.translatesAutoresizingMaskIntoConstraints = false
        avatarImageView.layer.masksToBounds = true
        avatarImageView.layer.cornerCurve = .continuous
        addSubview(avatarImageView)
        
        displayNameLabel.translatesAutoresizingMaskIntoConstraints = false
        displayNameLabel.font = .systemFont(ofSize: 20, weight: .semibold)
        addSubview(displayNameLabel)
        
        usernameLabel.translatesAutoresizingMaskIntoConstraints = false
        usernameLabel.font = .systemFont(ofSize: 17, weight: .light)
        usernameLabel.textColor = .secondaryLabel
        usernameLabel.isAccessibilityElement = false
        addSubview(usernameLabel)
        
        NSLayoutConstraint.activate([
            avatarImageView.heightAnchor.constraint(equalToConstant: 50),
            avatarImageView.widthAnchor.constraint(equalToConstant: 50),
            avatarImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
            avatarImageView.topAnchor.constraint(equalTo: topAnchor, constant: 8),
            avatarImageView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
            displayNameLabel.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: 8),
            displayNameLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
            displayNameLabel.topAnchor.constraint(equalTo: topAnchor, constant: 8),
            usernameLabel.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: 8),
            usernameLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
            usernameLabel.topAnchor.constraint(equalTo: displayNameLabel.bottomAnchor)
        ])
        
        NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
    }
    
    deinit {
        avatarRequest?.cancel()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        updateUIForPreferences()
    }
    
    @objc func updateUIForPreferences() {
        avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
    }
    
    func update(account: Account) {
        displayNameLabel.updateForAccountDisplayName(account: account)
        usernameLabel.text = "@\(account.acct)"
        
        if let avatar = account.avatar {
            avatarRequest = ImageCache.avatars.get(avatar) { [weak self] (_, image) in
                guard let self = self, let image = image else { return }
                self.avatarRequest = nil
                DispatchQueue.main.async {
                    self.avatarImageView.image = image
                }
            }
        }
    }

}
