import AptoSDK
import ReactiveKit
import UIKit

final class AddCardViewController: UIViewController {
    private var uiConfig: UIConfig
    private let viewModel: AddCardViewModelType
    private let disposeBag = DisposeBag()
    private let cardNetworks: [CardNetwork]
    private lazy var addCardView: AddCardView = {
        let addCardView = AddCardView(uiConfig: uiConfig, cardNetworks: self.cardNetworks)
        return addCardView
    }()

    var closeCompletion: ((UIViewController) -> Void)?

    init(viewModel: AddCardViewModelType, uiConfig: UIConfig, cardNetworks: [CardNetwork]) {
        self.viewModel = viewModel
        self.uiConfig = uiConfig
        self.cardNetworks = cardNetworks
        super.init(nibName: nil, bundle: nil)
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) { fatalError("Not implemented") }

    override func loadView() {
        title = "load_funds.add_card.title".podLocalized()
        view = addCardView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        configureNavigationBar()
        bind()
    }

    private func bind() {
        addCardView.use(viewModel: viewModel)
        viewModel.output.state.observeNext { [weak self] state in
            switch state {
            case .idle:
                self?.hideLoadingView()
            case .loading:
                self?.showLoadingView(uiConfig: nil)
            case let .error(error):
                self?.hideLoadingView()
                self?.show(error: error, uiConfig: nil)
            }
        }.dispose(in: disposeBag)
    }

    override func closeTapped() {
        if let closeCompletion = closeCompletion {
            closeCompletion(self)
        } else {
            viewModel.input.didTapOnClose()
        }
    }

    private func configureNavigationBar() {
        showNavCancelButton(.black)
        navigationController?.navigationBar.hideShadow()
        navigationController?.navigationBar.isOpaque = true
        navigationController?.navigationBar.barTintColor = .white
    }
}
