//
//  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: TodayForecastCell.self, tableView: tableView)
        registerCell(type: TodayAdCell.self, tableView: tableView)
        registerCell(type: TodayConditionsCell.self, tableView: tableView)
        registerCell(type: TodayForecastTimePeriodCell.self, tableView: tableView)
        registerCell(type: PrecipitationCell.self, tableView: tableView)
        registerCell(type: TodayDayTimesCell.self, tableView: tableView)
        registerCell(type: TodayAirQualityCell.self, tableView: tableView)
        registerCell(type: SunPhaseCell.self, tableView: tableView)
        registerCell(type: MoonPhaseCell.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: TodayForecastCell.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: TodayConditionsCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .forecastPeriod:
            let cell = dequeueReusableCell(type: TodayForecastTimePeriodCell.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: TodayDayTimesCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .sun:
            let cell = dequeueReusableCell(type: SunPhaseCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        case .moon:
            let cell = dequeueReusableCell(type: MoonPhaseCell.self, tableView: tableView, indexPath: indexPath)
            cell.configure(with: loc)
            return cell
        }
    }
    
    public func willDisplay(cell:UITableViewCell) {
        switch cell {
        case let sunCell as SunPhaseCell:
            sunCell.updateSunPosition()
        case let moonCell as MoonPhaseCell:
            moonCell.updateMoonPosition()
        default:
            break
        }
    }
}
