//
//  TodayCellFactory.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 03.03.2021.
//

import UIKit

private enum TodayCellType:Int, CaseIterable {
    case forecast = 0
    case ad
    case conditions
    case forecastPeriod
    case precipitation
    case dayTime
    case sun
    case moon
}

class TodayCellFactory: CellFactoryProtocol {
    public var onGetLocation:(() -> Location?)?
    public var numberOfSections: Int {
        return 1
    }
    
    public func numberOfRows(inSection section: Int) -> Int {
        return TodayCellType.allCases.count
    }
    
    public func registerCells(on tableView:UITableView) {
        registerCell(type: CityForecastCell.self, tableView: tableView)
        registerCell(type: TodayAdCell.self, tableView: tableView)
        registerCell(type: CityConditionsCell.self, tableView: tableView)
        registerCell(type: CityForecastTimePeriodCell.self, tableView: tableView)
        registerCell(type: PrecipitationCell.self, tableView: tableView)
        registerCell(type: CityDayTimesCell.self, tableView: tableView)
        registerCell(type: CityAirQualityCell.self, tableView: tableView)
        registerCell(type: CitySunCell.self, tableView: tableView)
        registerCell(type: CityMoonCell.self, tableView: tableView)
    }
    
    public func cellFromTableView(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell {
        guard let cellType = TodayCellType(rawValue: indexPath.row) else {
            return UITableViewCell()
        }
        
        guard let loc = self.onGetLocation?() else {
            return UITableViewCell()
        }
                
        switch cellType {
        case .forecast:
            let cell = dequeueReusableCell(type: CityForecastCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .ad:
            let cell = dequeueReusableCell(type: TodayAdCell.self, tableView: tableView, indexPath: indexPath)
            return cell
        case .conditions:
            let cell = dequeueReusableCell(type: CityConditionsCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .forecastPeriod:
            let cell = dequeueReusableCell(type: CityForecastTimePeriodCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .precipitation:
            let cell = dequeueReusableCell(type: PrecipitationCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc.daily)
            return cell
        case .dayTime:
            let cell = dequeueReusableCell(type: CityDayTimesCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .sun:
            let cell = dequeueReusableCell(type: CitySunCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .moon:
            let cell = dequeueReusableCell(type: CityMoonCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        }
    }
    
    public func willDisplay(cell:UITableViewCell) {
        switch cell {
        case let sunCell as CitySunCell:
            sunCell.updateSunPosition()
        case let moonCell as CityMoonCell:
            moonCell.updateMoonPosition()
        default:
            break
        }
    }
}
