//
//  ProductDetailVC.swift
//  Garmentoo
//
//  Created by Vijith on 11/04/22.
//

import UIKit
import Cosmos

class ProductDetailVC: UIViewController,UIScrollViewDelegate {
    
    
    @IBOutlet weak var imageScrollView: UIScrollView!
    @IBOutlet weak var imageHeight: NSLayoutConstraint!
    
    
    @IBOutlet weak var navBarView: UIView!
    
    
    @IBOutlet weak var view1: UIView!
    @IBOutlet weak var scroll: UIScrollView!
    
    @IBOutlet weak var sizeCV:UICollectionView!
    @IBOutlet weak var bulkCV:UICollectionView!
    
    @IBOutlet weak var reviewsTV: UITableView!
    @IBOutlet weak var tableHeight: NSLayoutConstraint!
    
    @IBOutlet weak var productImage: UIImageView!
    @IBOutlet weak var imageView: UIView!
    
    @IBOutlet weak var oldPrice: UILabel!
    
    @IBOutlet weak var ratingView: CosmosView!
    
    @IBOutlet weak var topSpaceConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var navTitle: UILabel!
    @IBOutlet weak var reviewCollection: UICollectionView!
    @IBOutlet weak var productListCollection: UICollectionView!
    
    @IBOutlet weak var descLbl: UILabel!
    var dummyImgArr = ["dummy", "dummy 1","dummy 3","dummy 1","dummy"]
    
    
    let scrollTopEdgeInsets:CGFloat = 200   //scrollView Top insets size
    
    var size = ["S","M","L","XL"]
    var bulk = ["Pack of 3","Pack of 5","Pack of 7","Pack of 9"]
    
    var sizeIndex = -1
    var bulkIndex = -1
    var ratings = 3.3
    
    var previousOffsetState: CGFloat = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setui()
        //productImage.enableZoom()
        imageScrollView.minimumZoomScale = 1.0
               imageScrollView.maximumZoomScale = 6.0
        imageScrollView.delegate = self
        
