//
//  CountryPickerViewModel.swift


import Foundation
import UIKit
import SDWebImage

class CountryPickerViewModel : NSObject{
    
    var arrSearchContryListWithCode: BoxBind<[CountryListModel]?> = BoxBind([CountryListModel]())
    var arrContryListWithCode: BoxBind<[CountryListModel]?> = BoxBind([CountryListModel]())
    
    var bindEmployeeViewModelToController : (() -> ()) = {}
    
    override init() {
        super.init()
    }
    
    func getContryListAPI(){
        APIDataManager.shared.getCountryNameList { response in
            self.arrContryListWithCode.value = response
            self.arrSearchContryListWithCode.value = response
        } failure: { error in
            debugPrint("There is some error occurs:", error)
        }
    }
    
    //MARK: - Country Tbl View Datasource & Delegate -
    
    //Datasource
    func getCellCountForDisplay(countryData : [CountryListModel]) -> Int{
        return countryData.count
    }
    
    func prepareCellForDisplay(tableView: UITableView, indexPath: IndexPath, countryData: [CountryListModel] ) -> UITableViewCell{
        
        let cell = tableView.dequeueReusableCell(withIdentifier: LWCountryPickerTCell.identifier, for: indexPath) as! LWCountryPickerTCell
        cell.lblCountryName.text = countryData[indexPath.row].name
        cell.lblCountryCode.text = countryData[indexPath.row].countrycode
        cell.imgCountryFlag.sd_imageIndicator = SDWebImageActivityIndicator.white
        cell.imgCountryFlag.sd_setImage(with: URL(string: countryData[indexPath.row].url ?? ""), completed: nil)
        return cell
    }
    
    //Delegate
//    func getAnimationForWillDisplay(indexPath : IndexPath, cell : UITableViewCell, tableView : UITableView, countryData: [CountryListModel]){
//        var lastInitialDisplayableCell = false
//        var finishedLoadingInitialTableCells = false
//        //Animation Reference: https://stackoverflow.com/questions/33410482/table-view-cell-load-animation-one-after-another
//        //change flag as soon as last displayable cell is being loaded (which will mean table has initially loaded)
//        if countryData.count > 0 && !finishedLoadingInitialTableCells {
//            if let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows,
//               let lastIndexPath = indexPathsForVisibleRows.last, lastIndexPath.row == indexPath.row {
//                lastInitialDisplayableCell = true
//            }
//        }
//        if !finishedLoadingInitialTableCells {
//            if lastInitialDisplayableCell {
//                finishedLoadingInitialTableCells = true
//            }
//            //animates the cell as it is being displayed for the first time
//            cell.transform = CGAffineTransform(translationX: 0, y: 55/2)
//            cell.alpha = 0
//            UIView.animate(withDuration: 0.5, delay: 0.015*Double(indexPath.row), options: [UIView.AnimationOptions.curveEaseInOut], animations: {
//                cell.transform = CGAffineTransform(translationX: 0, y: 0)
//                cell.alpha = 1
//            }, completion: nil)
//        }
//    }
}
