//
//  SubCategoryVC.swift
//  Garmentoo
//
//  Created by Vijith on 03/06/22.
//

import UIKit

class SubCategoryVC: UIViewController {
    
    let margin: CGFloat = 10
    @IBOutlet weak var headerTitle:UILabel!
    @IBOutlet weak var subcategCV:UICollectionView!
    
  //  var subcategName = []
    
   // var subcategImage = []
    
    var header = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        flowlayout()
        // Do any additional setup after loading the view.
        subcategCV.backgroundColor = UIColor.viewBg
        headerTitle.text = header
    }
    
    func flowlayout()
    {
        guard let collectionView = subcategCV, 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)
    }
    

   
    @IBAction func OnTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
}
extension SubCategoryVC:UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "subcategCell", for: indexPath) as! SubcategoryCollectionViewCell
//        cell.titleText.text = subcategName[indexPath.row]
//        cell.img.image = categImg[indexPath.row]
        return cell
    }
    
    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
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
    
}
extension SubCategoryVC:UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        
        let noOfCellsInRow = 3
        
        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 + 10 ) )
        }
   
}
