//
//  ImageListVC.swift
//  Garmentoo
//
//  Created by MAC on 10/05/22.
//

import UIKit
import ImageViewer_swift

class ImageListVC: UIViewController {
    
    let margin: CGFloat = 0
    let imageNames:[String] = [
        "dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image","dummy","dummy 3","image",
        
        
        ]
    var images: [UIImage] = []
//    var images : [UIImage] = [UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy"),UIImage(named: "dummy")]
    
    @IBOutlet weak var imageCollection: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
         images = self.imageNames.compactMap { UIImage(named: $0)! }
        flowlayout()
    }
    
    func flowlayout()
    {
        guard let collectionView = imageCollection, 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 ImageListVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imagecell", for: indexPath) as! ImagesListCollectionViewCell
        cell.imgView.image = images[indexPath.row]
        // Setup Image Viewer with [UIImage]
                cell.imgView.setupImageViewer(
                    images: images,
                    initialIndex: indexPath.row)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        var noOfCellsInRow = Int()
        let screenSize = view.frame.size
        
        if screenSize.width > screenSize.height
        {
           noOfCellsInRow = 4
        }
        else
        {
            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 )
        }
    
    
}
