//
//  SettingsCellFactory.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 26.03.2021.
//

import UIKit

public enum SettingsRow {
    case theme
    case temperature
    case wind
    case pressure
    case distance
    
    var roundedCorners:CACornerMask {
        switch self {
        case .theme:
            return [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner, .layerMaxXMinYCorner]
        case .temperature:
            return [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        case .distance:
            return [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        default:
            return []
        }
    }
    
    var localizedName:String {
        switch self {
        case .theme:
            return "settings.theme".localized()
        case .temperature:
            return "settings.unit.temp".localized()
        case .wind:
            return "settings.unit.wind".localized()
        case .pressure:
            return "settings.unit.pressure".localized()
        case .distance:
            return "settings.unit.distance".localized()
        }
    }
}

public enum SettingsSection {
    case theme
    case units
    case language
    case other
}

private struct SettingsDataSource {
    let section:SettingsSection
    let rows:[SettingsRow]
}

class SettingsCellFactory: CellFactoryProtocol {
    //Private
    private let viewModel:SettingsViewModel
    private let sections:[SettingsDataSource] = [SettingsDataSource(section: .theme, rows: [.theme]),
                                                 SettingsDataSource(section: .units, rows: [.temperature, .wind, .pressure, .distance])]
    
    //Public
    init(viewModel:SettingsViewModel) {
        self.viewModel = viewModel
    }
    
    public var numberOfSections: Int {
        return sections.count
    }
    
    public func numberOfRows(inSection section: Int) -> Int {
        return sections[section].rows.count
    }
    
    public func registerCells(on tableView: UITableView) {
        self.registerCell(type: SettingsThemeCell.self, tableView: tableView)
        self.registerCell(type: SettingsCell.self, tableView: tableView)
    }
    
    public func cellFromTableView(tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
        let type = sections[indexPath.section].rows[indexPath.row]
        
        switch type {
        case .theme:
            return self.dequeueReusableCell(type: SettingsThemeCell.self, tableView: tableView, indexPath: indexPath)
        default:
            let cell = self.dequeueReusableCell(type: SettingsCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(settingsName: type.localizedName, selectedValue: "Some", roundCorners: type.roundedCorners)
            return cell
        }
    }
    
    public func sectionTypeAt(indexPath:IndexPath) -> SettingsSection {
        return sections[indexPath.section].section
    }
}
