//
//  NonEMISummaryViewModel.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 08/12/21.
//

import Foundation
import Alamofire

class NonEMISummaryViewModel {
    
    var delegate: NonEMISummaryViewProtocol?
    
    var nonEmiOutputList = [EMIOriginalLookupList]()
    
    //POST NON-EMI Output List
    func getNonEMIOutputList(begningBal:String, interestFrequency:String, refDate:String, loanEndDate:String, interestPaidAmount:String, totalMonths:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_NON_EMI_LOAN_OUTPUT_LIST
                        
            let parameters: [String: String] = [
                "begningBal": begningBal,
                "interestFrequency": interestFrequency,
                "refDate": refDate,
                "loanEndDate": loanEndDate,
                "interestPaidAmount": interestPaidAmount,
                "totalMonths": totalMonths
            ]
            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 {
                        for item in data {
                            let serialNumber = item["serialNumber"] as! Int
                            let refDate = item["refDate"] as! String
                            let financialYear = item["financialYear"] as! String
                            let referenceMonth = item["referenceMonth"] as! String
                            let installmentNumber = item["installmentNumber"] as! Int
                            let begningBal = item["begningBal"] as! Double
                            let interestPayment = item["interestPayment"] as! Double
                            let principalPayment = item["principalPayment"] as! Double
                            let endingBalance = item["endingBalance"] as! Double
                            let emiAmount = item["emiAmount"] as! Double
                            let totalPrincipalPaid = item["totalPrincipalPaid"] as! Double
                            let totalInterestPaid = item["totalInterestPaid"] as! Double
                            let loanEndDays = item["loanEndDays"] as! Int
                            let displayDate = item["displayDate"] as! String
                            
                            let nonEmiOutputList = EMIOriginalLookupList(serialNumber: serialNumber, installmentNumber: installmentNumber, loanEndDays: loanEndDays, refDate: refDate, financialYear: financialYear, referenceMonth: referenceMonth, displayDate: displayDate, begningBal: begningBal, interestPayment: interestPayment, principalPayment: principalPayment, endingBalance: endingBalance, emiAmount: emiAmount, totalPrincipalPaid: totalPrincipalPaid, totalInterestPaid: totalInterestPaid)
                            self.nonEmiOutputList.append(nonEmiOutputList)
                        }
                    }
                    //delegate.navigate(bondModel: bondModel, bondOuputListModel: self.bondDebentureOutputListModel)
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
}

protocol NonEMISummaryViewProtocol {
    func startLoader()
    func stopLoader()
    func showError(error:String)
    func loadData(_ data:[EMIOriginalLookupList])
}
