//
//  MyOrdersVC.swift
//  Garmentoo
//
//  Created by MAC on 06/04/22.
//

import UIKit

class MyOrdersVC: UIViewController {
    @IBOutlet weak var categCollectionView:UICollectionView!
    @IBOutlet weak var orderListTable: UITableView!
    
    var categ = ["Upcoming","History"]
    var categIndex = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        swipeGest()
    }
    
    @IBAction func onTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
  func swipeGest()
  {
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeRight.direction = .right
        self.orderListTable.addGestureRecognizer(swipeRight)

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeLeft.direction = .left
        self.orderListTable.addGestureRecognizer(swipeLeft)
  }
    
    @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            case .right:
                print("Swiped right")
                if categIndex != 0
                {
                categIndex -= 1
                DispatchQueue.main.async {
                    self.orderListTable.layer.rightAnimation(duration: 0.3)
                    self.categCollectionView.reloadData()
                    self.orderListTable.reloadWithAnimation()
            }
                }
            
            case .left:
                print("Swiped left")
                if categIndex != (categ.count - 1)
                {
                categIndex += 1
                DispatchQueue.main.async {
                    self.orderListTable.layer.leftAnimation(duration: 0.3)
                    self.categCollectionView.reloadData()
                    self.orderListTable.reloadWithAnimation()
            }
                }
            
            default:
                break
            }
        }
    }
    
    
}

// collection view for categ select
extension MyOrdersVC : UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return categ.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "orderCateg", for: indexPath) as! MyOrderCategCollectionViewCell
        cell.lbl.text = categ[indexPath.row]
        if (categIndex == indexPath.row)
        {
            cell.outerView.backgroundColor = UIColor.neonOrange
        cell.lbl.textColor = UIColor.white
        }
        else
        {
        cell.outerView.backgroundColor = UIColor.viewBg
        cell.lbl.textColor = UIColor.textcolor1
            
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if !(categIndex == indexPath.row)
        {
            
            if (categIndex < indexPath.row) // left transition
            {
                self.orderListTable.layer.leftAnimation(duration: 0.3)
            }
            else // right transition
            {
                self.orderListTable.layer.rightAnimation(duration: 0.3)
            }
            categIndex = indexPath.row
            DispatchQueue.main.async {
                self.categCollectionView.reloadData()
               // self.orderListTable.reloadData()
                self.orderListTable.reloadWithAnimation()
        }
        }
      
}
}


// table view for orders list
extension MyOrdersVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "orderItems", for: indexPath) as! MyOrderItemsTableViewCell
        cell.trackOrderBtn.tag = indexPath.row
        cell.trackOrderBtn.addTarget(self, action: #selector(self.trackOrder(_:)), for: .touchUpInside)
        if self.categIndex == 1
        {
            cell.deliverTitleLabel.text = "Delivered On:"
            cell.trackOrderBtn.isHidden = true
        }
        else
        {
            cell.deliverTitleLabel.text = "Expected Delivery:"
            cell.trackOrderBtn.isHidden = false
        }
        
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let destViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "orderDetail") as! OrderDetailPage
        destViewController.selectedCatIndex = self.categIndex
        destViewController.hidesBottomBarWhenPushed = true
        
        self.navigationController?.pushViewController(destViewController, animated: true)
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }
    @objc func trackOrder(_ sender: UIButton?)
    {
       
        let destViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TrackingVC") as! TrackingVC
        destViewController.hidesBottomBarWhenPushed = true
        
        self.navigationController?.pushViewController(destViewController, animated: true)
    }
    /*
    @objc func detailOrder(_ sender: UIButton?)
    {
       
        let destViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "orderDetail") as! OrderDetailPage
        destViewController.selectedCatIndex = self.categIndex
        destViewController.hidesBottomBarWhenPushed = true
        
        self.navigationController?.pushViewController(destViewController, animated: true)
    }
    */
    
}
