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

import Foundation
import Alamofire

class EPFViewModel {
    
    var delegate: EPFViewProtocol?
    
    var epfModel = [EPFModel]()
    
    //POST EPF Interest Rate API
    func getInterestRate() {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_EPF_INTEREST_RATE
            
            AF.request(url, method: .get, 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 EPF Growth Rate API
    func getGrowthRate() {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_EPF_GROWTH_RATE
            
            AF.request(url, method: .get, 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.setGrowthRate(rate: String(interestRate))
                    }
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
    //POST EPF API
    func getEPFOutput(clientDOB:String, currEPFBal:String, currEPSBal:String, monthlyBasicDA:String, expectedIncreaseSal:String, contributionUptoAge:String, epfWithdrawAlage:String, increaseMonth:String) {
        if let delegate = delegate {
            delegate.startLoader()
            let url = ApiUrl.BASE_URL + ApiUrl.GET_EPF_DETAILS
                        
            let parameters: [String: String] = [
                "clientDOB": clientDOB,
                "currEPFBal": currEPFBal,
                "currEPSBal": currEPSBal,
                "monthlyBasicDA": monthlyBasicDA,
                "expectedIncreaseSal": expectedIncreaseSal,
                "contributionUptoAge": contributionUptoAge,
                "epfWithdrawAlage": epfWithdrawAlage,
                "increaseMonth": increaseMonth,
            ]
            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 clientDOB = data["clientDOB"] as? String
                        let currEPF = data["currEPF"] as! Double
                        let currEPS = data["currEPS"] as! Double
                        let monBasicDA = data["monBasicDA"] as! Double
                        let expSalInc = data["expSalInc"] as! Double
                        let expEPFO = data["expEPFO"] as! Double
                        let conUptoAge = data["conUptoAge"] as! Int
                        let epfWiDAge = data["epfWiDAge"] as! Int
                        let planDate = data["planDate"] as? String
                        let salIncreaseMonth = data["salIncreaseMonth"] as! Int
                        let epfBalWithdraw = data["epfBalWithdraw"] as! Double
                        let epsBalClientAge = data["epsBalClientAge"] as! Double
                        let totalInterestEarned = data["totalInterestEarned"] as! Double
                        let employeeCont = data["employeeCont"] as! Double
                        let employerCont = data["employerCont"] as! Double
                        let currentInterestRate = data["currentInterestRate"] as! Double
                        
                        let lookupList = data["lookupList"] as! [NSDictionary]
                        
                        let epfData = EPFModel(conUptoAge: conUptoAge, epfWiDAge: epfWiDAge, salIncreaseMonth: salIncreaseMonth, currEPF: currEPF, currEPS: currEPS, monBasicDA: monBasicDA, expSalInc: expSalInc, expEPFO: expEPFO, epfBalWithdraw: epfBalWithdraw, epsBalClientAge: epsBalClientAge, totalInterestEarned: totalInterestEarned, employeeCont: employeeCont, employerCont: employerCont, currentInterestRate: currentInterestRate)
                        
                        for list in lookupList {
                            let serialNumber = list.value(forKey: "serialNumber") as! NSNumber
                            let refDate = list.value(forKey: "refDate") as! String
                            let financialYear = list.value(forKey: "financialYear") as! String
                            let referenceMonth = list.value(forKey: "referenceMonth") as! String
                            let clientAge = list.value(forKey: "clientAge") as! Int
                            let monBasicDA = list.value(forKey: "monBasicDA") as! Double
                            let openingBalEPF = list.value(forKey: "openingBalEPF") as! Double
                            let openingBalEPS = list.value(forKey: "openingBalEPS") as! Double
                            let employeeContEPF = list.value(forKey: "employeeContEPF") as! Double
                            let employerContEPF = list.value(forKey: "employerContEPF") as! Double
                            let employerContEPS = list.value(forKey: "employerContEPS") as! Double
                            let interestRateEPF = list.value(forKey: "interestRateEPF") as! Double
                            let interestEarnedEPF = list.value(forKey: "interestEarnedEPF") as! Double
                            let totalInterestEarnedEPF = list.value(forKey: "totalInterestEarnedEPF") as! Double
                            let closingBalEPF = list.value(forKey: "closingBalEPF") as! Double
                            let closingBalEPS = list.value(forKey: "closingBalEPS") as! Double
                            let displayDate = list.value(forKey: "displayDate") as! String
                                                        
                            let list = EPFLookupList(serialNumber: Int(truncating: serialNumber), clientAge: clientAge, refDate: refDate, financialYear: financialYear, referenceMonth: referenceMonth, displayDate: displayDate, monBasicDA: monBasicDA, openingBalEPF: openingBalEPF, openingBalEPS: openingBalEPS, employeeContEPF: employeeContEPF, employerContEPF: employerContEPF, employerContEPS: employerContEPS, interestRateEPF: interestRateEPF, interestEarnedEPF: interestEarnedEPF, totalInterestEarnedEPF: totalInterestEarnedEPF, closingBalEPF: closingBalEPF, closingBalEPS: closingBalEPS)
                            
                            epfData.lookupList.append(list)
                        }
                        self.epfModel.append(epfData)
                        delegate.navigate(epfData)
                    }
                    
                case .failure(let error):
                    delegate.stopLoader()
                    print(error)
                    //delegate.showError(error: error)
                }
            }
        }
    }
    
}

protocol EPFViewProtocol {
    func startLoader()
    func stopLoader()
    func showError(error:String)
    func setInterestRate(rate:String)
    func setGrowthRate(rate:String)
    func navigate(_ data:EPFModel)
}
