//
//  POSTestViewController.swift
//  PrinterDemo
//
//  Created by ios on 2021/12/22.
//  Copyright © 2021 XiamenHY. All rights reserved.
//

import UIKit

class POSTestViewController: PDBaseViewController {

    lazy var ipTextField: UITextField = {
        let temp = UITextField()
        temp.textColor = UIColor.titleColor
        temp.textAlignment = .center
        temp.font = UIFont.systemFont(ofSize: 17)
        temp.borderStyle = .roundedRect
        temp.placeholder = "IP Address"
        view.addSubview(temp)
        return temp
    }()
    
    lazy var sendBtn : UIButton = {
        let temp = UIButton(type: .custom)
        temp.backgroundColor = UIColor.theme
        temp.setTitle("Send".localized, for: .normal)
        temp.setTitleColor(UIColor.white, for: .normal)
        temp.setTitleColor(UIColor.black, for: .highlighted)
        temp.clipsToBounds = true
        temp.layer.cornerRadius = 10
        temp.layer.borderColor = UIColor.theme.cgColor
        temp.layer.borderWidth = 0.5
        temp.addTarget(self, action: #selector(self.sendDataToPeripheral), for: .touchUpInside)
        view.addSubview(temp)
        return temp
    }()
    
    var ipAddress = ""
    var time: Timer?
    let model = "TP80BE"
    var counter = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        navigationItem.title = "POS(Wi-Fi)"
    }

    override func configUI() {
        ipTextField.snp.makeConstraints { make in
            make.left.equalTo(50)
            make.right.equalTo(-50)
            make.top.equalTo(80)
            make.height.equalTo(50)
        }
        
        sendBtn.snp.makeConstraints {
            $0.left.equalTo(15)
            $0.top.equalTo(ipTextField.snp.bottom).offset(30)
            $0.width.equalTo(kScreenWidth - 30)
            $0.height.equalTo(40)
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        ipTextField.becomeFirstResponder()
        PTDispatcher.share().whenUnconnect {(_) in
            
        }
    }

    @objc func sendDataToPeripheral() {
        
        ipTextField.resignFirstResponder()
        SVProgressHUD.show()
        guard let ipText = ipTextField.text, !ipText.isEmpty else {
            SVProgressHUD.showError(withStatus: "Incomplete input")
            return
        }
        ipAddress = ipText
        time = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(sendTask), userInfo: nil, repeats: false)
    }
    
    func handle(result: ReturnReult) {
        
        switch result {
            case .success(_):
                time?.invalidate()
                time = nil
                PTDispatcher.share().disconnect()
                time = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(sendTask), userInfo: nil, repeats: false)
            case .failure(let error): print(error.localizedDescription)
                SVProgressHUD.showError(withStatus: "send error")
        }
    }
    
    @objc func sendTask() {
        counter = counter > 3 ? 0 : counter
        beginTask(counter)
        counter += 1
    }
}

extension POSTestViewController {
    
    func beginTask(_ taskId: Int) {
        print("--------------\(taskId)")
        SVProgressHUD.show()
        switch taskId {
            case 0:
                let image = UIImage(contentsOfFile: Bundle.main.path(forResource: "IMG_6261", ofType: "JPG")!)!
                PrinterManager.printTaskFor(image: image, inPrinter: ipAddress, model: model, openDrawer: false) { result in
                    self.handle(result: result)
                }
            case 1:
                let image = UIImage(contentsOfFile: Bundle.main.path(forResource: "IMG_6262", ofType: "JPG")!)!
                PrinterManager.printTaskFor(image: image, inPrinter: ipAddress, model: model, openDrawer: false) { result in
                    self.handle(result: result)
                }
            case 2:
                let image = UIImage(contentsOfFile: Bundle.main.path(forResource: "IMG_6263", ofType: "JPG")!)!
                PrinterManager.printTaskFor(image: image, inPrinter: ipAddress, model: model, openDrawer: false) { result in
                    self.handle(result: result)
                }
            case 3:
                PrinterManager.openDrawerTaskForPrinterWith(ipAddress: ipAddress, model: model) { resut in
                    self.handle(result: resut)
                }
            default:
                PrinterManager.openDrawerTaskForPrinterWith(ipAddress: ipAddress, model: model) { resut in
                    self.handle(result: resut)
                }
        }
    }
}
