//
//  SettingsDetailsViewController.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 27.03.2021.
//

import UIKit

class SettingsDetailsViewController: UIViewController {
    //Private
    private let viewModel:SettingsDetailsViewModel
    private let coordinator:SettingsDetailsCoordinator
    private let cellFactory:SettingsDetailsCellFactory
    private let tableView = UITableView()
    
    init(coordinator: SettingsDetailsCoordinator, settingsRow:SettingsRow) {
        self.coordinator = coordinator
        self.viewModel = SettingsDetailsViewModel(rowType: settingsRow)
        self.cellFactory = SettingsDetailsCellFactory(viewModel: self.viewModel)
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.viewModel.delegate = self
        prepareController()
        prepareTable()
        updateUI()
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    private func updateUI() {
        tableView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        self.view.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
}

//MARK:- Prepare
private extension SettingsDetailsViewController {
    func prepareController() {
        navigationItem.title = viewModel.rowType.localizedName
    }
    
    func prepareTable() {
        cellFactory.registerCells(on: tableView)
        tableView.separatorStyle = .none
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView()
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = UITableView.automaticDimension
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

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

//MARK:- UITableView Delegate
extension SettingsDetailsViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        viewModel.selectUnitAtIndex(index: indexPath.row)
    }
}

//MARK:- ViewModel Delegate
extension SettingsDetailsViewController: ViewModelDelegate {
    func viewModelDidChange<P>(model: P) where P : ViewModelProtocol {
        tableView.reloadData()
    }
}
