//
//  PORecurringSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 04/12/21.
//

import UIKit

class PORecurringSummaryViewController: UIViewController {

    @IBOutlet var totalAmountDepositedTF: UITextField!
    @IBOutlet var maturityAmountTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var depositMaturityDateTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var tableView: UITableView!
    
    var poRecurringModel:PORecurringDepositModel?
    
    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 = String(format: "%.2f", "\(poRecurringModel!.totalAmountDeposited!)")
        maturityAmountTF.text = String(format: "%.2f", "\(poRecurringModel!.maturityAmount!)")
        overallInterestReceivedTF.text = String(format: "%.2f", "\(poRecurringModel!.totalInterestReceived!)")
        let date = poRecurringModel!.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 PORecurringSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        poRecurringModel?.poRecurringLookupList.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PORecurringDepositTableViewCell", for: indexPath) as? PORecurringDepositTableViewCell
        
        cell?.monthLabel.text = poRecurringModel!.poRecurringLookupList[indexPath.row].referenceMonth
        cell?.openingBalanceLabel.text = "\(poRecurringModel!.poRecurringLookupList[indexPath.row].openingBal!)"
        cell?.amountDepositedLabel.text = "\(poRecurringModel!.poRecurringLookupList[indexPath.row].amountDeposited!)"
        cell?.interestAccuredLabel.text = "\(poRecurringModel!.poRecurringLookupList[indexPath.row].interestAccrued!)"
        cell?.totalInterestAccuredLabel.text = "\(poRecurringModel!.poRecurringLookupList[indexPath.row].totalInterestAccrued!)"
        cell?.interestCreditedLabel.text = "\(poRecurringModel!.poRecurringLookupList[indexPath.row].interestCredited!)"
        cell?.closingBalanceLabel.text = "\(poRecurringModel!.poRecurringLookupList[indexPath.row].closingBalance!)"
        
        return cell!
    }
}
