//
//  Home1VC.swift
//  Garmentoo
//
//  Created by Vijith on 26/05/22.
//

import UIKit

class Home1VC: UIViewController {
    
    let categ = ["Men","Women","Boys","Girls"]
    
    let categImg = [UIImage(named: "men"),UIImage(named: "women"),UIImage(named: "boy"),UIImage(named: "girl")]
    let madeforu = ["New Arrivals","Offers","Discounts"]
    let madeforuIcons = [UIImage(named: "newarrivals"),UIImage(named: "offers"),UIImage(named: "discount")]
    
    let dummyImages = [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"]
    
    @IBOutlet weak var headerHeight:NSLayoutConstraint!
    
    @IBOutlet weak var scrollBottomConstraint: NSLayoutConstraint!
    
    var initialHeaderHeight :CGFloat = 100
  
    
    @IBOutlet weak var categCV: UICollectionView!
    @IBOutlet weak var popularCV: UICollectionView!
    @IBOutlet weak var madeForCV: UICollectionView!
    @IBOutlet weak var recommendedCV: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @IBAction func OnTapSeeAllBtn(_ 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 = "Popular Products"
            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 = "Recommended"
            self.navigationController?.pushViewController(nextVc, animated: true)
        }
    }
    
   

}
extension Home1VC:UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (collectionView == recommendedCV)
        {
            return dummyNames.count
        }
        if (collectionView == madeForCV)
        {
            return madeforu.count
        }
        if (collectionView == popularCV)
        {
            return dummyNames.count
        }
        return categ.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if (collectionView == recommendedCV)
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "recommendedcell", for: indexPath) as! HomeRecommendedCollectionViewCell
            cell.img.image = dummyImages[indexPath.row]
            cell.name.text = dummyNames[indexPath.row]
            return cell
        }
        if (collectionView == madeForCV)
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "madecell", for: indexPath) as! HomemadeforyouCollectionViewCell
            cell.lbl.text = madeforu[indexPath.row]
            cell.img.image = madeforuIcons[indexPath.row]
            return cell
        }
        if (collectionView == popularCV)
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "popularcell", for: indexPath) as! HomePopularCollectionViewCell
            cell.img.image = dummyImages[indexPath.row]
            cell.name.text = dummyNames[indexPath.row]

            
            return cell
        }
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "categCell", for: indexPath) as! HomeCategCollectionViewCell
        cell.titleText.text = categ[indexPath.row]
        cell.img.image = categImg[indexPath.row]
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (collectionView == categCV)
        {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let nextVc = sb.instantiateViewController(withIdentifier: "SubCategoryVC") as! SubCategoryVC
            nextVc.header = categ[indexPath.item]
            nextVc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(nextVc, animated: true)
        }
         else if(collectionView == madeForCV)
            {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let nextVc = sb.instantiateViewController(withIdentifier: "SubCategoryVC") as! SubCategoryVC
            nextVc.header = madeforu[indexPath.item]
            nextVc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(nextVc, animated: true)
         }
        else
        {
            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 Home1VC:UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if (collectionView == madeForCV)
        {
            let cellSize = self.view.frame.size
            return CGSize(width:
                            (cellSize.width/3)-15, height: 90)
        }
        if (collectionView == popularCV) || (collectionView == recommendedCV)
        {
            return CGSize(width: 160, height: 250)
        }
        // for categoriesCV
        let width = ((collectionView.frame.width - 15) / 5) // 15 because of paddings
            print("cell width : \(width)")
            return CGSize(width: 80, height: 128)
    }
    
    // Distance Between Item Cells
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            if (collectionView == madeForCV)
            {
                return 5
            }
            if (collectionView == popularCV)
            {
                return 10
            }
            // for categoriesCV,made for you CV
            return 20
        }
        
        // Cell Margin
//        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//            return UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
//        }
}
/*
extension Home1VC:UIScrollViewDelegate
{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y > 50 {// the value when you want the headerview to hide
        view.layoutIfNeeded()
        headerViewHeightConstraint.constant = 0
        UIView.animate(withDuration: 0.5, delay: 0, options: [.allowUserInteraction], animations: {
            self.view.layoutIfNeeded()
        }, completion: nil)

    }else {
        // expand the header
        view.layoutIfNeeded()
        headerViewHeightConstraint.constant = 100 // Your initial height of header view
        UIView.animate(withDuration: 0.5, delay: 0, options: [.allowUserInteraction], animations: {
            self.view.layoutIfNeeded()
        }, completion: nil)
     }
    }
}
*/

// for hiding tabbar when scrolls
extension Home1VC:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
            //scrolling down
            //self.scrollBottomConstraint.constant = 0
            changeTabBar(hidden: true, animated: true)
        }
        else{
            //scrolling up
           // self.scrollBottomConstraint.constant = 20
            changeTabBar(hidden: false, animated: true)
        }
    }
    
    func changeTabBar(hidden:Bool, animated: Bool){
           let tabBar = self.tabBarController?.tabBar
           let offset = (hidden ? UIScreen.main.bounds.size.height : UIScreen.main.bounds.size.height - (tabBar?.frame.size.height)! )
           if offset == tabBar?.frame.origin.y {return}
           print("changing origin y position")
           let duration:TimeInterval = (animated ? 0.5 : 0.0)
           UIView.animate(withDuration: duration,
                          animations: {tabBar!.frame.origin.y = offset},
                          completion:nil)
       }
}
