//
//  BindingViewControllers.swift
//  FireBoltt
//
//  Created by lemo. on 2020/3/21.
//  Copyright © 2020 Sheldon. All rights reserved.
//

import UIKit

fileprivate enum Resuable {
    static let tableViewCell = ReusableCell<CommonTableViewCell>(nibName: "CommonTableViewCell")
}

class BindingViewControllers: TableViewController {

    private lazy var nodataView: NoDataView = {
        let noDataView = NoDataView(frame: CGRect.zero, tip: MultiLanguageKey_FB.searchFailFB.localized, type: .scanDevice)
        noDataView.isHidden = false
        noDataView.backgroundColor = kHexColor(0xffffff)
        return noDataView
    }()
    
    let rxDataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, TableViewCellModel>>(configureCell: {  (_ , tv, indexPath, model) -> UITableViewCell in
        let cell = tv.dequeue(Resuable.tableViewCell, for: indexPath)
        cell.bindViewModel(model: model)
        return cell
    },  titleForHeaderInSection: { ds, index in
        return ds.sectionModels[index].model
    })
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "deviceCell")
    }
    
    override func makeUI() {
        super.makeUI()
        let rightItem = UIBarButtonItem.baseBarButtonItem(normalImg: "", highImg: nil, title: MultiLanguageKey_FB.scanFB.localized, target: self, action: #selector(clickScan))
        navigationItem.rightBarButtonItem = rightItem
        view.addSubview(nodataView)
        nodataView.snp.makeConstraints { (make) in
             make.edges.equalToSuperview()
         }
        tableView.register(Resuable.tableViewCell)
        tableView.rowHeight = kScaleHeight(80)
        tableView.sectionHeaderHeight = kScaleHeight(42)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        guard let viewModel = viewModel as? BindingViewModels else { return }
        viewModel.scanState
            .subscribe(onNext: { [weak self] (state) in
                if !state {
                    SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.scanFailTipFB.localized)
                }else {
                    self?.nodataView.isHidden = true
                }
            })
            .disposed(by: rx.disposeBag)
        viewModel.connectState
            .subscribe(onNext: { [weak self] (state) in
                if !state {
                    SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.connectFailTipFB.localized)
                    return
                }
                // 连接成功
                SVProgressHUD.dismiss()
                self?.navigator.pop(sender: self)
            })
            .disposed(by: rx.disposeBag)
        viewModel.scanDevices
            .bind(to: tableView.rx.items(dataSource: rxDataSource))
            .disposed(by: rx.disposeBag)
        viewModel.scanTimeout
            .subscribe(onNext: { [weak self] _ in
                self?.nodataView.isHidden = false
            })
            .disposed(by: rx.disposeBag)
        tableView.rx.itemSelected
            .subscribe(onNext: { (indexPath) in
                if viewModel.scanBleModels.count > indexPath.row {
                    SVProgressHUD.show(withStatus: MultiLanguageKey_FB.coonectingFB.localized)
                    let bleModel = viewModel.scanBleModels[indexPath.row]
                    viewModel.connctDevice(bleModel: bleModel)
                }
            })
            .disposed(by: rx.disposeBag)
         clickScan()
    }
    
    @objc func clickScan() {
        guard let viewModel = viewModel as? BindingViewModels else { return }
        viewModel.startScan()
    }

}