//
//  BankRecurringViewModel.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 29/11/21.
//

import Foundation
import Alamofire

class BankRecurringViewModel {
    
    var delegate: BankRecurringViewProtocol?
    
    var bankRecurring = [BankRecurringModel]()
    
    //Fixed deposit TDR API call
    func getRecurringDeposit(depositAmount:String, annualInterestRate:String, year:String, month:String, amountDepositFrequency:String, compoundFrequency:String, depositDate:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_RECURRING_DEPOSIT
            
            var amountDeposit = String()
            switch amountDepositFrequency {
            case "Monthly":
                amountDeposit = "12"
            case "Bi-Monthly":
                amountDeposit = "6"
            case "Quarterly":
                amountDeposit = "4"
            case "Triannually":
                amountDeposit = "3"
            case "Half Yearly":
                amountDeposit = "2"
            case "Annually":
                amountDeposit = "1"
            default:
                amountDeposit = ""
            }
            
            var compound = String()
            switch compoundFrequency {
            case "Monthly":
                compound = "12"
            case "Bi-Monthly":
                compound = "6"
            case "Quarterly":
                compound = "4"
            case "Triannually":
                compound = "3"
            case "Half Yearly":
                compound = "2"
            case "Annually":
                compound = "1"
            default:
                compound = ""
            }
                        
            let parameters: [String: String] = [
                "depositAmount": depositAmount,
                "annualInterestRate": annualInterestRate,
                "year": year,
                "month": month,
                "amountDepositFrequency": amountDeposit,
                "compoundFrequency": compound,
                "depositDate": depositDate
            ]
            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 depositAmount = data["depositAmount"] as! Double
                        let amountDepositFreq = data["amountDepositFreq"] as! Int
                        let annualInterest = data["annualInterest"] as! Double
                        let term = data["term"] as! Int
                        let compundFreq = data["compundFreq"] as! Double
                        let depositDate = data["depositDate"] as? String
                        let totalAmountDeposited = data["totalAmountDeposited"] as! Double
                        let maturityAmount = data["maturityAmount"] as! Double
                        let totalInterestReceived = data["totalInterestReceived"] as! Double
                        let maturityDate = data["maturityDate"] as! String
                        
                        let bankRecurringLookupList = data["bankFdTdrLookupList"] as! [NSDictionary]
                        
                        let recurringData = BankRecurringModel(depositDate: depositDate ?? "", maturityDate: maturityDate, term: term, amountDepositFreq: amountDepositFreq, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, maturityAmount: maturityAmount, totalInterestReceived: totalInterestReceived, totalAmountDeposited: totalAmountDeposited)
                        
                        for list in bankRecurringLookupList {
                            let serialNo = list.value(forKey: "serialNo") as! NSNumber
                            let referenceDate = list.value(forKey: "referenceDate") as! String
                            let referenceMonth = list.value(forKey: "referenceMonth") as! String
                            let financialYear = list.value(forKey: "financialYear") as! String
                            let openingBal = list.value(forKey: "openingBal") as! Double
                            let amountDeposited = list.value(forKey: "amountDeposited") as! Double
                            let interestAccrued = list.value(forKey: "interestAccrued") as! Double
                            let interestCredited = list.value(forKey: "interestCredited") as! Double
                            let totalInterestAccrued = list.value(forKey: "totalInterestAccrued") as! Double
                            let closingBalance = list.value(forKey: "closingBalance") as! Double
                            let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
                                                        
                            let list = BankRecurringLookupList(serialNo: Int(truncating: serialNo), openingBal: openingBal, amountDeposited: amountDeposited, interestAccrued: interestAccrued, totalInterestAccrued: totalInterestAccrued, interestCredited: interestCredited, daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, closingBalance: closingBalance)
                            
                            recurringData.bankRecurringLookupList.append(list)
                        }
                        self.bankRecurring.append(recurringData)
                        delegate.navigate(recurringData)
                    }
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
}

protocol BankRecurringViewProtocol {
    func startLoader()
    func stopLoader()
    func showError(error:String)
    func navigate(_ data:BankRecurringModel)
}
