//
//  SubMenuViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 13/11/21.
//

import UIKit
import SideMenu

class SubMenuViewController: UIViewController {

    @IBOutlet var subMenuCollectionView: UICollectionView!
    
    var menu = Int()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.enableSwipeToPop()
        subMenuCollectionView.reloadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.navigationBar.barStyle = .black
        setupUI()
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    func setupUI() {
        self.navigationItem.title = AppConstant.MENU_TITLE[menu]
        self.navigationItem.hidesBackButton = true
        let menuBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "line.3.horizontal"), style: .plain, target: self, action: #selector(openMenu))
        self.navigationItem.leftBarButtonItem  = menuBarButtonItem
        if #available(iOS 13, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .systemBlue
            appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            
            navigationController?.navigationBar.tintColor = .white
            navigationController?.navigationBar.standardAppearance = appearance
            navigationController?.navigationBar.compactAppearance = appearance
            navigationController?.navigationBar.scrollEdgeAppearance = appearance
        }
    }
    
    @objc func openMenu(_ sender: Any) {
        let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
        present(menu, animated: true, completion: nil)
    }
    

    /*
    // 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.
    }
    */

}

extension SubMenuViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch menu {
        case 0:
            return 4
        case 1:
            return 7
        case 2:
            return 5
        case 3:
            return 2
        default:
            return 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell: SubMenuCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubMenuCollectionViewCell", for: indexPath) as? SubMenuCollectionViewCell else {
            return UICollectionViewCell()
        }
        switch menu {
        case 0:
            cell.cardView()
            cell.imageView.image = UIImage(named: AppConstant.SUB_MENU_IMAGE_1[indexPath.row])
            cell.titleLabel.text = AppConstant.SUB_MENU_TITLE_1[indexPath.row]
        case 1:
            cell.cardView()
            cell.imageView.image = UIImage(named: AppConstant.SUB_MENU_IMAGE_2[indexPath.row])
            cell.titleLabel.text = AppConstant.SUB_MENU_TITLE_2[indexPath.row]
        case 2:
            cell.cardView()
            cell.imageView.image = UIImage(named: AppConstant.SUB_MENU_IMAGE_3[indexPath.row])
            cell.titleLabel.text = AppConstant.SUB_MENU_TITLE_3[indexPath.row]
        case 3:
            cell.cardView()
            cell.imageView.image = UIImage(named: AppConstant.SUB_MENU_IMAGE_4[indexPath.row])
            cell.titleLabel.text = AppConstant.SUB_MENU_TITLE_4[indexPath.row]
        default:
            break
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch menu {
        case 0:
            switch indexPath.row {
            case 0:
                DispatchQueue.main.async {
                    let fixedDepositVC = self.storyboard?.instantiateViewController(withIdentifier: "FixedDepositViewController") as! FixedDepositViewController
                    fixedDepositVC.menu = self.menu
                    self.navigationController?.pushViewController(fixedDepositVC, animated: true)
                }
            case 1:
                DispatchQueue.main.async {
                    let bankRecurringVC = self.storyboard?.instantiateViewController(withIdentifier: "BankRecurringViewController") as! BankRecurringViewController
                    bankRecurringVC.menu = self.menu
                    self.navigationController?.pushViewController(bankRecurringVC, animated: true)
                }
            case 2:
                DispatchQueue.main.async {
                    let bondVC = self.storyboard?.instantiateViewController(withIdentifier: "BondDebenturesViewController") as! BondDebenturesViewController
                    bondVC.menu = self.menu
                    self.navigationController?.pushViewController(bondVC, animated: true)
                }
            case 3:
                DispatchQueue.main.async {
                    let cpcdVC = self.storyboard?.instantiateViewController(withIdentifier: "CpCdViewController") as! CpCdViewController
                    cpcdVC.menu = self.menu
                    self.navigationController?.pushViewController(cpcdVC, animated: true)
                }
            default:
                break
            }
        case 1:
            switch indexPath.row {
            case 0:
                DispatchQueue.main.async {
                    let poNSCVC = self.storyboard?.instantiateViewController(withIdentifier: "PONationalSavingViewController") as! PONationalSavingViewController
                    poNSCVC.menu = self.menu
                    self.navigationController?.pushViewController(poNSCVC, animated: true)
                }
            case 1:
                DispatchQueue.main.async {
                    let kvpVC = self.storyboard?.instantiateViewController(withIdentifier: "KisanVikasPatraViewController") as! KisanVikasPatraViewController
                    kvpVC.menu = self.menu
                    self.navigationController?.pushViewController(kvpVC, animated: true)
                }
            case 2:
                DispatchQueue.main.async {
                    let pomisVC = self.storyboard?.instantiateViewController(withIdentifier: "POMonthlyIncomeSchemeViewController") as! POMonthlyIncomeSchemeViewController
                    pomisVC.menu = self.menu
                    self.navigationController?.pushViewController(pomisVC, animated: true)
                }
            case 3:
                DispatchQueue.main.async {
                    let poRecurringVC = self.storyboard?.instantiateViewController(withIdentifier: "PORecurringDepositViewController") as! PORecurringDepositViewController
                    poRecurringVC.menu = self.menu
                    self.navigationController?.pushViewController(poRecurringVC, animated: true)
                }
            case 4:
                DispatchQueue.main.async {
                    let poTimeVC = self.storyboard?.instantiateViewController(withIdentifier: "POTimeDepositViewController") as! POTimeDepositViewController
                    poTimeVC.menu = self.menu
                    self.navigationController?.pushViewController(poTimeVC, animated: true)
                }
            case 5:
                DispatchQueue.main.async {
                    let sukanyaVC = self.storyboard?.instantiateViewController(withIdentifier: "SukanyaSamriddhiViewController") as! SukanyaSamriddhiViewController
                    sukanyaVC.menu = self.menu
                    self.navigationController?.pushViewController(sukanyaVC, animated: true)
                }
            case 6:
                DispatchQueue.main.async {
                    let scssVC = self.storyboard?.instantiateViewController(withIdentifier: "SeniorCitizenSavingViewController") as! SeniorCitizenSavingViewController
                    scssVC.menu = self.menu
                    self.navigationController?.pushViewController(scssVC, animated: true)
                }
            default:
                break
            }
        case 2:
            switch indexPath.row {
            case 0:
                DispatchQueue.main.async {
                    let ppfVC = self.storyboard?.instantiateViewController(withIdentifier: "PublicProvidentViewController") as! PublicProvidentViewController
                    ppfVC.menu = self.menu
                    self.navigationController?.pushViewController(ppfVC, animated: true)
                }
            case 1:
                DispatchQueue.main.async {
                    let epfVC = self.storyboard?.instantiateViewController(withIdentifier: "EPFViewController") as! EPFViewController
                    epfVC.menu = self.menu
                    self.navigationController?.pushViewController(epfVC, animated: true)
                }
            case 2:
                break
            case 3:
                break
            case 4:
                break
            default:
                break
            }
        case 3:
            switch indexPath.row {
            case 0:
                DispatchQueue.main.async {
                    let emiLoanVC = self.storyboard?.instantiateViewController(withIdentifier: "EMILoanViewController") as! EMILoanViewController
                    emiLoanVC.menu = self.menu
                    self.navigationController?.pushViewController(emiLoanVC, animated: true)
                }
            case 1:
                DispatchQueue.main.async {
                    let nonEMIVC = self.storyboard?.instantiateViewController(withIdentifier: "NonEMIViewController") as! NonEMIViewController
                    nonEMIVC.menu = self.menu
                    self.navigationController?.pushViewController(nonEMIVC, animated: true)
                }
            default:
                break
            }
        default:
            break
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (collectionView.frame.size.width-10)/2, height: 180)
    }
}

extension SubMenuViewController {
    func enableSwipeToPop() {
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
    }
}
