//
//  KisanVikasPatraViewModel.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 03/12/21.
//

import Foundation
import Alamofire

class KisanVikasPatraViewModel {
    
    var delegate: KisanVikasPatraViewProtocol?
    
    var kvpModel = [KisanVikasPatraModel]()
    
    //POST PO National Saving Certificates Interest API
    func getKVPInterest(depositDate:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_KVP_INTEREST_RATE
           
            let parameters: [String: String] = [
                "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 interestRate = data["interestRate"] as! Double
                        
                        delegate.setInterestRate(rate: String(interestRate))
                    }
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
    //POST Kisan vikas patra API
    func getKVPOutput(depositAmount:String, depositDate:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_KISAN_VIKAS_PATRA
                        
            let parameters: [String: String] = [
                "deposit": depositAmount,
                "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 annualInterest = data["annualInterest"] as! Double
                        let term = data["term"] as! Int
                        let compundFreq = data["compundFreq"] as! Double
                        let depositDate = data["depositDate"] as? String
                        let maturityAmount = data["maturityAmount"] as! Double
                        let interestReceived = data["interestReceived"] as! Double
                        let totalInterestReceived = data["totalInterestReceived"] as! Double
                        let interestCredited = data["interestCredited"] as! Double
                        let maturityDate = data["maturityDate"] as? String
                        
                        let kisanVikasPatraLookupList = data["kisanVikasPatraLookupList"] as! [NSDictionary]
                        
                        let kvpData = KisanVikasPatraModel(depositDate: depositDate ?? "", maturityDate: maturityDate ?? "", term: term, depositAmount: depositAmount, annualInterest: annualInterest, compundFreq: compundFreq, maturityAmount: maturityAmount, totalInterestReceived: totalInterestReceived, interestReceived: interestReceived, interestCredited: interestCredited)
                        
                        for list in kisanVikasPatraLookupList {
                            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 yearOfInvestment = list.value(forKey: "yearOfInvestment") as! Double
                            let interestRate = list.value(forKey: "interestRate") as! Double
                            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 interestReceived = list.value(forKey: "interestReceived") as! Double
                            let totalInterestReceived = list.value(forKey: "totalInterestReceived") as! Double
                            let closingBalance = list.value(forKey: "closingBalance") as! Double
                            let daysToMaturity = list.value(forKey: "daysToMaturity") as! NSNumber
                                                        
                            let list = KVPLookupList(serialNo: Int(truncating: serialNo), openingBal: openingBal, amountDeposited: amountDeposited, interestAccrued: interestAccrued, totalInterestReceived: totalInterestReceived, interestReceived: interestReceived, daysToMaturity: Int(truncating: daysToMaturity), referenceDate: referenceDate, referenceMonth: referenceMonth, financialYear: financialYear, closingBalance: closingBalance, yearOfInvestment: yearOfInvestment, interestRate: interestRate)
                            
                            kvpData.kisanVikasPatraLookupList.append(list)
                        }
                        self.kvpModel.append(kvpData)
                        delegate.navigate(kvpData)
                    }
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
}

protocol KisanVikasPatraViewProtocol {
    func startLoader()
    func stopLoader()
    func showError(error:String)
    func setInterestRate(rate:String)
    func navigate(_ data:KisanVikasPatraModel)
}
