//
//  SeeAllItemsVC.swift
//  Garmentoo
//
//  Created by Vijith on 03/05/22.
//

import UIKit

class SeeAllItemsVC: UIViewController {
    
    let margin: CGFloat = 10
    @IBOutlet weak var headerTitle:UILabel!
    @IBOutlet weak var itemsCollectionView:UICollectionView!
    
    var header = ""
    
    let dummyImages = [UIImage(named: "Garmentoo Bodycon"),UIImage(named: "Garmentoo jeans"),UIImage(named: "Garmentoo Kurta"),UIImage(named: "Garmentoo Palazzos"),UIImage(named: "Garmentoo Bodycon"),UIImage(named: "Garmentoo jeans"),UIImage(named: "Garmentoo Kurta"),UIImage(named: "Garmentoo Palazzos"),UIImage(named: "Garmentoo Bodycon"),UIImage(named: "Garmentoo jeans"),UIImage(named: "Garmentoo Kurta"),UIImage(named: "Garmentoo Palazzos"),UIImage(named: "Garmentoo Bodycon"),UIImage(named: "Garmentoo jeans"),UIImage(named: "Garmentoo Kurta"),UIImage(named: "Garmentoo Palazzos")]
    
    let dummyNames = ["Garmentoo Bodycon","Garmentoo jeans","Garmentoo Kurta","Garmentoo Palazzos","Garmentoo Bodycon","Garmentoo jeans","Garmentoo Kurta","Garmentoo Palazzos","Garmentoo Bodycon","Garmentoo jeans","Garmentoo Kurta","Garmentoo Palazzos","Garmentoo Bodycon","Garmentoo jeans","Garmentoo Kurta","Garmentoo Palazzos"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        flowlayout()
        headerTitle.text = header
        setui()
    }
    
    @IBAction func OnTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    func flowlayout()
    {
        guard let collectionView = itemsCollectionView, 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)
    }
    
    func setui()
    {
        self.itemsCollectionView.backgroundColor = UIColor.viewBg
    }
    

}
extension SeeAllItemsVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dummyNames.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "allitemscell", for: indexPath) as! SeeAllItemsCollectionViewCell
        cell.pic.image = dummyImages[indexPath.row]
        cell.product.text = dummyNames[indexPath.row]
        return cell
    }
    
    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, 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)
    }
}
