import UIKit
import Eureka
import PKHUD
import RxSwift
import RxCocoa

enum OsagoCheckSource: Equatable, CustomStringConvertible {
    case plateNumber(number: String)
    case vin(number: String)
    
    var description: String {
        switch self {
        case .plateNumber(let number):
            return NSLocalizedString("plate number", comment: "Check by") + " (\(number))"
        case .vin(let number):
            return "VIN (\(number))"
        }
    }
}

class OsagoAddController: FormViewController {
    
    private let bag = DisposeBag()
    var checkSources: [OsagoCheckSource] = []
    var onDone: ((Vehicle) -> Void)?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = NSLocalizedString("OSAGO check", comment: "")
        
        form +++ Section(NSLocalizedString("Check parameters", comment: ""))
            <<< DateTimeInlineRow("date") { row in
                row.title = NSLocalizedString("Check date", comment: "")
                row.value = Date()
            }
            <<< PickerInlineRow<OsagoCheckSource>("SourcePicker") { row in
                row.title = NSLocalizedString("Check by", comment: "")
                row.value = self.checkSources.first
                row.options = self.checkSources
            }
        
        form +++ Section()
            <<< ButtonRow() { $0.title = NSLocalizedString("Check", comment: "verb") }.onCellSelection { _, _ in
//                HUD.show(.progress)
//                Api.checkOsago(number: "Н133РО161", vin: nil, date: Date(), token: "")
//                    .observeOn(MainScheduler.instance)
//                    .subscribe { vehicle in
//                        HUD.hide()
//                        self.onDone?(vehicle)
//                    } onError: { err in
//                        HUD.show(error: err)
//                    }
//                    .disposed(by: self.bag)
//                return
                
                
                let controller = DkbmController()
                controller.onDone = { token in
                    self.navigationController?.popViewController(animated: true, completion: {
                        guard let source = (self.form.rowBy(tag: "SourcePicker") as? PickerInlineRow<OsagoCheckSource>)?.value,
                              let date = (self.form.rowBy(tag: "date") as? DateTimeInlineRow)?.value
                        else { return }
                        
                        var number, vin: String?
                        switch source {
                        case .plateNumber(let n):
                            number = n
                        case .vin(let v):
                            vin = v
                        }
                        HUD.show(.progress)
                        Api.checkOsago(number: number, vin: vin, date: date, token: token)
                            .observeOn(MainScheduler.instance)
                            .subscribe { vehicle in
                                HUD.hide()
                                self.onDone?(vehicle)
                            } onError: { err in
                                HUD.show(error: err)
                            }
                            .disposed(by: self.bag)

                    })
                }
                self.navigationController?.pushViewController(controller, animated: true)
                //self.present(controller, animated: true)
            }
    }
}
