//
//  ViewController.swift
//  BluetoothDemo
//
//  Created by midmirror on 16/10/28.
//  Copyright © 2016年 Mellow. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableView: UITableView?
    var functions: [String]?
    
    var bleButton: UIBarButtonItem!
    var wifiButton: UIBarButtonItem!
    var autoButton: UIBarButtonItem!
    var disconnectButton: UIBarButtonItem?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = NSLocalizedString("Printer command", comment: "")
        
        self.tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.plain)
        self.tableView!.dataSource = self;
        self.tableView!.delegate = self;
        self.view.addSubview(self.tableView!)
        
        bleButton = UIBarButtonItem.init(title: "BLE", style: .plain, target: self, action: #selector(connectBLE))
        wifiButton = UIBarButtonItem.init(title: "WiFi", style: .plain, target: self, action: #selector(connectWiFi))
        autoButton = UIBarButtonItem.init(title: "Auto", style: .plain, target: self, action: #selector(autoConnectBLE))
        disconnectButton = UIBarButtonItem.init(title: NSLocalizedString("disconnect", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(disconnectPrinter))
        
        self.navigationItem.rightBarButtonItems = [bleButton, autoButton ,wifiButton]
        self.navigationItem.leftBarButtonItem = self.disconnectButton
        //        self.disconnectButton!.isEnabled = false
        //        self.connectButton!.isEnabled = true
        self.functions = ["CPCL", "TSPL", "ESC", "ZPL"]
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)

    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        PTDispatcher.share().whenUnconnect(nil)
    }
    
    @objc private func connectBLE() {
        
        self.navigationController?.pushViewController(BluetoothVC(), animated: true)
    }
    
    @objc private func autoConnectBLE() {
        
        self.navigationController?.pushViewController(AutoConnectBleController(), animated: true)
    }
    
    @objc private func connectWiFi() {
        
        self.navigationController?.pushViewController(WiFiController(), animated: true)
    }
    
    @objc private func disconnectPrinter() {
        PTDispatcher.share().whenUnconnect { (usedTime) in
            ProgressHUD.showError(NSLocalizedString("unconnected", comment: ""))
            PTDispatcher.share().sendSuccessBlock = nil
            PTDispatcher.share().sendFailureBlock = nil
            PTDispatcher.share().sendProgressBlock = nil
            PTDispatcher.share().connectFailBlock = nil
            PTDispatcher.share().connectSuccessBlock = nil
            PTDispatcher.share().readRSSIBlock = nil
        }
        PTDispatcher.share().unconnectPrinter(PTDispatcher.share().printerConnected)
        
    }
    
    open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.functions!.count
    }
    
    open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellIdentifier = "BluetoothVC"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
        
        if cell == nil {
            
            cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: cellIdentifier)
        }
        
        cell!.textLabel!.text = self.functions?[indexPath.row]
        
        return cell!
    }
    
    open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.row == 0 {
            let cpclController = CPCLViewController()
            navigationController?.pushViewController(cpclController, animated: true)
        } else if indexPath.row == 1 {
            let tsplController = TSPLViewController()
            navigationController?.pushViewController(tsplController, animated: true)
        } else if indexPath.row == 2 {
            let escController = ESCViewController()
            navigationController?.pushViewController(escController, animated: true)
        } else if indexPath.row == 3 {
            let zplController = ZPLViewController()
            navigationController?.pushViewController(zplController, animated: true)
        }
    }
}

