//
//  MediaBrowserViewOptionsSheet.swift
//  Yattee
//
//  Sheet for customizing media browser view options.
//

import SwiftUI

struct MediaBrowserViewOptionsSheet: View {
    @Environment(\.dismiss) private var dismiss

    let sourceType: MediaSourceType

    @Binding var sortOrder: MediaBrowserSortOrder
    @Binding var sortAscending: Bool
    @Binding var showOnlyPlayable: Bool

    private var availableSortOptions: [MediaBrowserSortOrder] {
        MediaBrowserSortOrder.availableOptions(for: sourceType)
    }

    var body: some View {
        #if os(macOS)
        // Popover content: no navigation chrome, click-outside dismisses.
        formContent
            .formStyle(.grouped)
            .frame(width: 300)
            .onAppear {
                // Reset sort order if current selection is not available for this source type
                if !availableSortOptions.contains(sortOrder) {
                    sortOrder = .name
                }
            }
        #else
        NavigationStack {
            #if os(tvOS)
            listContent
            #else
            formContent
                .navigationTitle("mediaBrowser.viewOptions.title")
                #if os(iOS)
                .navigationBarTitleDisplayMode(.inline)
                #endif
                .toolbar {
                    sheetCloseToolbarItem { dismiss() }
                }
            #endif
        }
        #if os(iOS)
        .presentationDetents([.height(280)])
        .presentationDragIndicator(.visible)
        #endif
        .onAppear {
            // Reset sort order if current selection is not available for this source type
            if !availableSortOptions.contains(sortOrder) {
                sortOrder = .name
            }
        }
        #endif
    }

    @ViewBuilder
    private var sharedOptions: some View {
        Toggle("mediaBrowser.viewOptions.showOnlyPlayable", isOn: $showOnlyPlayable)
        PlatformMenuPicker(String(localized: "mediaBrowser.viewOptions.sortBy"), selection: $sortOrder) {
            ForEach(availableSortOptions) { order in
                Label(order.displayName, systemImage: order.systemImage)
                    .tag(order)
            }
        }

        Picker("mediaBrowser.viewOptions.order", selection: $sortAscending) {
            Label(String(localized: "mediaBrowser.viewOptions.ascending"), systemImage: "arrow.up")
                .tag(true)
            Label(String(localized: "mediaBrowser.viewOptions.descending"), systemImage: "arrow.down")
                .tag(false)
        }
        .pickerStyle(.segmented)
        .listRowBackground(Color.clear)
        .listRowInsets(EdgeInsets(top: 8, leading: 0, bottom: 8, trailing: 0))
    }

    #if os(tvOS)
    private var listContent: some View {
        List {
            Section {
                sharedOptions
            }
        }
        .scrollClipDisabled()
        .padding(.horizontal, 40)
        .padding(.vertical, 24)
    }
    #endif

    private var formContent: some View {
        Form {
            Section {
                sharedOptions
            }
        }
    }
}

// MARK: - Preview

#Preview("Local Folder") {
    @Previewable @State var sortOrder: MediaBrowserSortOrder = .name
    @Previewable @State var sortAscending = true
    @Previewable @State var showOnlyPlayable = false

    MediaBrowserViewOptionsSheet(
        sourceType: .localFolder,
        sortOrder: $sortOrder,
        sortAscending: $sortAscending,
        showOnlyPlayable: $showOnlyPlayable
    )
}

#Preview("WebDAV") {
    @Previewable @State var sortOrder: MediaBrowserSortOrder = .name
    @Previewable @State var sortAscending = true
    @Previewable @State var showOnlyPlayable = false

    MediaBrowserViewOptionsSheet(
        sourceType: .webdav,
        sortOrder: $sortOrder,
        sortAscending: $sortAscending,
        showOnlyPlayable: $showOnlyPlayable
    )
}
