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

import Foundation
import Alamofire

class EMILoanSummaryViewModel {
    
    var delegate: EMILoanSummaryViewProtocol?
    
    var emiOriginalOutputList = [EMIOriginalLookupList]()
    var emiOutstandingOutputList = [EMIOutstandingLookupList]()
    
    //POST EMI Original Output List
    func getEMIOriginalOutputList(numberOfEMI:String, loanStartingDate:String, originalLoanAmount:String, loanEMIAmt:String, originalInterestRate:String, loanFlag:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_BOND_DEBENTURE_OUTPUT_LIST
                        
            let parameters: [String: String] = [
                "numberOfEMI": numberOfEMI,
                "loanStartingDate": loanStartingDate,
                "originalLoanAmount": originalLoanAmount,
                "loanEMIAmt": loanEMIAmt,
                "originalInterestRate": originalInterestRate,
                "loanFlag": loanFlag
            ]
            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 emiOriginalOutputList = 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.emiOriginalOutputList.append(emiOriginalOutputList)
                        }
                    }
                    delegate.loadOriginal(self.emiOriginalOutputList)
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
    //POST EMI Outstanding Output List
    func getEMIOutstandingOutputList(numberOfEMI:String, loanStartingDate:String, originalLoanAmount:String, loanEMIAmt:String, originalInterestRate:String, loanFlag:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_BOND_DEBENTURE_OUTPUT_LIST
                        
            let parameters: [String: String] = [
                "numberOfEMI": numberOfEMI,
                "loanStartingDate": loanStartingDate,
                "originalLoanAmount": originalLoanAmount,
                "loanEMIAmt": loanEMIAmt,
                "originalInterestRate": originalInterestRate,
                "loanFlag": loanFlag
            ]
            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 emiOutstandingOutputList = EMIOutstandingLookupList(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.emiOutstandingOutputList.append(emiOutstandingOutputList)
                        }
                    }
                    delegate.loadOutstanding(self.emiOutstandingOutputList)
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
}

protocol EMILoanSummaryViewProtocol {
    func startLoader()
    func stopLoader()
    func showError(error:String)
    func loadOriginal(_ data:[EMIOriginalLookupList])
    func loadOutstanding(_ data:[EMIOutstandingLookupList])
}
