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

import UIKit

class PONationalSavingSummaryViewController: UIViewController {

    @IBOutlet var maturityAmountTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var investmentMaturityDateTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var tableView: UITableView!
    
    var poNScModel:PONationalSavingModel?
    
    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() {
        maturityAmountTF.text = String(format: "%.2f", "\(poNScModel!.maturityAmount!)")
        overallInterestReceivedTF.text = String(format: "%.2f", "\(poNScModel!.totalInterestReceived!)")
        let date = poNScModel!.maturityDate
        let dateString = date?.stringBefore("T")
        investmentMaturityDateTF.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 PONationalSavingSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        poNScModel?.poNScLookupList.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PONScTableViewCell", for: indexPath) as? PONScTableViewCell
        
        cell?.monthLabel.text = poNScModel?.poNScLookupList[indexPath.row].referenceMonth
        cell?.openingBalanceLabel.text = "\(poNScModel!.poNScLookupList[indexPath.row].openingBal!)"
        cell?.interestAccuredLabel.text = "\(poNScModel!.poNScLookupList[indexPath.row].interestAccrued!)"
        cell?.totalInterestAccuredLabel.text = "\(poNScModel!.poNScLookupList[indexPath.row].totalInterestReceived!)"
        cell?.interestCreditedLabel.text = "\(poNScModel!.poNScLookupList[indexPath.row].interestCredited!)"
        cell?.closingBalanceLabel.text = "\(poNScModel!.poNScLookupList[indexPath.row].closingBalance!)"
        
        return cell!
    }
}
