//
//  HomeVC.swift
//  OrderPicky
//
//  Created by Vijith on 10/02/22.
//

import UIKit

class HomeVC: UIViewController {
    
    @IBOutlet weak var CollectionView: UICollectionView!
    @IBOutlet weak var PageView: UIPageControl!
    
    @IBOutlet weak var leftView:UIView!
    @IBOutlet weak var rightView:UIView!
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableHeight: NSLayoutConstraint!
    
    @IBOutlet weak var tabView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
       setui()
        setPageView()
    }
    override func viewWillLayoutSubviews() {
        super.updateViewConstraints()
        self.tableHeight.constant = self.tableView.contentSize.height
    }
   

    
    var timer = Timer()
    var counter = 0
    
     var images = [
     UIImage(named: "banner1"),UIImage(named: "banner1"),UIImage(named: "banner1")
     ]
     
    
    func setPageView()
    {
        PageView.numberOfPages = images.count
         PageView.currentPage = 0
         DispatchQueue.main.async {
             self.timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(self.changeScreen), userInfo: nil, repeats: true)
         }
    }
   
       
   

    @objc func changeScreen()
    {
        if counter < images.count
        {
            let index = IndexPath.init(item: counter, section: 0)
            CollectionView.isPagingEnabled = false
            self.CollectionView.scrollToItem(at: index, at: .centeredHorizontally, animated: true)
            CollectionView.isPagingEnabled = true
            PageView.currentPage = counter
            counter += 1
        } else {
            counter = 0
            let index = IndexPath.init(item: counter, section: 0)
            CollectionView.isPagingEnabled = false
            self.CollectionView.scrollToItem(at: index, at: .centeredHorizontally, animated: false)
            CollectionView.isPagingEnabled = true
            PageView.currentPage = counter
        }
    }
    
    func setui(){
        self.leftView.dropShadow(color: UIColor.lightGray, opacity: 0.5, offSet: CGSize(width: 0, height: 0), radius: 5, scale: true)
        self.rightView.dropShadow(color: UIColor.lightGray, opacity: 0.5, offSet: CGSize(width: 0, height: 0), radius: 5, scale: true)
        self.tabView.roundCorners(corners: [.topLeft, .topRight], radius: 16)
    }
    
    @IBAction func homeBtnTapped(_ sender: Any) {
        let nextVc = self.storyboard?.instantiateViewController(withIdentifier: "addAddress")as! AddAddressVC
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
    

}
extension HomeVC : 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: "banner", for: indexPath) as! BannerCollectionCell
        cell.image1.image = images[indexPath.row]
       
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width - 60, height: collectionView.frame.height - 10)
    }
    
}
extension HomeVC : UITableViewDelegate,UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tcell") as! TableListCell
        //cell.outerView.dropShadow()
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        viewWillLayoutSubviews()
    }
    
}
class BannerCollectionCell: UICollectionViewCell {
    @IBOutlet weak var image1:UIImageView!
}
class TableListCell: UITableViewCell {
    @IBOutlet weak var image1:UIImageView!
    @IBOutlet weak var outerView: UIView!
    
    override class func awakeFromNib() {
        
    }
    override func layoutSubviews() {
        self.outerView.dropShadow(color: UIColor.lightGray, opacity: 0.5, offSet: CGSize(width: 0, height: 0), radius: 5, scale: true)
        self.outerView.layer.cornerRadius = 12.0
    }
}

