//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

class MusicCollectionViewCell: UICollectionViewCell {
	// MARK: Public Properties and Types
	enum MusicCollectionViewCellAccessory {
		case current
		case playing
		case options
		case unchecked
		case checked
		case none
	}

	static let cellHeight: CGFloat = 76.0
	static let narrowRowCellHeight: CGFloat = 56.0
	var accessory: MusicCollectionViewCellAccessory = .options {
		didSet {
			showAccessory(accessory != .none)
			showArtwork(accessory != .checked && accessory != .unchecked)
			if let iv = accessoryImageView {
				switch accessory {
				case .current:
					if iv.isAnimating {
						iv.stopAnimating()
					}
					iv.image = UIImage(named:"playing-animation-paused")
				case .playing:
					iv.isHighlighted = true
					if !iv.isAnimating {
						iv.startAnimating()
					}
					iv.image = UIImage(named:"playing-animation-placeholder")
				case .options:
					if iv.isAnimating {
						iv.stopAnimating()
					}
					iv.image = UIImage(named:"btn-more-options")
				case .unchecked:
					if iv.isAnimating {
						iv.stopAnimating()
					}
					iv.image = nil
				case .checked:
					if iv.isAnimating {
						iv.stopAnimating()
					}
					iv.image = UIImage(named:"ic-input-checked")
				default:
					if iv.isAnimating {
						iv.stopAnimating()
					}
					iv.image = nil
				}
			}
		}
	}

	// MARK: Private Properties
	private static let reorderWidth: CGFloat = 42.0
	private static let accessoryWidth: CGFloat = 51.0
	private static let artworkWidth: CGFloat = 60.0
	private static let artworkRightSpacerWidth: CGFloat = 20.0
	private static let errorStateAlpha: CGFloat = 0.25

	// MARK: UI Outlets
	@IBOutlet weak var titleLabel: UILabel?
	@IBOutlet weak var subtitleLabel: UILabel?
	@IBOutlet weak var artworkImageView: UIImageView?
	@IBOutlet private weak var accessoryImageView: UIImageView?
	@IBOutlet private weak var reorderImageView: UIImageView?
	@IBOutlet private weak var errorStateImageView: UIImageView?
	@IBOutlet private weak var reorderWidthConstraint: NSLayoutConstraint?
	@IBOutlet private weak var accessoryWidthConstraint: NSLayoutConstraint?
	@IBOutlet private weak var artworkWidthConstraint: NSLayoutConstraint?
	@IBOutlet private weak var artworkRightSpacerWidthConstraint: NSLayoutConstraint?
}

// MARK: - Overridden Methods
extension MusicCollectionViewCell {
	override func awakeFromNib() {
		accessoryImageView?.loadAnimationImages(nameStem: "playing-animation", count: 30)
	}
	
	override func prepareForReuse() {
		super.prepareForReuse()
		artworkImageView?.alpha = 1.0
		titleLabel?.alpha = 1.0
		subtitleLabel?.alpha = 1.0
		errorStateImageView?.isHidden = true
	}
}

// MARK: - Public Methods
extension MusicCollectionViewCell {
	func showReorder(_ state: Bool) {
		reorderImageView?.isHidden = !state
		reorderWidthConstraint?.constant = state ? MusicCollectionViewCell.reorderWidth : 0.0
	}
	
	func showErrorState(_ state: Bool) {
		artworkImageView?.alpha = state ? MusicCollectionViewCell.errorStateAlpha : 1.0
		titleLabel?.alpha = state ? MusicCollectionViewCell.errorStateAlpha : 1.0
		subtitleLabel?.alpha = state ? MusicCollectionViewCell.errorStateAlpha : 1.0
		errorStateImageView?.isHidden = !state
	}
}

// MARK: - Private Methods
extension MusicCollectionViewCell {
	private func showAccessory(_ state: Bool) {
		accessoryImageView?.isHidden = !state
		accessoryWidthConstraint?.constant = state ? MusicCollectionViewCell.accessoryWidth : 12.0
	}
	
	private func showArtwork(_ state: Bool) {
		artworkImageView?.isHidden = !state
		artworkWidthConstraint?.constant = state ? MusicCollectionViewCell.artworkWidth : 0.0
		artworkRightSpacerWidthConstraint?.constant = state ? MusicCollectionViewCell.artworkRightSpacerWidth : 8.0
	}
}
