//
//  LWCountryPickerVC.swift


import UIKit

class LWCountryPickerVC: BaseViewController {
    
    
    //MARK:- Outlet
    @IBOutlet weak var seachBar: UISearchBar!
    @IBOutlet weak var tblView: UITableView!
    @IBOutlet weak var lblHeader: UILabel!
    @IBOutlet var lblDataNotFound: UILabel!
    
    //MARK:- Variable
//    fileprivate let arrCountry = CountryCode.countryNamesByCode()
    var arrCountry = [CountryListModel]()
    var arrSearchCountry = [CountryListModel]()
    fileprivate var finishedLoadingInitialTableCells = false
    var completionPickedCountry: ((CountryListModel) -> ())?
    fileprivate var isSearchActive = false
    private var countryPickerViewModel = CountryPickerViewModel()
    
    //MARK:- Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setLayout()
        initWithData()
        setNavigationBar()

//        self.countryPickerViewModel.arrContryListWithCode.bind { _ in
//            if self.countryPickerViewModel.arrContryListWithCode.value?.count == 0{
//                self.lblDataNotFound.isHidden = false
//                self.tblView.isHidden = true
//            }
//            else{
//                
//                self.tblView.reloadData()
//            }
//        }
    }
    override func viewWillAppear(_ animated: Bool) {
        
    }
    
    //MARK:- Init Function
    func setLayout(){
        seachBar.searchTextField.textColor = .white
        lblHeader.font = DeviceType.ISPAD ? Font.SFProDisplay_Semibold(fontSize: 31): Font.SFProDisplay_Semibold(fontSize: 21)
        seachBar.searchTextField.font = Font.SFProDisplay_Regular(fontSize: 15)
        seachBar.searchTextField.leftView?.tintColor = UIColor.hexStringToUIColor(hex: "#8B8B8B")

        if arrCountry.count > 0{
            self.lblDataNotFound.isHidden = true
            self.tblView.isHidden = false
        }
        else{
            self.lblDataNotFound.isHidden = false
            self.tblView.isHidden = true
        }
    }
    func initWithData(){}
    func setNavigationBar(){}
    
//    func getCountryNameWithCode(){
//        APIDataManager.shared.getCountryNameList { response in
//            self.arrCountry = response
//            self.tblView.reloadData()
//        } failure: { error in
//            debugPrint("There is some error occurs:", error)
//        }
//    }
    
//    func callToViewModelForUIUpdate(){
//        self.countryPickerViewModel.bindEmployeeViewModelToController = {
////            self.tblView.reloadData()
//        }
//    }
    
    
    //MARK:- Action
    @IBAction func btnDissmissPickerTapped(_ sender: UIButton){
        self.dismiss(animated: true)
    }
}

//MARK: - UITableview Datasource & Delegate -

extension LWCountryPickerVC:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return isSearchActive ? self.arrSearchCountry.count : self.arrCountry.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = isSearchActive ? self.arrSearchCountry : self.arrCountry
        return self.countryPickerViewModel.prepareCellForDisplay(tableView: tableView, indexPath: indexPath, countryData: data)
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.dismiss(animated: true) {
            self.completionPickedCountry?(self.isSearchActive ? self.arrSearchCountry[indexPath.row] : self.arrCountry[indexPath.row])
            NotificationCenter.default.post(name: Notification.Name("openKeyboard"), object: nil)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return DeviceType.ISPAD ? 100 : 70
    }
    
//    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//
//        self.countryPickerViewModel.getAnimationForWillDisplay(indexPath: indexPath, cell: cell, tableView: tableView, countryData: self.countryPickerViewModel.arrContryListWithCode.value!)
//    }
}

//MARK: - UISearchBarDelegate -

extension LWCountryPickerVC: UISearchBarDelegate {

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchBar.isSearchResultsButtonSelected = false
        if let searchText = searchBar.text, !searchText.isEmpty {
            self.arrSearchCountry = self.arrCountry.filter({ (countryData) -> Bool in
                countryData.name!.lowercased().contains(searchText.lowercased())
            })
        } else {
            self.arrSearchCountry = self.arrCountry
        }
        tblView.reloadData()
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        isSearchActive = true
        tblView.reloadData()
        
    }
    
//    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
//        if !searchBar.isSearchResultsButtonSelected {
//            isSearchActive = false
//            tblView.reloadData()
//        }
//    }
    
//    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
//        if !searchBar.text!.isBlank {
//            searchBar.isSearchResultsButtonSelected = true
//        } else {
//            searchBar.text = ""
//        }
//        dismissKeyboard()
//    }
}
