//
//  EPFSummaryViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 09/12/21.
//

import UIKit

class EPFSummaryViewController: UIViewController {
    
    @IBOutlet var epfWithdrawalTF: UITextField!
    @IBOutlet var epsClientAgeTF: UITextField!
    @IBOutlet var totalInterestOnEpfTF: UITextField!
    @IBOutlet var employeeContributionTF: UITextField!
    @IBOutlet var employerContributionTF: UITextField!
    @IBOutlet var viewDetailedButton: UIButton!
    @IBOutlet var collectionView: UICollectionView!
    
    var epfModel : EPFModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
        setupData()
    }
    
    func setupUI() {
        viewDetailedButton.isHidden = false
        collectionView.isHidden = true
    }
    
    func setupData() {
        epfWithdrawalTF.text = String(format: "%.2f", "\(epfModel!.epfBalWithdraw!)")
        epsClientAgeTF.text = String(format: "%.2f", "\(epfModel!.epsBalClientAge!)")
        totalInterestOnEpfTF.text = String(format: "%.2f", "\(epfModel!.totalInterestEarned!)")
        employeeContributionTF.text = String(format: "%.2f", "\(epfModel!.employeeCont!)")
        employerContributionTF.text = String(format: "%.2f", "\(epfModel!.employerCont!)")
    }
    

    /*
    // 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 viewDetailedButtonAction(_ sender: Any) {
        viewDetailedButton.isHidden = true
        collectionView.isHidden = false
    }
    
}

// MARK: - UICollectionViewDataSource
extension EPFSummaryViewController: UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return epfModel?.lookupList.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 13
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // swiftlint:disable force_cast
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DataCollectionViewCell", for: indexPath) as! DataCollectionViewCell
        
        if indexPath.section % 2 != 0 {
            cell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
        } else {
            cell.backgroundColor = UIColor.white
        }
        
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                cell.contentLabel.text = "Month"
            } else if indexPath.row == 1 {
                cell.contentLabel.text = "Client Age"
            } else if indexPath.row == 2 {
                cell.contentLabel.text = "Monthly Basic/Basic+DA"
            } else if indexPath.row == 3 {
                cell.contentLabel.text = "Opening Balance(EPF)"
            } else if indexPath.row == 4 {
                cell.contentLabel.text = "Opening Balance(EPS)"
            } else if indexPath.row == 5 {
                cell.contentLabel.text = "Employee Contribution(EPF)"
            } else if indexPath.row == 6 {
                cell.contentLabel.text = "Employer Contribution(EPF)"
            } else if indexPath.row == 7 {
                cell.contentLabel.text = "Employer Contribution(EPS)"
            } else if indexPath.row == 8 {
                cell.contentLabel.text = "Interest Rate(EPF)"
            } else if indexPath.row == 9 {
                cell.contentLabel.text = "Interest Earned(EPF)"
            } else if indexPath.row == 10 {
                cell.contentLabel.text = "Total Interest Earned(EPF)"
            } else if indexPath.row == 11 {
                cell.contentLabel.text = "Closing Balance(EPF)"
            } else if indexPath.row == 12 {
                cell.contentLabel.text = "Closing Balance(EPS)"
            }
        } else {
            if indexPath.row == 0 {
                cell.contentLabel.text = epfModel?.lookupList[indexPath.section-1].displayDate
                print(epfModel?.lookupList[indexPath.section-1].displayDate!)
            } else if indexPath.row == 1 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].clientAge! ?? 0)")
            } else if indexPath.row == 2 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].monBasicDA! ?? 0)")
            } else if indexPath.row == 3 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].openingBalEPF! ?? 0)")
                print(String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].openingBalEPF! ?? 0)"))
            } else if indexPath.row == 4 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].openingBalEPS! ?? 0)")
            } else if indexPath.row == 5 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].employeeContEPF! ?? 0)")
            } else if indexPath.row == 6 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].employerContEPF! ?? 0)")
            } else if indexPath.row == 7 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].employerContEPS! ?? 0)")
            } else if indexPath.row == 8 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].interestRateEPF! ?? 0)")
            } else if indexPath.row == 9 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].interestEarnedEPF! ?? 0)")
            } else if indexPath.row == 10 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].totalInterestEarnedEPF! ?? 0)")
            } else if indexPath.row == 11 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].closingBalEPF! ?? 0)")
            } else if indexPath.row == 12 {
                cell.contentLabel.text = String(format: "%.2f", "\(epfModel?.lookupList[indexPath.section-1].closingBalEPS! ?? 0)")
            }
        }
        
        return cell
    }
    
}

// MARK: - UICollectionViewDelegate
extension EPFSummaryViewController: UICollectionViewDelegate {
    
}
