//
//  FaqVC.swift
//  WOKA
//
//  Created by MacBook Pro on 03/06/24.
//

import UIKit

class FaqVC: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var vm = FaqVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
        let color1 = #colorLiteral(red: 0.05490196078, green: 0.01176470588, blue: 0.3882352941, alpha: 1)
        let color2 = #colorLiteral(red: 0.2041364683, green: 0.156624428, blue: 0.5904380268, alpha: 1)
        self.view.applyMultiGradient(colors: [color1, color2,color1], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 0.8))
        self.title = "FAQs"
        setupCell()
        
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        
        self.navigationController?.setColor(color: .white)
    }
    
    func setupCell(){
        tableView.register(UINib(nibName: K.CellIdentifier.SideBarNav.faqCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.SideBarNav.faqCell)
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }

    override func viewDidDisappear(_ animated: Bool) {
        self.navigationController?.setColor(color: .black)
    }
}

// MARK: - TableView DataSource , Delegates

extension FaqVC : TableViewSRC{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return vm.faqData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.SideBarNav.faqCell) as! FaqCell
        cell.setData(data: vm.faqData[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        PersistentStorage.shared.addOthersCount()
        var index = [IndexPath]()
        if let lastIndex = vm.lastIndex{
            if lastIndex == indexPath.row{
                vm.faqData[indexPath.row].isExpanded?.toggle()
                self.tableView.reloadRows(at: [indexPath], with: .fade)
                return
            }
            vm.faqData[lastIndex].isExpanded = false
            index.append(IndexPath(row: lastIndex, section: 0))
        }
        
        index.append(indexPath)
        vm.faqData[indexPath.row].isExpanded = true
        
        self.tableView.reloadRows(at: index, with: .fade)
        vm.lastIndex = indexPath.row
    }
}
