//
//  EMILoanViewModel.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 01/12/21.
//

import Foundation
import Alamofire

class EMILoanViewModel {
    
    var delegate: EMILoanViewProtocol?
    
    var emiOriginalModel = [EMILoanOriginalModel]()
    var emiOriginalOutputList = [EMIOriginalLookupList]()
    var emiOutstandingModel = [EMILoanOutstandingModel]()
    var emiOutstandingOutputList = [EMIOutstandingLookupList]()
    
    //POST EMI Original API
    func getEMIOriginalLoan(loanFlag:String, originalInterestRate:String, originalLoanAmount:String, loanStartDate:String, loanTenure:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_EMI_LOAN
                        
            let parameters: [String: String] = [
                "loanFlag": loanFlag,
                "originalInterestRate": originalInterestRate,
                "originalLoanAmount": originalLoanAmount,
                "loanStartDate": loanStartDate,
                "loanTenure": loanTenure
            ]
            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 emiOriginalData = 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, loanTenure: loanTenure, numberOfEMI: numberOfEMI, interestPaymentFrequency: interestPaymentFrequency, loanAmount: loanAmount, interestRate: interestRate, emiAmount: emiAmount, totalInterestPaid: totalInterestPaid)
                        
                        self.emiOriginalModel.append(emiOriginalData)
                        delegate.navigateOriginal(emiOriginalData)
                    }
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
    //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_EMI_LOAN_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.navigate(bondModel: bondModel, bondOuputListModel: self.bondDebentureOutputListModel)
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
    //POST EMI Outstanding API
    func getEMIOutstandingLoan(loanFlag:String, originalInterestRate:String, originalLoanAmount:String, loanDayout:String, loanEMIAmt:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_EMI_LOAN
                        
            let parameters: [String: String] = [
                "loanFlag": loanFlag,
                "originalInterestRate": originalInterestRate,
                "originalLoanAmount": originalLoanAmount,
                "loanDayout": loanDayout,
                "loanEMIAmt": loanEMIAmt
            ]
            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 emiOutstandingData = EMILoanOutstandingModel(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, interestRate: interestRate, emiAmount: emiAmount, totalInterestPaid: totalInterestPaid)
                        
                        self.emiOutstandingModel.append(emiOutstandingData)
                        //delegate.navigate(cpcdData)
                    }
                    
                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_EMI_LOAN_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.navigate(bondModel: bondModel, bondOuputListModel: self.bondDebentureOutputListModel)
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
}

protocol EMILoanViewProtocol {
    func startLoader()
    func stopLoader()
    func showError(error:String)
    func navigateOriginal(_ data:EMILoanOriginalModel)
    func navigateOutstanding(_ data:EMILoanOutstandingModel)
}
