//
//  ItemsListTableViewCell.swift
//  Garmentoo
//
//  Created by Vijith on 30/03/22.
//

import UIKit

protocol collectionCellselect
{
    func navigateAction(index:Int)
}

class ItemsListTableViewCell: UITableViewCell {
   
    
    
    var parent: UIViewController?
     var delegate: collectionCellselect?
    
    @IBOutlet weak var headTitle: UILabel!
    @IBOutlet weak var seeAllBtn:UIButton!
    @IBOutlet weak var allitemsCollectionView: UICollectionView!
    
    let margin: CGFloat = 10
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        allitemsCollectionView.delegate = self
        allitemsCollectionView.dataSource = self
        flowlayout()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

       
    }
    func flowlayout()
    {
        guard let collectionView = allitemsCollectionView, let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
        flowLayout.minimumInteritemSpacing = margin
        flowLayout.minimumLineSpacing = margin
        flowLayout.sectionInset = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
    }
   

}
extension ItemsListTableViewCell : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "allitemscell", for: indexPath) as! AllItemsCollectionViewCell
        
        
        return cell
        // // change the image height and ratio to adjust cell size
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let nextVc = sb.instantiateViewController(withIdentifier: "ProductDetailVC") as! ProductDetailVC
        nextVc.hidesBottomBarWhenPushed = true
        parent?.navigationController!.pushViewController(nextVc, animated: true)
//        self.delegate?.navigateAction(index: indexPath.row)
    }
    /*
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
       
        let noOfCellsInRow = 2
        
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        
        let totalSpace = flowLayout.sectionInset.left
            + flowLayout.sectionInset.right
            + (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
        
        let size = Int((collectionView.bounds.width - totalSpace ) / CGFloat(noOfCellsInRow))
        
        
            return CGSize(width: size , height: (size + 50) )
        }
    
   */

    func collectionView(_ collectionView: UICollectionView,
                                layout collectionViewLayout: UICollectionViewLayout,
                                sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 150, height: 190)

            }
    /*

            func collectionView(_ collectionView: UICollectionView,
                                layout collectionViewLayout: UICollectionViewLayout,
                                minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
                return 8.0
            }

            func collectionView(_ collectionView: UICollectionView, layout
                collectionViewLayout: UICollectionViewLayout,
                                minimumLineSpacingForSectionAt section: Int) -> CGFloat {
                return 8.0
            }
     */
    
}
