//
//  ProductViewSubfamilyUserControl.swift
//  iPOS
//
//  Created by bbc on 24.03.2017.
//  Copyright © 2017 BBC. All rights reserved.
//

import UIKit

class ProductSubfamilyViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate, UIPopoverPresentationControllerDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    
    var searchActive: Bool = false
    var productFamilyId: Int?
    var isShoppingBagButtonEnabled: Bool = true
    var delegate: IProductSelectorListener!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.searchBar.delegate = self
        
        if let title = self.title{
            self.searchBar.placeholder = Util.getString("Search")+" in \(title)"
        }
        
        setSearchBarScopeTitle()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        State.loadProductSubfamilies(productFamilyId: productFamilyId!, filterString: nil){ result in
            if result{
                self.tableView.reloadData()
            }
            else{
                NetworkController.AlertConnectionError(viewController: self)
            }
        }
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setSearchBarScopeTitle(){
        self.searchBar.scopeButtonTitles?[0] = Util.getString("Name")
        self.searchBar.scopeButtonTitles?[1] = Util.getString("Code")
        self.searchBar.scopeButtonTitles?[2] = Util.getString("DefaultBarcode")
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return State.productSubfamily.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell
        let subfamily = State.productSubfamily[indexPath.row]
        
        cell.textLabel?.text = subfamily.name
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let subfamily = State.productSubfamily[indexPath.row]
        let productViewController = self.storyboard?.instantiateViewController(withIdentifier: "productViewController") as! ProductViewController
        State.productsFilterResponse = ProductsFilterResponse()
        productViewController.delegate = self.delegate
        productViewController.title = subfamily.name
        productViewController.selectedFamilyIds = [subfamily.productFamilyId]
        productViewController.selectedSubfamilyIds = [subfamily.productSubfamilyId]
        self.navigationController?.pushViewController(productViewController, animated: true)
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        self.searchBar.setShowsCancelButton(true, animated: true)
        self.searchActive = true
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        if let searchText = searchBar.text, searchText.count > 2{
            self.searchBar.endEditing(true)
            self.searchActive = true
            self.filteredProducts(searchText: searchText)
        }
    }
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        self.searchBar.text = ""
        self.searchBar.showsCancelButton = false
        self.searchBar.endEditing(true)
        self.searchActive = false
    }
    
    func filteredProducts(searchText: String?){
        let productViewController = self.storyboard?.instantiateViewController(withIdentifier: "productViewController") as! ProductViewController
        State.productsFilterResponse = ProductsFilterResponse()
        productViewController.delegate = self.delegate
        if let familyId = self.productFamilyId{
            productViewController.selectedFamilyIds = [familyId]
        }
        productViewController.filterString = searchText
        self.navigationController?.pushViewController(productViewController, animated: true)
    }
}
