//
//  MenuCellFactory.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 25.03.2021.
//

import UIKit

public enum MenuRow {
    case settings
    case about
    case ad
    case rateUs
    case help
    case faq
    case privacy
    
    var image: UIImage? {
        switch self {
        case .settings:
            return UIImage(named: "menu_settings")
        case .about:
            return UIImage(named: "menu_about")
        case .ad:
            return UIImage(named: "menu_ad")
        case .rateUs:
            return UIImage(named: "menu_rateUs")
        case .help:
            return UIImage(named: "menu_help")
        case .faq:
            return UIImage(named: "menu_faq")
        case .privacy:
            return UIImage(named: "menu_privacyPolicy")
        }
    }
    
    var localized: String {
        switch self {
        case .settings:
            return "menu.settings".localized()
        case .about:
            return "menu.about".localized()
        case .ad:
            return "menu.ad".localized()
        case .rateUs:
            return "menu.rateUs".localized()
        case .help:
            return "menu.help".localized()
        case .faq:
            return "menu.faq".localized()
        case .privacy:
            return "menu.privacy".localized()
        }
    }
    
    var roundedCorners:CACornerMask {
        switch self {
        case .settings:
            return [.layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        case .about:
            return [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        case .ad:
            return []
        case .rateUs:
            return []
        case .help:
            return []
        case .faq:
            return []
        case .privacy:
            return [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        }
    }
}

private enum Section {
    case settings
    case info
}

private struct SectionItem {
    let type:Section
    let rows:[MenuRow]
}

class MenuCellFactory<T>: CellFactoryProtocol {
    //Private
    private let menuViewModel:MenuViewModel
    private let sections:[SectionItem] = [SectionItem(type: .info, rows: [.settings]),
                                          SectionItem(type: .settings, rows: [.about, .ad, .rateUs, .help, .faq, .privacy])]
    
    //Public
    public var kSectionHeight:CGFloat {
        return 30
    }
    public var kRowHeight:CGFloat {
        return 57
    }
    public var numberOfSections:Int {
        return sections.count
    }
    
    init(viewModel: MenuViewModel) {
        self.menuViewModel = viewModel
    }
    
    public func numberOfRows(inSection section:Int) -> Int {
        switch section {
        case 0:
            return sections[0].rows.count
        case 1:
            return sections[1].rows.count
        case 2:
            return sections[2].rows.count
        default:
            return 0
        }
    }
    
    public func registerCells(on tableView:UITableView) {
        self.registerCell(type: MenuCell.self, tableView: tableView)
    }
    
    public func cellFromTableView(tableView:UITableView, indexPath:IndexPath) -> UITableViewCell {
        let cellType = sections[indexPath.section].rows[indexPath.row]
        
        let cell = self.dequeueReusableCell(type: MenuCell.self, tableView: tableView, indexPath: indexPath)
        cell.configure(image: cellType.image,
                       text: cellType.localized,
                       roundendCorners: cellType.roundedCorners)
        
        
        return cell
    }
    
    func cellTypeAt(indexPath: IndexPath) -> MenuRow? {
        return sections[indexPath.section].rows[indexPath.row]
    }
}
