//
//  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()
        
        prepareController()
        prepareTable()
    }
}

//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 {
    
}
