//
//  HistoryViewController.swift


import UIKit

class HistoryViewController : BaseViewController, UICollectionViewDataSource{
    
    
    @IBOutlet var historyCollectionView: UICollectionView!
    @IBOutlet var lblNoHistory: UILabel!
    @IBOutlet weak var btnCloseIniPad: UIButton!
    
    var historyViewModel = HistoryViewModel()
    var current_page = 2 //Current page for Pagination API
    
    //MARK: - Life Cycle -
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setUpLayout()
        self.callToViewModelForUIUpdate()
        if UserDefaultManager.shared.isHideCustomReview == false && FIRRemoteManagement.shared.isShow_CustomReviewPopup{
           // ScreenManager.shared.moveToCustomRating()
            debugPrint("Open Custom Review From history")
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        APPDEL.setNotifyFlagFalse()
    }
    
    func setUpLayout(){
        if (historyCollectionView.contentSize.height < historyCollectionView.frame.size.height) {
            historyCollectionView.alwaysBounceVertical = false
        }
        else{
            historyCollectionView.alwaysBounceVertical = true
        }
        btnCloseIniPad.isHidden = DeviceType.ISPAD ? false : true
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if DeviceType.ISPAD {
            if let layout = historyCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
                let itemWidth = view.bounds.width / 3.0
                let itemHeight = layout.itemSize.height
                layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
                layout.invalidateLayout()
            }
        }
        
    }
    
    func callToViewModelForUIUpdate(){
        self.historyViewModel.arrHistroyList.bind { _ in
            if self.historyViewModel.arrHistroyList.value.count == 0{
                self.lblNoHistory.isHidden = false
                self.historyCollectionView.isHidden = true
            }
            else{
                self.lblNoHistory.isHidden = true
                self.historyCollectionView.isHidden = false
                self.historyCollectionView.reloadData()
            }
        }
    }
    @objc func updateHistoryList(notification: Notification){
        historyViewModel.callHistoryListAPI(current_page: 1, page_limit: 10, isFromResend: false, justRefresh: true)
    }
    @IBAction func btnCloseTapped(_ sender: Any) {
        APPDEL.setNotifyFlagFalse()
        self.dismiss(animated: true, completion: nil)
    }
    
    @IBAction func btnCloseForiPad(_ sender: Any) {
        APPDEL.setNotifyFlagFalse()
        self.dismiss(animated: true, completion: nil)
    }
    
}

//MARK: - Delegate and Datasource

extension HistoryViewController : UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    
    //Datasource Methods
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.historyViewModel.getCellCountForDisplay()
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return self.historyViewModel.prepareCellForDisplay(collectionView: collectionView, indexPath: indexPath, historyData: self.historyViewModel.arrHistroyList.value)
    }
    
    //Delegate Methods
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if DeviceType.ISPAD {
            return CGSize.init(width: self.historyCollectionView.frame.width/3.07 - 5, height: self.historyCollectionView.frame.width/3.07 - 5)
        }
        return CGSize.init(width: self.historyCollectionView.frame.width/2 - 5, height: self.historyCollectionView.frame.width/2 - 5)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return DeviceType.ISPAD ? 15 : 10
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if indexPath.row == self.historyViewModel.arrHistroyList.value.count - 1 && self.historyViewModel.have_more_pages.value{
            self.historyViewModel.callHistoryListAPI(current_page: self.current_page, page_limit: 10, isFromResend: false) { sucess in
                if sucess{
                    self.current_page += 1
                }
            }
        }
    }
}
