//
//  Links.swift
//  MobileFirstFramework
//
//  Created by Lekshmi S on 15/12/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//
import MVMCoreUI

open class Links: View, UICollectionViewDelegate {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public var collectionView: SelfSizingCollectionView!
    public var links: [LinkModel]?
    private var size: CGFloat?
    private var delegateObject: MVMCoreUIDelegateObject?
    private let itemSpacing: CGFloat = 24.0
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    override open var intrinsicContentSize: CGSize {
        return collectionView.intrinsicContentSize
    }
    
    open override func setupView() {
        super.setupView()
        collectionView = createCollectionView()
        addSubview(collectionView)
        isAccessibilityElement = false
        NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: collectionView, useMargins: false).values))
    }
    
    @objc override open func updateView(_ size: CGFloat) {
        super.updateView(size)
        self.size = size
        collectionView.updateView(size)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        self.delegateObject = delegateObject
        
        guard let linksModel = model as? LinksModel else { return }
        links = linksModel.links
        collectionView.reloadData()
    }
    
    open override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        frame = CGRect(x: 0, y: 0, width: targetSize.width, height: CGFloat(MAXFLOAT))
        layoutIfNeeded()
        print("sssC \(targetSize.width) \(collectionView.contentSize)")
        return collectionView.contentSize
    }
    
    //------------------------------------------------------
    // MARK: - Methods
    //------------------------------------------------------
    /// Creates the collection view.
    open func createCollectionView() -> SelfSizingCollectionView {
        let collection = SelfSizingCollectionView(frame: .zero, collectionViewLayout: createCollectionViewLayout())
        collection.dataSource = self
        collection.delegate = self
        collection.register(LinkCollectionViewCell.self, forCellWithReuseIdentifier: "LinkCollectionViewCell")
        return collection
    }
    
    /// Creates the layout for the collection.
    func createCollectionViewLayout() -> UICollectionViewLeftAlignedLayout {
        let layout = UICollectionViewLeftAlignedLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = itemSpacing
        layout.minimumInteritemSpacing = itemSpacing
        layout.estimatedItemSize = CGSize(width: 20, height: 30)
        return layout
    }
}

//------------------------------------------------------
// MARK: - Delegate methods
//------------------------------------------------------

extension Links: UICollectionViewDataSource {
    open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return links?.count ?? 0
    }
    
    open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let molecule = links?[indexPath.item], let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LinkCollectionViewCell", for: indexPath) as? LinkCollectionViewCell else {
            fatalError()
        }
        cell.reset()
        cell.set(with: molecule, delegateObject, nil)
        return cell
    }
}

//extension Links: MoleculeTableViewCellMoleculeProtocol {
//    public func getSystemFittingTargetSize(for targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority, cell: MoleculeTableViewCell) -> CGSize {
//        print("sss \(targetSize.width)")
//        cell.frame = CGRect(x: 0, y: 0, width: targetSize.width, height: CGFloat(MAXFLOAT))
//        cell.layoutIfNeeded()
// 
//        return CGSize(width: collectionView.contentSize.width + cell.contentView.directionalLayoutMargins.leading + cell.contentView.directionalLayoutMargins.trailing, height: collectionView.contentSize.height + cell.contentView.directionalLayoutMargins.top + cell.contentView.directionalLayoutMargins.bottom)
//    }
//}
