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

import UIKit

class SCSSSummaryViewController: UIViewController {

    @IBOutlet var interestReceivedMonthlyTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var depositMaturityDateTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var tableView: UITableView!
    
    var scssModel:SeniorCitizenSavingModel?
    
    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() {
        interestReceivedMonthlyTF.text = String(format: "%.2f", "\(scssModel!.interestReceived!)")
        overallInterestReceivedTF.text = String(format: "%.2f", "\(scssModel!.totalInterestReceived!)")
        let date = scssModel!.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 SCSSSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        scssModel?.seniorCitizenSavingSchemeLookupsList.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SeniorCitizenSavingTableViewCell", for: indexPath) as? SeniorCitizenSavingTableViewCell
        
        cell?.monthLabel.text = scssModel!.seniorCitizenSavingSchemeLookupsList[indexPath.row].referenceMonth
        cell?.amountDepositedLabel.text = "\(scssModel!.seniorCitizenSavingSchemeLookupsList[indexPath.row].amountDeposited!)"
        cell?.interestReceivedLabel.text = "\(scssModel!.seniorCitizenSavingSchemeLookupsList[indexPath.row].interestReceived!)"
        cell?.totalInterestReceivedLabel.text = "\(scssModel!.seniorCitizenSavingSchemeLookupsList[indexPath.row].totalInterestReceived!)"
        
        return cell!
    }
}
