//
//  BondDebenturesSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 30/11/21.
//

import UIKit

class BondDebenturesSummaryViewController: UIViewController {
    
    @IBOutlet var couponReceivedMonthlyTF: UITextField!
    @IBOutlet var totalCouponReceivedTF: UITextField!
    @IBOutlet var bondMaturityDateTF: UITextField!
    @IBOutlet var effectiveTimeTF: UITextField!
    @IBOutlet var currentValueTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var tableView: UITableView!
    
    var bondDebentureModel : BondDebenturesModel?
    var bondDebentureOutputListModel = [BondDebenturesOutputListModel]()
    
    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() {
        couponReceivedMonthlyTF.text = String(format: "%.2f", "\(bondDebentureModel!.couponReceived!)")
        totalCouponReceivedTF.text = String(format: "%.2f", "\(bondDebentureModel!.totalCouponReceived!)")
        let date = bondDebentureModel!.daysToMaturity
        let dateString = date?.stringBefore("T")
        bondMaturityDateTF.text = convertDateFormat(inputDate: dateString!)
        effectiveTimeTF.text = String(format: "%.2f", "\(bondDebentureModel!.effectiveTimeToMaturity!)")
        currentValueTF.text = String(format: "%.2f", "\(bondDebentureModel!.currentValue!)")
    }
    
    
    /*
     // 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 BondDebenturesSummaryViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        bondDebentureOutputListModel.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BondDebenturesTableViewCell", for: indexPath) as? BondDebenturesTableViewCell
        
        cell?.monthLabel.text = bondDebentureOutputListModel[indexPath.row].referenceMonth
        cell?.amountDepositedLabel.text = "\(bondDebentureOutputListModel[indexPath.row].bondAmountDeposited!)"
        cell?.couponReceivedLabel.text = "\(bondDebentureOutputListModel[indexPath.row].couponReceived!)"
        cell?.totalCouponReceivedLabel.text = "\(bondDebentureOutputListModel[indexPath.row].totalCouponReceived!)"
        
        return cell!
    }
}
