//
//  ViewController.swift
//  Shopping List
//
//  Created by Kiratijuta Bhumichitr on 5/1/2565 BE.
//

import UIKit

class ViewController: UIViewController {

    var products: [Product] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let p1 = Product(name: "MacBook Air", price: 999.0, imageName: "air")
        products.append(p1)
        let p2 = Product(name: "MacBook Pro", price: 1299.0, imageName: "mbpro")
        products.append(p2)
        let p3 = Product(name: "iPad Pro", price: 799.0, imageName: "ipadpro")
        products.append(p3)
        let p4 = Product(name: "iPad Air", price: 599.0, imageName: "ipadair")
        products.append(p4)
        let p5 = Product(name: "iPad mini", price: 399.0, imageName: "ipadmini")
        products.append(p5)
        let p6 = Product(name: "Apple Watch", price: 499.0, imageName: "applewatch")
        products.append(p6)
    }


}

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return products.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let index = indexPath.row
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "shoppingcell", for: indexPath) as! ProductCollectionViewCell
        cell.productImageView.image = UIImage(named: products[index].imageName)
        cell.productNameLabel.text = products[index].name
        cell.productPriceLabel.text = "$\(products[index].price)"
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let index = indexPath.row
        print(products[index].name)
    }
    
    
}
