//
//  CarouselItem.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 4/6/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

open class CarouselItem: MoleculeCollectionViewCell, CarouselItemProtocol {
    
    open var allowsPeaking = false
    var peakingLeftArrow = UIImageView(image: MVMCoreUIUtility.imageNamed("peakingRightArrow")?.withRenderingMode(.alwaysTemplate))
    var peakingRightArrow = UIImageView(image: MVMCoreUIUtility.imageNamed("peakingRightArrow")?.withRenderingMode(.alwaysTemplate))
    var peakingCover = MVMCoreUICommonViewsUtility.commonView()
    
    open override func addMolecule(_ molecule: MoleculeViewProtocol) {
        super.addMolecule(molecule)
        contentView.sendSubviewToBack(molecule)
    }
    
    open override func setupView() {
        super.setupView()
        
        // Covers the card when peaking.
        peakingCover.backgroundColor = .white
        peakingCover.alpha = 0
        contentView.addSubview(peakingCover)
        NSLayoutConstraint.constraintPinSubview(toSuperview: peakingCover)
        
        // A small arrow on the next card for when peaking.
        let ratio: CGFloat = 0.015
        peakingLeftArrow.translatesAutoresizingMaskIntoConstraints = false
        peakingLeftArrow.alpha = 0
        peakingLeftArrow.tintColor = .black
        contentView.addSubview(peakingLeftArrow)
        peakingLeftArrow.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        NSLayoutConstraint.scalingPinViewLeft(toSuper: peakingLeftArrow, ratio: ratio, anchor: contentView.widthAnchor)
        
        peakingRightArrow.translatesAutoresizingMaskIntoConstraints = false
        peakingRightArrow.transform = CGAffineTransform(scaleX: -1, y: 1) // Flip
        peakingRightArrow.alpha = 0
        peakingRightArrow.tintColor = .black
        contentView.addSubview(peakingRightArrow)
        peakingRightArrow.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        NSLayoutConstraint.scalingPinViewRight(toSuper: peakingRightArrow, ratio: ratio, anchor: contentView.widthAnchor)
    }

    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        guard let collectionModel = model as? CarouselItemModel else { return }
        
        // Handles peaking.
        allowsPeaking = collectionModel.peakingUI ?? false
        if let peakingArrowColor = collectionModel.peakingArrowColor {
            let color = peakingArrowColor.uiColor
            peakingLeftArrow.tintColor = color
            peakingRightArrow.tintColor = color
        }
    }
   
    public func setPeaking(_ peaking: Bool, animated: Bool) {
        guard allowsPeaking else {
            return
        }
        let animation = {() in
            self.peakingRightArrow.alpha = peaking ? 1 : 0
            self.peakingLeftArrow.alpha = peaking ? 1 : 0
            self.peakingCover.alpha = peaking ? 0.5 : 0
        }
        if animated {
            UIView.animate(withDuration: 0.4, animations: animation)
        } else {
            animation()
        }
    }
}