        scroll.delegate = self
    }
    override func viewWillAppear(_ animated: Bool) {
        ratingView.rating = ratings
        
        
    }
    
    func setui()
    {
        self.view1.layer.cornerRadius = 20
        self.view1.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        self.scroll.layer.cornerRadius = 20
        self.scroll.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        self.oldPrice.strikeThrough(true)
        self.navBarView.layer.backgroundColor = UIColor.clear.cgColor
        
        self.imageView.backgroundColor = UIColor.viewBg
        
        self.descLbl.textColor = UIColor.textcolor1
        
        let reviewLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        reviewLayout.sectionInset = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
        let screenWidth = self.view.frame.size.width
        reviewLayout.itemSize = CGSize(width: (screenWidth/4)-11, height: (screenWidth/4)-15)
        reviewLayout.minimumInteritemSpacing = 0
        reviewLayout.minimumLineSpacing = 0
        reviewLayout.scrollDirection = .horizontal
        self.reviewCollection!.collectionViewLayout = reviewLayout

        let productLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        productLayout.sectionInset = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
        productLayout.itemSize = CGSize(width: 60, height: 75)
        productLayout.minimumInteritemSpacing = 0
        productLayout.minimumLineSpacing = 0
        productLayout.scrollDirection = .horizontal
        self.productListCollection.collectionViewLayout = productLayout
    }
    
    override func viewWillLayoutSubviews() {
        self.tableHeight.constant = self.reviewsTV.contentSize.height
    }
    
    @IBAction func onTapAddToCart(_ sender: Any) {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let nextVc = sb.instantiateViewController(withIdentifier: "loginPop")as! LoginPopVC
        self.navigationController?.present(nextVc, animated: false, completion: nil)
    }
    
    
    @IBAction func OnTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    // delegate function to enable zooming and scrolling
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
           
            return productImage
        }
    
}
extension ProductDetailVC:UICollectionViewDelegate,UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (collectionView == sizeCV)
        {
            return size.count
        }
        else
        if collectionView == self.reviewCollection
        {
            return 4
        }
        else
        if collectionView == self.productListCollection
        {
            return 5
        }
        return bulk.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if (collectionView == sizeCV)
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sizecell", for: indexPath) as! SizeCollectionViewCell
            cell.lbl.text = size[indexPath.row]
            if (indexPath.row == sizeIndex)
            {
                cell.outerView.layer.borderColor = UIColor.red.cgColor
                cell.lbl.textColor = UIColor.red
            }
            else
            {
                cell.outerView.borderColor = UIColor.textcolor1
                cell.lbl.textColor = UIColor.textcolor1
            }
            return cell
        }
        else if (collectionView == reviewCollection)
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reviewImage", for: indexPath) as! ReviewImageCVC
            
            if indexPath.row == 3
            {
                cell.seeMoreView.isHidden = false
                cell.reviewImage.isHidden = true
            }
            else
            {
                cell.seeMoreView.isHidden = true
                cell.reviewImage.isHidden = false
            }

            return cell
        }
        else if (collectionView == productListCollection)
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "produtImageList", for: indexPath) as! ProductImageListCVC
            
            cell.productImage.image = UIImage(named: self.dummyImgArr[indexPath.row])
            return cell
        }
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "bulkcell", for: indexPath) as! BulkCollectionViewCell
        cell.lbl.text = bulk[indexPath.row]
        if (indexPath.row == bulkIndex)
        {
            cell.outerView.layer.borderColor = UIColor.red.cgColor
            cell.lbl.textColor = UIColor.red
        }
        else
        {
            cell.outerView.layer.borderColor = UIColor.textcolor1.cgColor
            cell.lbl.textColor = UIColor.textcolor1
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (collectionView == sizeCV)
        {
            self.sizeIndex = indexPath.row
            self.sizeCV.reloadData()
        }
        else if (collectionView == self.reviewCollection)
        {
            if (indexPath.row == 3)
            {
                let sb = UIStoryboard(name: "Main", bundle: nil)
                let nextVc = sb.instantiateViewController(withIdentifier: "ImageListVC")as! ImageListVC
                self.navigationController?.pushViewController(nextVc, animated: true)
            }
        }
        else if collectionView == self.productListCollection
        {
            self.productImage.image = UIImage(named: self.dummyImgArr[indexPath.row])
        }
        else
        {
            self.bulkIndex = indexPath.row
            self.bulkCV.reloadData()
        }
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        if collectionView == self.productListCollection
//        {
//            return CGSize(width: 60, height: 75)
//        }
//        else if collectionView ==  self.reviewCollection
//        {
//            let width = (self.view.frame.size.width/4)
//            return CGSize(width: width-15, height: width-11)
//        }
//        return CGSize(width: 100, height: 100)
//    }
//    
    
}
extension ProductDetailVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reviewcell", for: indexPath) as! ReviewsTableViewCell
        cell.ratingsView.rating = 2.4
        return cell
    }
    
    
}
extension ProductDetailVC
{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (scrollView.contentOffset.y >= 336) //216
        
        {
            self.topSpaceConstraint.constant = 50
          //  self.productImage.isHidden = true
            self.navTitle.isHidden = false
            UIView.animate(withDuration: 0.2) {
                self.navBarView.layer.backgroundColor = UIColor.neonOrange.cgColor
            }
//           self.navBarView.layer.backgroundColor = UIColor.neonOrange.cgColor
            self.productListCollection.isHidden = true
           // self.navBarView.layer.backgroundColor = UIColor.white.cgColor
           
            
        }
       
       
       
     else  if scrollView.contentOffset.y < 336 && scrollView.contentOffset.y >= 0
        {
        let offsetDiff = previousOffsetState - scrollView.contentOffset.y
        previousOffsetState = scrollView.contentOffset.y
            let newLayout = self.topSpaceConstraint.constant + offsetDiff
//            self.imageHeight.constant = newLayout
        self.topSpaceConstraint.constant = newLayout
        self.navTitle.isHidden = true
       // self.productImage.isHidden = false
       // self.navBarView.layer.backgroundColor = UIColor.clear.cgColor
        UIView.animate(withDuration: 0.2) {
            self.navBarView.layer.backgroundColor = UIColor.clear.cgColor
        }
        self.productListCollection.isHidden = false
        }
    }

}
