//
//  RegionSelectViewController.swift
//  SummerBagFinder
//
//  Created by EUIGYOM KIM on 2020/06/21.
//  Copyright (c) 2020 EUIGYOM KIM. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit
import RxSwift
import RxCocoa

protocol RegionSelectDisplayLogic: class {
    func displayRegionList()
    func displayStoreList()
}

class RegionSelectViewController: UIViewController {
    
    private struct Constant {
        static var regionCellIdentifier: String     = "RegionCell"
    }
    
    var interactor: RegionSelectBusinessLogic?
    var router: (NSObjectProtocol & RegionSelectRoutingLogic & RegionSelectDataPassing)?
    
    var dataStore: RegionSelectDataStore?
    
    lazy var tableView: UITableView = {
        let tableView = UITableView()
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: Constant.regionCellIdentifier)
        tableView.delegate = self
        tableView.dataSource = self
        
        return tableView
    }()
    
    // MARK: Object lifecycle
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    
    // MARK: View lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        setupTableView()
        
        interactor?.doFetchRegions()
    }
}

// MARK: - Setup

extension RegionSelectViewController {
    private func setup() {
        let viewController = self
        let interactor = RegionSelectInteractor()
        let presenter = RegionSelectPresenter()
        let router = RegionSelectRouter()
        viewController.interactor = interactor
        viewController.dataStore = interactor
        viewController.router = router
        interactor.presenter = presenter
        presenter.viewController = viewController
        router.viewController = viewController
        router.dataStore = interactor
    }
    
    private func setupViews() {
        self.title = "지역 선택"
        self.edgesForExtendedLayout = [.all]
        
        view.backgroundColor = .systemBackground
    }
    
    private func setupTableView() {
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (maker) in
            maker.top.leading.trailing.equalTo(view.safeAreaLayoutGuide)
            maker.bottom.equalToSuperview()
        }
    }
}

extension RegionSelectViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataStore?.regions.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataStore?.regions[section].subregions.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return dataStore?.regions[section].name
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Constant.regionCellIdentifier, for: indexPath)
        
        let region = dataStore?.regions[indexPath.section].subregions[indexPath.row]
        cell.textLabel?.text = region?.name
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        interactor?.doMoveToStoreList(RegionSelect.MoveStoreList.Request(indexPath: indexPath))
    }
}

extension RegionSelectViewController: RegionSelectDisplayLogic {
    func displayRegionList() {
        tableView.reloadData()
    }
    
    func displayStoreList() {
        router?.routeToStoreList()
    }
}
