import UIKit

// FIXME: solve linter suppressed issues
// swiftlint: disable force_unwrapping
// swiftlint: disable force_cast
class ExperimentalSettingsViewController: UITableViewController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let setting = SettingsEnum(rawValue: indexPath.row)!
        switch setting {
        case .notificationsFromUnknown:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XmppPipeliningTableViewCell", for: indexPath) as! SwitchTableViewCell
            cell.switchView.isOn = Settings.xmppPipelining.getBool()
            cell.valueChangedListener = { (switchView: UISwitch) in
                Settings.xmppPipelining.setValue(switchView.isOn)
            }
            return cell

        case .enableBookmarksSync:
            let cell = tableView.dequeueReusableCell(withIdentifier: "EnableBookmarksSyncTableViewCell", for: indexPath) as! SwitchTableViewCell
            cell.switchView.isOn = Settings.enableBookmarksSync.getBool()
            cell.valueChangedListener = { (switchView: UISwitch) in
                Settings.enableBookmarksSync.setValue(switchView.isOn)
            }
            return cell

        case .enableMarkdown:
            let cell = tableView.dequeueReusableCell(withIdentifier: "EnableMarkdownTableViewCell", for: indexPath) as! SwitchTableViewCell
            cell.switchView.isOn = Settings.enableMarkdownFormatting.getBool()
            cell.valueChangedListener = { (switchView: UISwitch) in
                Settings.enableMarkdownFormatting.setValue(switchView.isOn)
                if !switchView.isOn {
                    Settings.showEmoticons.setValue(false)
                }
                self.tableView.reloadData()
            }
            return cell

        case .showEmoticons:
            let cell = tableView.dequeueReusableCell(withIdentifier: "EnableEmoticonsTableViewCell", for: indexPath) as! SwitchTableViewCell
            cell.switchView.isOn = Settings.showEmoticons.getBool()
            cell.switchView.isEnabled = Settings.enableMarkdownFormatting.getBool()
            cell.valueChangedListener = { (switchView: UISwitch) in
                Settings.showEmoticons.setValue(switchView.isOn)
            }
            return cell

        case .usePublicStunServers:
            let cell = tableView.dequeueReusableCell(withIdentifier: "PublicStunServersTableViewCell", for: indexPath) as! SwitchTableViewCell
            cell.switchView.isOn = Settings.usePublicStunServers.getBool()
            cell.switchView.isEnabled = true
            cell.valueChangedListener = { (switchView: UISwitch) in
                Settings.usePublicStunServers.setValue(switchView.isOn)
            }
            return cell
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    }

    internal enum SettingsEnum: Int {
        case notificationsFromUnknown = 0
        case enableBookmarksSync = 1
        case enableMarkdown = 2
        case showEmoticons = 3
        case usePublicStunServers = 4
    }
}
