//
//  MenuViewController.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 25.03.2021.
//

import UIKit

class MenuViewController: UIViewController {
    //Private
    private let coordinator:MenuCoordinator
    private let viewModel:MenuViewModel
    private let menuHeaderView = MenuHeaderView()
    private let menuCellFactory:MenuCellFactory<MenuRow>
    private let tableView = UITableView()
    
    init(coordinator:MenuCoordinator, menuViewModel: MenuViewModel) {
        self.coordinator = coordinator
        self.viewModel = menuViewModel
        self.menuCellFactory = MenuCellFactory(viewModel: menuViewModel)
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareController()
        prepareTableView()
        updateUI()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.layoutTableHeaderView()
        tableView.layoutTableFooterView()
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    private func updateUI() {
        view.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        tableView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
}

//MARK:- Prepare
private extension MenuViewController {
    func prepareController() {
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
    
    func prepareTableView() {
        menuCellFactory.registerCells(on: tableView)
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = menuCellFactory.kRowHeight
        tableView.tableFooterView = UIView()
        tableView.tableHeaderView = menuHeaderView
        tableView.tableFooterView = MenuFooterView()
        tableView.contentInset = .init(top: 0, left: 0, bottom: 30, right: 0)
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

//MARK:- UITableView Data Source
extension MenuViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuCellFactory.numberOfRows(inSection: section)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return menuCellFactory.numberOfSections
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return menuCellFactory.cellFromTableView(tableView: tableView, indexPath: indexPath)
    }
    
}

//MARK:- UITableView Delegate
extension MenuViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0
        }
        
        return menuCellFactory.kSectionHeight
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let type = menuCellFactory.cellTypeAt(indexPath: indexPath) else {
            return
        }
        
        if type == .settings {
            coordinator.openSettings()
        }
    }
}
