//
//  FixedDepositSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 23/11/21.
//

import UIKit

class FixedDepositSummaryViewController: UIViewController {
    
    @IBOutlet var interestReceivedMonthlyTF: UITextField!
    @IBOutlet var overallInterestReceivedTF: UITextField!
    @IBOutlet var depositMaturityDateTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var detailedProjectionTableView: UITableView!
    
    var fdtdrDetails:FDTDRDetailsModel?
    var fdStdrDetails:FDSTDRDetailsModel?

    var fdType = String()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
        setupData()
    }
    
    func setupUI() {
        viewDetailedButton.isHidden = false
        detailedProjectionTableView.isHidden = true
        
    }
    
    func setupData() {
        if fdType == "FDTDR" {
            interestReceivedMonthlyTF.text = "\(fdtdrDetails!.interestReceived!)"
            overallInterestReceivedTF.text = "\(String(describing: fdtdrDetails!.totalInterestReceived!))"
            let date = fdtdrDetails!.maturityDate
            let dateString = date?.stringBefore("T")
            depositMaturityDateTF.text = convertDateFormat(inputDate: dateString!)
        } else {
            interestReceivedMonthlyTF.text = "\(fdStdrDetails!.interestReceived!)"
            overallInterestReceivedTF.text = "\(String(describing: fdStdrDetails!.totalInterestReceived!))"
            let date = fdStdrDetails!.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
        detailedProjectionTableView.isHidden = false
    }
    
}

extension FixedDepositSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var count = Int()
        if fdType == "FDTDR" {
            count = fdtdrDetails?.bankFdTdrLookupList.count ?? 0
        } else {
            count = fdStdrDetails?.bankFdStdrLookupList.count ?? 0
        }
        return count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailedProjectionTableViewCell", for: indexPath) as? DetailedProjectionTableViewCell
        
        if fdType == "FDTDR" {
            cell?.monthLabel.text = fdtdrDetails?.bankFdTdrLookupList[indexPath.row].referenceMonth
            cell?.amountDepositedLabel.text = String(fdtdrDetails!.bankFdTdrLookupList[indexPath.row].amountDeposited!)
            cell?.interestAccuredLabel.text = String(fdtdrDetails!.bankFdTdrLookupList[indexPath.row].interestAccrued!)
            cell?.totalInterestAccuredLabel.text = String(fdtdrDetails!.bankFdTdrLookupList[indexPath.row].totalInterestAccrued!)
            cell?.interestReceivedLabel.text = String(fdtdrDetails!.bankFdTdrLookupList[indexPath.row].interestReceived!)
        } else {
            cell?.monthLabel.text = fdStdrDetails?.bankFdStdrLookupList[indexPath.row].referenceMonth
            cell?.amountDepositedLabel.text = String(fdStdrDetails!.bankFdStdrLookupList[indexPath.row].amountDeposited!)
            cell?.interestAccuredLabel.text = String(fdStdrDetails!.bankFdStdrLookupList[indexPath.row].interestAccrued!)
            cell?.totalInterestAccuredLabel.text = String(fdStdrDetails!.bankFdStdrLookupList[indexPath.row].totalInterestReceived!)
            cell?.interestReceivedLabel.text = String(fdStdrDetails!.bankFdStdrLookupList[indexPath.row].interestCredited!)
        }
                 
        return cell!
    }
}
