//
//  PDBLEViewController.swift
//  PrinterDemo
//
//  Created by ios on 2019/9/9.
//  Copyright © 2019 XiamenHY. All rights reserved.
//

import UIKit
import PrinterSDK
import SVProgressHUD
import MJRefresh

class PDBLEViewController: PDBaseViewController {
    
    private var dataSources = [BleTarget]()
    
    let scanner = BleScannerFactory.generate()
    
    lazy var tableView : UITableView = {
        let temp = UITableView.init(frame: .zero, style: .grouped)
        temp.delegate = self
        temp.dataSource = self
        temp.backgroundColor = .white
        temp.rowHeight = 60.0
        temp.showsVerticalScrollIndicator = false
        temp.separatorStyle = UITableViewCell.SeparatorStyle.singleLine
        return temp
    }()
    
    lazy var mj_header: MJRefreshNormalHeader = {
        let temp = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(loadBLE))
        temp.setTitle("Scanning nearby Bluetooth".localized, for: MJRefreshState.refreshing)
        temp.stateLabel?.textColor = UIColor.gray
        temp.lastUpdatedTimeLabel?.isHidden = true
        
        return temp
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "BLE".localized
        view.addSubview(tableView)
        tableView.snp.makeConstraints {
            $0.edges.equalTo(self.view.usnp.edges)
        }
        tableView.mj_header = mj_header
    }
    
    @objc func loadBLE() {
        
        scanner.stop()
        dataSources = []
        self.tableView.reloadData()
        scanner.start { [weak self] targets in
            self?.dataSources = targets
            self?.mj_header.endRefreshing()
            self?.tableView.reloadData()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        scanner.start { [weak self] targets in
            self?.dataSources = targets
            self?.mj_header.endRefreshing()
            self?.tableView.reloadData()
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        scanner.stop()
    }
    
}

extension PDBLEViewController : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSources.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "iden")
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "iden")
        }
        let target = dataSources[indexPath.row]
        cell?.textLabel?.text = target.btName
        cell?.detailTextLabel?.text = target.btMac
        
        cell?.textLabel?.textColor = UIColor.black
        cell?.detailTextLabel?.textColor = UIColor.titleColor
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Bluetooth device nearby".localized
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45.0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let target = dataSources[indexPath.row]
        self.tryAddPrinter(target)
    }
}

extension UIViewController {
    
    func tryAddPrinter(_ convertable: AccessoryConvertable) -> Void {
        SVProgressHUD.show(withStatus: "Connecting, pls. wait".localized)
        PrinterManager.connect(target: convertable) { result in
            switch (result) {
            case .success(let printer):
                PrinterManager.instance.add(printer: printer)
                SVProgressHUD.dismiss()
                self.showAlert(
                    title: "Select paper size".localized,
                    buttonTitles: [
                        "2\" (384dots)",
                        "3\" (576dots)",
                        "3\" (640dots)",
                        "4\" (832dots)",
                        "8\" (2360dots)",
                        "12\" (3540dots)",
                    ],
                    handler: { (selectedButtonIndex) in
                    self.didSelectPaperSize(buttonIndex: selectedButtonIndex)
                })
            case .failure(let error):
                SVProgressHUD.showError(withStatus: S1ErrorDescription(error: error))
            }
        }
    }
    
    func showAlert(title: String, buttonTitles: [String], handler: ((Int) -> Void)?) {
        let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
        
        for i: Int in 0..<buttonTitles.count {
            alert.addAction(UIAlertAction(title: buttonTitles[i], style: .default, handler: { _ in
                DispatchQueue.main.async {
                    handler?(i + 1)
                }
            }))
        }
        
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: { (_) in
            kUserDefaults.set(576, forKey: PDPrintDots)
            self.navigationController?.popViewController(animated: true)
        }))
        self.present(alert, animated: true, completion: nil)
    }
    
    func didSelectPaperSize(buttonIndex: Int) {
        switch buttonIndex {
            case 1:
                kUserDefaults.set(384, forKey: PDPrintDots)
            case 2:
                kUserDefaults.set(576, forKey: PDPrintDots)
            case 3:
                kUserDefaults.set(640, forKey: PDPrintDots)
            case 4:
                kUserDefaults.set(832, forKey: PDPrintDots)
            case 5:
                kUserDefaults.set(2360, forKey: PDPrintDots)
            case 6:
                kUserDefaults.set(3540, forKey: PDPrintDots)
            default:
                fatalError()
        }
        self.navigationController?.popViewController(animated: true)
    }
}
