//
//  SukanyaSamriddhiSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 06/12/21.
//

import UIKit

class SukanyaSamriddhiSummaryViewController: UIViewController {
    
    @IBOutlet var totalAmountDepositedTF: UITextField!
    @IBOutlet var maturityAmountTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var paymentUptoDateTF: UITextField!
    @IBOutlet var depositMaturityDateTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var tableView: UITableView!
    
    var sukanyaModel:SukanyaSamriddhiModel?
    
    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", "\(sukanyaModel!.totalAmountDeposited!)")
        maturityAmountTF.text = String(format: "%.2f", "\(sukanyaModel!.maturityAmount!)")
        overallInterestReceivedTF.text = String(format: "%.2f", "\(sukanyaModel!.totalInterestReceived!)")
        let paymentDate = sukanyaModel!.paymentMaturityDate
        let paymentDateString = paymentDate?.stringBefore("T")
        paymentUptoDateTF.text = convertDateFormat(inputDate: paymentDateString!)
        let date = sukanyaModel!.depositMaturityDate
        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 SukanyaSamriddhiSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        sukanyaModel?.sukanyaSamSchLookupList.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SukanyaSamriddhiTableViewCell", for: indexPath) as? SukanyaSamriddhiTableViewCell
        
        cell?.monthLabel.text = sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].referenceMonth
        cell?.openingBalanceLabel.text = "\(sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].openingBal!)"
        cell?.amountDepositedLabel.text = "\(sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].amountDeposited!)"
        cell?.interestAccuredLabel.text = "\(sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].interestAccrued!)"
        cell?.totalInterestAccuredLabel.text = "\(sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].totalInterestAccrued!)"
        cell?.interestCreditedLabel.text = "\(sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].interestCredited!)"
        cell?.closingBalanceLabel.text = "\(sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].closingBalance!)"
        cell?.interestRateLabel.text = "\(sukanyaModel!.sukanyaSamSchLookupList[indexPath.row].interestRate!)"
        
        return cell!
    }
}
