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

import UIKit
import Localize_Swift

class SettingsDetailsCellFactory: CellFactoryProtocol {
    //Private
    private let viewModel:SettingsDetailsViewModel
    
    //Public
    public var numberOfSections: Int {
        return 1
    }
    
    init(viewModel: SettingsDetailsViewModel) {
        self.viewModel = viewModel
    }
    
    public func numberOfRows(inSection section: Int) -> Int {
        switch viewModel.rowType {
        case .temperature:
            return viewModel.temperatures.count
        case .wind:
            return viewModel.winds.count
        case .pressure:
            return viewModel.pressures.count
        case .distance:
            return viewModel.distances.count
        case .language:
            return viewModel.languages.count
        default:
            return 0
        }
    }
    
    public func registerCells(on tableView: UITableView) {
        self.registerCell(type: SettingsDetailsCell.self, tableView: tableView)
    }
    
    public func cellFromTableView(tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
        let cell = dequeueReusableCell(type: SettingsDetailsCell.self, tableView: tableView, indexPath: indexPath)
        
        var mask:CACornerMask = []
        switch indexPath.row {
        case 0:
            mask = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        case numberOfRows(inSection: 1) - 1:
            mask = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        default:
            break
        }
        
        if numberOfRows(inSection: 1) == 1 {
            mask = [.layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        }
        
        switch viewModel.rowType {
        case .temperature:
            cell.configure(item: viewModel.temperatures[indexPath.row].name,
                           isSelected: indexPath.row == viewModel.selectedTemperatureIndex,
                           roundedConrers: mask)
        case .wind:
            cell.configure(item: viewModel.winds[indexPath.row].name,
                           isSelected: indexPath.row == viewModel.selectedWindIndex,
                           roundedConrers: mask)
        case .pressure:
            cell.configure(item: viewModel.pressures[indexPath.row].name,
                           isSelected: indexPath.row == viewModel.selectedPressureIndex,
                           roundedConrers: mask)
        case .distance:
            cell.configure(item: viewModel.distances[indexPath.row].name,
                           isSelected: indexPath.row == viewModel.selectedDistanceIndex,
                           roundedConrers: mask)
        case .language:
            cell.configure(item: Localize.displayNameForLanguage(viewModel.languages[indexPath.row]),
                           isSelected: indexPath.row == viewModel.selectedLanguageIndex,
                           roundedConrers: mask)
        default:
            break
        }
        
        return cell
    }
}
