//
//  HomeVC.swift
//  Garmentoo
//
//  Created by Vijith on 29/03/22.
//

import UIKit

class HomeVC: UIViewController {

    @IBOutlet weak var profPic: UIImageView!
    @IBOutlet weak var viewTable: UIView!
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var viewCollection: UIView!
    @IBOutlet weak var itemsCollectionView: UICollectionView!
    @IBOutlet weak var categCollectionView:UICollectionView!
    
   
    // for categ selection
    var categ = ["All","Kids","Men","Women"]
    var categIndex = 0
    let margin: CGFloat = 10
    // for categ list
    var productType = ["Popular Products","Recommended Products"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        flowlayout()
        flowlayout2()
        setui()
        selectedView()
        
    }
    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 flowlayout2()
    {
        guard let collectionView = categCollectionView, let flowLayout2 = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
        flowLayout2.minimumInteritemSpacing = margin
        flowLayout2.minimumLineSpacing = margin
        flowLayout2.sectionInset = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
    }
    func setui(){
        self.profPic.layer.cornerRadius = self.profPic.frame.height/2
        self.table.layer.cornerRadius = 12.0
        self.table.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner] // Top right corner, Top left corner respectively
        self.viewCollection.layer.cornerRadius = 12.0
        self.viewCollection.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner] // Top right corner, Top left corner respectively
   }
    func selectedView(){
        if (categIndex == 0)
        {
            self.viewTable.isHidden = false
            self.viewCollection.isHidden = true
            DispatchQueue.main.async {
                self.table.reloadData()
            }
        }
        else{
            self.viewTable.isHidden = true
            self.viewCollection.isHidden = false
            DispatchQueue.main.async {
                self.itemsCollectionView.reloadData()
            }
        }
    }
}
extension HomeVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (collectionView == itemsCollectionView)
        {
            return 10
        }
        return categ.count // categ collection view
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if (collectionView == itemsCollectionView)
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "itemscell", for: indexPath) as! ItemsCollectionViewCell
            return cell
        }
       // if collectionView == 
        
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "typeCell", for: indexPath) as! CategSelectCVCell
        cell.txtLabel.text = categ[indexPath.row]
        if (categIndex == indexPath.row)
        {
            cell.outerView.backgroundColor = UIColor.neonOrange
            cell.txtLabel.textColor = UIColor.white
        }
        else
        {
            cell.outerView.backgroundColor = UIColor.white
            cell.txtLabel.textColor = UIColor.darkGray
        }
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (collectionView == categCollectionView)
        {
            categIndex = indexPath.row
            DispatchQueue.main.async {
                self.categCollectionView.reloadData()
            }
            selectedView()
        }
        else //if (collectionView == itemsCollectionView)
        {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let nextVc = sb.instantiateViewController(withIdentifier: "ProductDetailVC") as! ProductDetailVC
            nextVc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(nextVc, animated: true)
        }
       
        
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if (collectionView == itemsCollectionView)
        {
        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) )
        }
        
        let noOfCellsInRow = 4
        
        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: 40 )
    
    }
}
extension HomeVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return productType.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "listTableCell", for: indexPath) as! ItemsListTableViewCell
        cell.headTitle.text = productType[indexPath.row]
        cell.seeAllBtn.tag = indexPath.row
        cell.seeAllBtn.addTarget(self, action: #selector(self.seeAllBtnTap(_:)), for: .touchUpInside)
        cell.parent = self
        return cell
    }
    @objc func seeAllBtnTap(_ sender: UIButton?)
    {
        switch sender?.tag {
        case 0:
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let nextVc = sb.instantiateViewController(withIdentifier: "SeeAllItemsVC") as! SeeAllItemsVC
            nextVc.hidesBottomBarWhenPushed = true
            nextVc.header = productType[sender?.tag ?? 0]
            self.navigationController?.pushViewController(nextVc, animated: true)
        default:
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let nextVc = sb.instantiateViewController(withIdentifier: "SeeAllItemsVC") as! SeeAllItemsVC
            nextVc.hidesBottomBarWhenPushed = true
            nextVc.header = productType[sender?.tag ?? 0]
            self.navigationController?.pushViewController(nextVc, animated: true)
        }
    }
    
    
}
// to give cell action for collection cell inside table cell
extension HomeVC: collectionCellselect
{
    func navigateAction(index: Int) {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let nextVc = sb.instantiateViewController(withIdentifier: "ProductDetailVC") as! ProductDetailVC
        nextVc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
}
