//
//  KisanVikasPatraSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 03/12/21.
//

import UIKit

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