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

import UIKit

class POMonthlyIncomeSchemeSummaryViewController: UIViewController {

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