//
//  PaymentViewModel.swift
//  FoodOrderApp
//
//  Created by Simge Çakır on 22.08.2021.
//

import Foundation

protocol PaymentViewModelProtocol {
    var delegate: PaymentViewModelDelegate? { get set }
    func validateFields(cardNumber: String?, cvc: String?, addres: String?)
}

protocol PaymentViewModelDelegate: AnyObject {
    func navigateToHome()
    func showToastMessage(message: String)
}


final class PaymentViewModel: PaymentViewModelProtocol {
    weak var delegate: PaymentViewModelDelegate?
    
    
    func validateFields(cardNumber: String?, cvc: String?, addres: String?){
        
        if cardNumber == "" || cvc == "" || addres == "" {
            delegate?.showToastMessage(message: "Please fill in all fields")
        } else{
            if let _ = Int(cardNumber ?? ""), let _ = Int(cvc ?? ""){
                if cardNumber!.count < 16 || cardNumber!.count > 16 {
                    delegate?.showToastMessage(message: "Missing or excessive Card Number! ")
                } else {
                    delegate?.showToastMessage(message: "Order Completed 🍔")
                    delegate?.navigateToHome()
                }
            } else {
                delegate?.showToastMessage(message: "Please enter numbers for card number and cvc fields.")
            }
        }
    }
}
