//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2024 Jellyfin & Jellyfin Contributors
//

import Defaults
import SwiftUI

// TODO: rename `LibraryDisplayTypeToggle`/Section
//       - change to 2 Menu's in a section with subtitle
//         like on `SelectUserView`?

extension PagingLibraryView {

    struct LibraryViewTypeToggle: View {

        @Binding
        private var listColumnCount: Int
        @Binding
        private var posterType: PosterDisplayType
        @Binding
        private var viewType: LibraryDisplayType

        init(
            posterType: Binding<PosterDisplayType>,
            viewType: Binding<LibraryDisplayType>,
            listColumnCount: Binding<Int>
        ) {
            self._listColumnCount = listColumnCount
            self._posterType = posterType
            self._viewType = viewType
        }

        var body: some View {
            Menu {

                Section("Poster") {
                    Button {
                        posterType = .landscape
                    } label: {
                        if posterType == .landscape {
                            Label("Landscape", systemImage: "checkmark")
                        } else {
                            Label("Landscape", systemImage: "rectangle")
                        }
                    }

                    Button {
                        posterType = .portrait
                    } label: {
                        if posterType == .portrait {
                            Label("Portrait", systemImage: "checkmark")
                        } else {
                            Label("Portrait", systemImage: "rectangle.portrait")
                        }
                    }
                }

                Section("Layout") {
                    Button {
                        viewType = .grid
                    } label: {
                        if viewType == .grid {
                            Label("Grid", systemImage: "checkmark")
                        } else {
                            Label("Grid", systemImage: "square.grid.2x2.fill")
                        }
                    }

                    Button {
                        viewType = .list
                    } label: {
                        if viewType == .list {
                            Label("List", systemImage: "checkmark")
                        } else {
                            Label("List", systemImage: "square.fill.text.grid.1x2")
                        }
                    }
                }

                if viewType == .list, UIDevice.isPad {
                    Stepper("Columns: \(listColumnCount)", value: $listColumnCount, in: 1 ... 3)
                }
            } label: {
                switch viewType {
                case .grid:
                    Label("Layout", systemImage: "square.grid.2x2.fill")
                case .list:
                    Label("Layout", systemImage: "square.fill.text.grid.1x2")
                }
            }
        }
    }
}
