import UIKit

class DeviceIdTableViewController: UITableViewController {

    let resuseIdentifier = "deviceIdCell"
    var spinner : UIActivityIndicatorView?
    var deviceIdList : Array<NSNumber> = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: resuseIdentifier)
        self.tableView.tableFooterView = UIView()
        let ad = UIApplication.shared.delegate as? AppDelegate
        ad?.deviceId = -1
    }

    override func viewDidAppear(_ animated: Bool) {
        DispatchQueue.global(qos: .background).async { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.showSpinnerAtFooter()
            DispatchQueue.main.async {
                let config = MPosControllerConfig()
                let ad = UIApplication.shared.delegate as? AppDelegate
                config.selectInterface(MPOS_INTERFACE_TYPE(rawValue: ad!.conntectionType)!, address: ad?.connectionString)
                if config.openService().rawValue == MPOS_RESULT.RES_SUCCESS.rawValue {
                    strongSelf.deviceIdList = config.searchDevices()
                }
                config.closeService(0)
                strongSelf.tableView.reloadData()
                strongSelf.hideSpinnerAtFooter()
            }
        }
    }
    
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.deviceIdList.count
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ad = UIApplication.shared.delegate as? AppDelegate
        ad?.deviceId = self.deviceIdList[indexPath.row].intValue
        self.navigationController?.popViewController(animated: true)
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: resuseIdentifier, for: indexPath)
        cell = UITableViewCell(style: .subtitle, reuseIdentifier: resuseIdentifier)
        if self.deviceIdList.count <= 0{
            return cell
        }
        let value = self.deviceIdList[indexPath.row].intValue
        cell.detailTextLabel?.text = String(value)
        if(value > 0 && value < 10)             { cell.textLabel?.text = "Label Printer"}
        else if(value == 10)                    { cell.textLabel?.text = "POS Printer (internal)"}
        else if(value > 10 && value < 20)       { cell.textLabel?.text = "POS Printer"}
        else if(value >= 20 && value < 30)      { cell.textLabel?.text = "HID"}
        else if(value >= 30 && value < 40)      { cell.textLabel?.text = "MSR"}
        else if(value >= 40 && value < 50)      { cell.textLabel?.text = "Scanner"}
        else if(value >= 60 && value < 69)      { cell.textLabel?.text = "RFID"}
        else if(value >= 70 && value < 79)      { cell.textLabel?.text = "DALLAS KEY"}
        else if(value >= 80 && value < 89)      { cell.textLabel?.text = "NFC"}
        else if(value >= 100 && value < 110)    { cell.textLabel?.text = "Unregistered USB UART Device"}
        else if(value >= 110 && value < 120)    { cell.textLabel?.text = "Customer Display"}
        else if(value >= 120 && value < 130)    { cell.textLabel?.text = "TTY USB (USB to Serial)"}
        else if(value >= 130 && value < 140)    { cell.textLabel?.text = "SCALE"}
        else{
            cell.textLabel?.text = "Unknown Type Device";
        }
        return cell
    }
    
    func hideSpinnerAtFooter (){
        DispatchQueue.main.async { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.spinner?.stopAnimating()
        }
    }
    
    func showSpinnerAtFooter (){
        DispatchQueue.main.async { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.spinner = UIActivityIndicatorView(style: .gray)
            strongSelf.tableView.tableFooterView = strongSelf.spinner;
            strongSelf.spinner?.startAnimating()
        }
    }
}
