//
//  POTimeSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 05/12/21.
//

import UIKit

class POTimeSummaryViewController: UIViewController {
    
    @IBOutlet var interestReceivedAnnuallyTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var depositMaturityDateTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var tableView: UITableView!

    var poTimeModel:POTimeDepositModel?
    
    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() {
        interestReceivedAnnuallyTF.text = String(format: "%.2f", "\(poTimeModel!.annualInterest!)")
        overallInterestReceivedTF.text = String(format: "%.2f", "\(poTimeModel!.totalInterestReceived!)")
        let date = poTimeModel!.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 POTimeSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        poTimeModel?.poTimeDepositLookupList.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "POTimeDepositTableViewCell", for: indexPath) as? POTimeDepositTableViewCell
        
        cell?.monthLabel.text = poTimeModel!.poTimeDepositLookupList[indexPath.row].referenceMonth
        cell?.openingBalanceLabel.text = "\(poTimeModel!.poTimeDepositLookupList[indexPath.row].openingBal!)"
        cell?.amountDepositedLabel.text = "\(poTimeModel!.poTimeDepositLookupList[indexPath.row].amountDeposited!)"
        cell?.interestAccuredLabel.text = "\(poTimeModel!.poTimeDepositLookupList[indexPath.row].interestAccrued!)"
        cell?.interestPaidLabel.text = "\(poTimeModel!.poTimeDepositLookupList[indexPath.row].interestPaid!)"
        cell?.totalInterestReceived.text = "\(poTimeModel!.poTimeDepositLookupList[indexPath.row].totalInterestReceived!)"
        cell?.closingBalanceLabel.text = "\(poTimeModel!.poTimeDepositLookupList[indexPath.row].closingBalance!)"
        
        return cell!
    }
}
