//
// 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 SwiftUI

extension SelectUserView {

    struct ServerSelectionMenu: View {

        @Environment(\.colorScheme)
        private var colorScheme

        @EnvironmentObject
        private var router: SelectUserCoordinator.Router

        @Binding
        private var serverSelection: SelectUserServerSelection

        @ObservedObject
        private var viewModel: SelectUserViewModel

        private var selectedServer: ServerState? {
            if case let SelectUserServerSelection.server(id: id) = serverSelection,
               let server = viewModel.servers.keys.first(where: { server in server.id == id })
            {
                return server
            }

            return nil
        }

        init(
            selection: Binding<SelectUserServerSelection>,
            viewModel: SelectUserViewModel
        ) {
            self._serverSelection = selection
            self.viewModel = viewModel
        }

        var body: some View {
            Menu {
                Section {
                    Button("Add Server", systemImage: "plus") {
                        router.route(to: \.connectToServer)
                    }

                    if let selectedServer {
                        Button("Edit Server", systemImage: "server.rack") {
                            router.route(to: \.editServer, selectedServer)
                        }
                    }
                }

                Picker("Servers", selection: _serverSelection) {

                    if viewModel.servers.keys.count > 1 {
                        Label("All Servers", systemImage: "person.2.fill")
                            .tag(SelectUserServerSelection.all)
                    }

                    ForEach(viewModel.servers.keys.reversed()) { server in
                        Button {
                            Text(server.name)
                            Text(server.currentURL.absoluteString)
                        }
                        .tag(SelectUserServerSelection.server(id: server.id))
                    }
                }
            } label: {
                ZStack {

                    if colorScheme == .light {
                        Color.secondarySystemFill
                    } else {
                        Color.tertiarySystemBackground
                    }

                    Group {
                        switch serverSelection {
                        case .all:
                            Label("All Servers", systemImage: "person.2.fill")
                        case let .server(id):
                            if let server = viewModel.servers.keys.first(where: { $0.id == id }) {
                                Label(server.name, systemImage: "server.rack")
                            }
                        }
                    }
                    .font(.body.weight(.semibold))
                    .foregroundStyle(Color.primary)
                }
                .frame(height: 50)
                .frame(maxWidth: 400)
                .clipShape(RoundedRectangle(cornerRadius: 10))
            }
            .buttonStyle(.plain)
        }
    }
}
