//
//  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 disconnectButton: UIBarButtonItem?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.title = "打印机指令"
        
        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: UIBarButtonItemStyle.plain, target: self, action: #selector(connectBLE))
        wifiButton = UIBarButtonItem.init(title: "WiFi", style: UIBarButtonItemStyle.plain, target: self, action: #selector(connectWiFi))
        disconnectButton = UIBarButtonItem.init(title: "断开", style: UIBarButtonItemStyle.plain, target: self, action: #selector(disconnectPrinter))
        
        self.navigationItem.rightBarButtonItems = [bleButton, 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)
        
        //        if (PTDispatcher.share().printerConnected != nil) {
        //            self.disconnectButton!.isEnabled = true
        //            self.connectButton!.isEnabled = false
        //
        //            self.regiserBluetoothStatus() //当连接成功时，注册打印机状态
        //        }
    }
    
    @objc private func connectBLE() {
        
        self.navigationController?.pushViewController(BluetoothVC(), animated: true)
    }
    
    @objc private func connectWiFi() {
        
        self.navigationController?.pushViewController(WiFiController(), animated: true)
    }
    
    @objc private func disconnectPrinter() {
        
        PTDispatcher.share().unconnectPrinter(PTDispatcher.share().printerConnected)
        PTDispatcher.share().whenUnconnect { (usedTime) in
            ProgressHUD.showError("unconnected")
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    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 (PTDispatcher.share().printerConnected == nil) {
        //            AlertVC.show(message: "请先连接打印机", target: self)
        //        } else {
        
        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)
        }
        //        }
    }
}

