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

import UIKit

class PPFSummaryViewController: UIViewController {
    
    @IBOutlet var annualDepositAmountTF: UITextField!
    @IBOutlet var totalAmountDepositedTF: UITextField!
    @IBOutlet var maturityAmountTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var ppfMaturityDateTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var tableView: UITableView!
    
    var ppfModel:PublicProvidentModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        setupUI()
        setupData()
    }
    
    func setupUI() {
        viewDetailedButton.isHidden = false
        tableView.isHidden = true
    }
    
    func setupData() {
        annualDepositAmountTF.text = String(format: "%.2f", "\(ppfModel!.annualAmountDeposited!)")
        totalAmountDepositedTF.text = String(format: "%.2f", "\(ppfModel!.totalAmountDeposited!)")
        maturityAmountTF.text = String(format: "%.2f", "\(ppfModel!.maturityAmount!)")
        overallInterestReceivedTF.text = String(format: "%.2f", "\(ppfModel!.totalInterestReceived!)")
        let date = ppfModel!.maturityDate
        let dateString = date?.stringBefore("T")
        ppfMaturityDateTF.text = convertDateFormat(inputDate: dateString!)
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
    @IBAction func viewDetailedProjectionAction(_ sender: Any) {
        viewDetailedButton.isHidden = true
        tableView.isHidden = false
    }
    
}

extension PPFSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        ppfModel?.ppfFixedAmountLookupList.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PPFTableViewCell", for: indexPath) as? PPFTableViewCell
        
        cell?.monthLabel.text = ppfModel!.ppfFixedAmountLookupList[indexPath.row].referenceMonth
        cell?.openingBalanceLabel.text = String(format: "%.2f", "\(ppfModel!.ppfFixedAmountLookupList[indexPath.row].openingBal!)")
        cell?.amountDepositedLabel.text = String(format: "%.2f", "\(ppfModel!.ppfFixedAmountLookupList[indexPath.row].amountDeposited!)")
        cell?.interestRateLabel.text = String(format: "%.2f", "\(ppfModel!.ppfFixedAmountLookupList[indexPath.row].interestRate!)")
        cell?.interestEarnedLabel.text = String(format: "%.2f", "\(ppfModel!.ppfFixedAmountLookupList[indexPath.row].interestEarned!)")
        cell?.totalInterestAccuredLabel.text = String(format: "%.2f", "\(ppfModel!.ppfFixedAmountLookupList[indexPath.row].totalInterestAccrued!)")
        cell?.interestCreditedLabel.text = String(format: "%.2f", "\(ppfModel!.ppfFixedAmountLookupList[indexPath.row].interestCredited!)")
        cell?.closingBalanceLabel.text = String(format: "%.2f", "\(ppfModel!.ppfFixedAmountLookupList[indexPath.row].closingBalance!)")
        
        return cell!
    }
}
