import UIKit

fileprivate let cellID = "exampleCell"

class ExamplesViewController: UIViewController {
    
    @IBOutlet weak var integrationKindPicker: UIButton!
    @IBOutlet weak var adFormatPicker: UIButton!
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    
    private let testCases = IntegrationCaseManager.allCases
    private var displayedCases = [IntegrationCase]()
    
    private var filterText = ""
    private var currentIntegrationKind: IntegrationKind?
    private var currentAdFormat: AdFormat?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
        tableView.delegate = self
        searchBar.delegate = self
        
        if let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
            title = appName
        }
        
        displayedCases = testCases
        
        setupPickers()
    }
    
    private func setupPickers() {
        let allIntegrationKindsAction = UIAction(title: "All") { [weak self] _ in
            self?.currentIntegrationKind = nil
            self?.filterTestCases()
        }
        
        let integrationKindActions = IntegrationKind.allCases.map { integration in
            UIAction(title: integration.description) { [weak self] _ in
                self?.currentIntegrationKind = integration
                self?.filterTestCases()
            }
        }
        if CommandLine.arguments.contains("-integrationKindAll") {
            integrationKindPicker.setupPullDown(with: [allIntegrationKindsAction] + integrationKindActions)
        } else {
            integrationKindPicker.setupPullDown(with: integrationKindActions + [allIntegrationKindsAction])
            currentIntegrationKind = IntegrationKind.inApp
            filterTestCases()
        }
        let allAdFormatsAction = UIAction(title: "All") { [weak self] _ in
            self?.currentAdFormat = nil
            self?.filterTestCases()
        }
        
        let adFormatActions = AdFormat.allCases.map { adFormat in
            UIAction(title: adFormat.description) { [weak self] _ in
                self?.currentAdFormat = adFormat
                self?.filterTestCases()
            }
        }
        
        adFormatPicker.setupPullDown(with: [allAdFormatsAction] + adFormatActions)
        
    }
    
    private func filterTestCases() {
        displayedCases = testCases
            .filter { currentIntegrationKind == nil ? true : $0.integrationKind == currentIntegrationKind }
            .filter { currentAdFormat == nil ? true : $0.adFormat == currentAdFormat }
            .filter { filterText.isEmpty || $0.title.range(of: filterText, options: .caseInsensitive) != nil }
        
        tableView.reloadData()
    }
    
    @IBAction func settingsButtonTapped(_ sender: Any) {
        if let settingsViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "SettingsViewController") as? SettingsViewController {
            settingsViewController.title = "Settings"
            navigationController?.pushViewController(settingsViewController, animated: true)
        }
    }
}

// MARK: - UITableViewDelegate & UITableViewDataSource

extension ExamplesViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        displayedCases.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        
        var content = cell.defaultContentConfiguration()
        content.text = displayedCases[indexPath.row].title
        cell.contentConfiguration = content
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let testCase = displayedCases[indexPath.row]
        
        let viewController = testCase.configurationClosure()
        // 不在 push 前访问 view，避免同步触发 loadView/createAd 导致主线程在 Registry 锁上阻塞（第二次点击卡死列表页）
        viewController.title = testCase.title
        
        navigationController?.pushViewController(viewController, animated: true)
    }
}

// MARK: - UISearchBarDelegate

extension ExamplesViewController: UISearchBarDelegate {
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        filterText = searchBar.text ?? ""
        filterTestCases()
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        filterText = searchBar.text ?? ""
        filterTestCases()
        searchBar.endEditing(true)
    }
}
