//
//  NonEMIViewModel.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 07/12/21.
//

import Foundation
import Alamofire

class NonEMIViewModel {
    
    var delegate: NonEMIViewProtocol?
    
    var nonEmiModel = [EMILoanOriginalModel]()
    
    //POST Non-EMI Base Loan API
    func getNonEMILoan(loanAmount:String, interestRate:String, loanTenure:String, interestPaymentFrequency:String, loanStartDate:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_NON_EMI_LOAN
            
            var interest = String()
            switch interestPaymentFrequency {
            case "Monthly":
                interest = "12"
            case "Bi-Monthly":
                interest = "6"
            case "Quarterly":
                interest = "4"
            case "Triannually":
                interest = "3"
            case "Half Yearly":
                interest = "2"
            case "Annually":
                interest = "1"
            default:
                interest = ""
            }
                        
            let parameters: [String: String] = [
                "loanAmount": loanAmount,
                "interestRate": interestRate,
                "loanTenure": loanTenure,
                "interestPaymentFrequency": interest,
                "loanStartDate": loanStartDate
            ]
            print(parameters)
            
            AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
                
                switch response.result {
                case .success(let value):
                    delegate.stopLoader()
                    print(value)
                    let data = value as! NSDictionary
                    print(data)
                    DispatchQueue.main.async {
                        let loanCategory = data["loanCategory"] as? Int
                        let loanCategoryLabel = data["loanCategoryLabel"] as? String
                        let loanDiscription = data["loanDiscription"] as? String
                        let loanProvider = data["loanProvider"] as? String
                        let loanType = data["loanType"] as? Int
                        let loanTypeLabel = data["loanTypeLabel"] as? String
                        let loanAmount = data["loanAmount"] as? Double
                        let interestRate = data["interestRate"] as! Double
                        let loanTenure = data["loanTenure"] as!  Int
                        let loanStartDate = data["loanStartDate"] as? String
                        let numberOfEMI = data["numberOfEMI"] as! Int
                        let emiAmount = data["emiAmount"] as! Double
                        let loanEndDate = data["loanEndDate"] as! String
                        let interestPaymentFrequency = data["interestPaymentFrequency"] as! Int
                        let totalInterestPaid = data["totalInterestPaid"] as! Double
                        let loan_original_flag = data["loan_original_flag"] as? String
                        let displayDate = data["displayDate"] as? String
                        
                        let nonEmiData = EMILoanOriginalModel(loanCategoryLabel: loanCategoryLabel ?? "", loanDiscription: loanDiscription ?? "", loanProvider: loanProvider ?? "", loanTypeLabel: loanTypeLabel ?? "", loanStartDate: loanStartDate ?? "", loanEndDate: loanEndDate, loan_original_flag: loan_original_flag ?? "", displayDate: displayDate ?? "", loanCategory: loanCategory ?? 0, loanType: loanType ?? 0, loanTenure: loanTenure, numberOfEMI: numberOfEMI, interestPaymentFrequency: interestPaymentFrequency, loanAmount: loanAmount ?? 0.0 , interestRate: interestRate, emiAmount: emiAmount, totalInterestPaid: totalInterestPaid)
                        
                        self.nonEmiModel.append(nonEmiData)
                        delegate.navigate(nonEmiData)
                    }
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
}

protocol NonEMIViewProtocol {
    func startLoader()
    func stopLoader()
    func showError(error:String)
    func navigate(_ data:EMILoanOriginalModel)
}
