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

import UIKit

class BankRecurringSummaryViewController: UIViewController {
    
    @IBOutlet var totalAmountDepositedTF: UITextField!
    @IBOutlet var maturityAmountTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var depositMaturityDateTF: UITextField!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var viewDetailedButton: UIButton!
    
    var recurringModel:BankRecurringModel?
    
    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() {
        totalAmountDepositedTF.text = "\(recurringModel!.totalAmountDeposited!)"
        maturityAmountTF.text = "\(recurringModel!.maturityAmount!)"
        overallInterestReceivedTF.text = "\(recurringModel!.totalInterestReceived!)"
        let date = recurringModel!.maturityDate
        let dateString = date?.stringBefore("T")
        depositMaturityDateTF.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 BankRecurringSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        recurringModel?.bankRecurringLookupList.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BankRecurringDetailTableViewCell", for: indexPath) as? BankRecurringDetailTableViewCell
        
        cell?.monthLabel.text = recurringModel?.bankRecurringLookupList[indexPath.row].referenceMonth
        cell?.openingBalanceLabel.text = "\(recurringModel!.bankRecurringLookupList[indexPath.row].openingBal!)"
        cell?.amountDepositedLabel.text = "\(recurringModel!.bankRecurringLookupList[indexPath.row].amountDeposited!)"
        cell?.interestAccuredLabel.text = "\(recurringModel!.bankRecurringLookupList[indexPath.row].interestAccrued!)"
        cell?.totalInterestAccuredLabel.text = "\(recurringModel!.bankRecurringLookupList[indexPath.row].totalInterestAccrued!)"
        cell?.interestCreditedLabel.text = "\(recurringModel!.bankRecurringLookupList[indexPath.row].interestCredited!)"
        cell?.closingBalanceLabel.text = "\(recurringModel!.bankRecurringLookupList[indexPath.row].closingBalance!)"
        
        return cell!
    }
}
