//
//  ZPLDemoController.swift
//  iOSPrinterDemo
//
//  Created by mellow on 2017/4/19.
//  Copyright © 2017年 Mellow. All rights reserved.
//

import Foundation

class ZPLViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableView: UITableView?
    var functions: [String]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.title = "ZPL Demo"
        
        self.tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.plain)
        self.tableView!.dataSource = self;
        self.tableView!.delegate = self;
        self.view.addSubview(self.tableView!)
        
        self.functions = ["testCallFont", "testBarcodeB0", "testBarcodeB1", "testBarcodeB3", "testBarcodeB5", "testBarcodeB8", "testBarcodeBE", "testBarcodeBI", "testBarcodeBJ", "testBarcodeBL", "testBarcodeBS", "testChangeAlphanumericDefaultFont", "testCodeValidation", "testDownloadFormat", "testPrintBitmap", "testFieldHexadecimalIndicator", "testFieldParameter", "testFieldTypset", "testFieldVariable", "testFieldOrientation", "testFieldComment", "testGraphicBox"]
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
    }
    
    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 = "TSPLDemoControllerCell"
        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: NSLocalizedString("Please connect the printer", comment: ""), target: self)
        }else {
            zplTest(withIndexPath: indexPath)
        }
        
    }
    
    // MARK: - Method
    func zplTest(withIndexPath indexPath: IndexPath) {
        let zpl = PTTestZPL()
        switch indexPath.row {
        case 0: zpl.testCallFont();    break;
        case 1: zpl.testBarcodeB0();    break;
        case 2: zpl.testBarcodeB1();    break;
        case 3: zpl.testBarcodeB3();    break;
        case 4: zpl.testBarcodeB4();    break;
        case 5: zpl.testBarcodeB8();    break;
        case 6: zpl.testBarcodeBE();    break;
        case 7: zpl.testBarcodeBI();    break;
        case 8: zpl.testBarcodeBJ();    break;
        case 9: zpl.testBarcodeBL();       break;
        case 10: zpl.testBarcodeBS();       break;
        case 11: zpl.testChangeAlphanumericDefaultFont();      break;
        case 12: zpl.testCodeValidation();  break;
        case 13: zpl.testDownloadFormat();  break;
        case 14: zpl.testPrintBitmap();             break;
        case 15: zpl.testFieldHexadecimalIndicator();   break;
        case 16: zpl.testFieldParameter();              break;
        case 17: zpl.testFieldTypset();                 break;
        case 18: zpl.testFieldVariable();               break;
        case 19: zpl.testFieldOrientation();            break;
        case 20: zpl.testFieldComment();                break;
        case 21: zpl.testGraphicBox();                  break;
            
        case 22: zpl.testGraphicCircle();               break;
        case 23: zpl.testGraphicDiagonal();             break;
        case 24: zpl.testGraphicEllipse();              break;
        case 25: zpl.testGraphicSymbol();               break;
        case 26: zpl.testImageLoad();                   break;
        case 27: zpl.testLabelReverse();                break;
        case 28: zpl.testPrintOrientation();            break;
        case 29: zpl.testSerializationFile();           break;
        default: break;
        }
    }
}
