//
//  CpCdSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 30/11/21.
//

import UIKit

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