//  BehaviorPrefsView.swift
//  Tusker
//
//  Created by Shadowfacts on 6/13/19.
//  Copyright © 2019 Shadowfacts. All rights reserved.
//

import SwiftUI
import Pachyderm

struct BehaviorPrefsView: View {
    @ObservedObject var preferences = Preferences.shared

    var body: some View {
        List {
            untitledSection
            timelineSection
            linksSection
            contentWarningsSection
        }
        .listStyle(.insetGrouped)
        .appGroupedListBackground(container: PreferencesNavigationController.self)
        .navigationBarTitle(Text("Behavior"))
    }
    
    private var untitledSection: some View {
        Section {
            Toggle(isOn: $preferences.confirmBeforeReblog) {
                Text("Require Confirmation Before Reblogging")
            }
        }
        .appGroupedListRowBackground()
    }
    
    private var timelineSection: some View {
        Section {
            Toggle(isOn: $preferences.timelineStateRestoration) {
                Text("Maintain Position Across App Launches")
            }
            
            Picker(selection: $preferences.timelineSyncMode) {
                Text("iCloud").tag(Preferences.TimelineSyncMode.icloud)
                Text("Mastodon").tag(Preferences.TimelineSyncMode.mastodon)
            } label: {
                Text("Sync Timeline Position via")
            }
        } header: {
            Text("Timeline")
        } footer: {
            Text("Syncing via the Mastodon API can be more reliable than iCloud, but is not compatible with the Mastodon web interface. Only the Home timeline can be synced via the Mastodon API.")
        }
        .appGroupedListRowBackground()
    }

    private var linksSection: some View {
        Section(header: Text("Links")) {
            Toggle(isOn: $preferences.openLinksInApps) {
                Text("Open Links in Apps")
            }
            Toggle(isOn: $preferences.useInAppSafari) {
                Text("Use In-App Safari")
            }
            Toggle(isOn: $preferences.inAppSafariAutomaticReaderMode) {
                Text("Always Use Reader Mode in In-App Safari")
            }.disabled(!preferences.useInAppSafari)
        }
        .appGroupedListRowBackground()
    }
    
    private var contentWarningsSection: some View {
        Section(header: Text("Content Warnings")) {
            Toggle(isOn: $preferences.collapseLongPosts) {
                Text("Collapse Long Posts")
            }
            
            Toggle(isOn: $preferences.expandAllContentWarnings) {
                Text("Expand All Content Warnings")
            }
            
            NavigationLink(destination: OppositeCollapseKeywordsView()) {
                Text(preferences.expandAllContentWarnings ? "Collapse Posts with Keywords in CWs" : "Expand Posts with Keywords in CWs")
            }
        }
        .appGroupedListRowBackground()
    }
}

#if DEBUG
struct BehaviorPrefsView_Previews : PreviewProvider {
    static var previews: some View {
        BehaviorPrefsView()
    }
}
#endif
