import SwiftUI
import Models

struct ComposeView: View {
	@Bindable var viewModel: ComposeViewModel
	@Environment(\.dismiss) private var dismiss

	@State private var showBcc = false
	@State private var showDiscardConfirmation = false

	var body: some View {
		#if os(macOS)
		content
			.frame(minWidth: 500, minHeight: 400)
		#else
		NavigationStack {
			content
		}
		#endif
	}

	private var content: some View {
		Form {
			Section {
				TextField("To", text: $viewModel.to)
					#if os(iOS)
					.textContentType(.emailAddress)
					.keyboardType(.emailAddress)
					.autocapitalization(.none)
					#endif

				TextField("CC", text: $viewModel.cc)
					#if os(iOS)
					.textContentType(.emailAddress)
					.keyboardType(.emailAddress)
					.autocapitalization(.none)
					#endif

				if showBcc {
					TextField("BCC", text: $viewModel.bcc)
						#if os(iOS)
						.textContentType(.emailAddress)
						.keyboardType(.emailAddress)
						.autocapitalization(.none)
						#endif
				}

				TextField("Subject", text: $viewModel.subject)
			}

			Section {
				TextEditor(text: $viewModel.bodyText)
					.frame(minHeight: 200)
			}

			if let error = viewModel.errorMessage {
				Section {
					Text(error)
						.foregroundStyle(.red)
				}
			}
		}
		.formStyle(.grouped)
		.navigationTitle(navigationTitle)
		#if os(macOS)
		.navigationSubtitle(viewModel.subject)
		#endif
		.toolbar {
			ToolbarItem(placement: .cancellationAction) {
				Button("Discard") {
					if viewModel.isDirty {
						showDiscardConfirmation = true
					} else {
						viewModel.deleteDraft()
						dismiss()
					}
				}
			}

			ToolbarItem(placement: .automatic) {
				Button {
					showBcc.toggle()
				} label: {
					Label("BCC", systemImage: showBcc ? "eye.slash" : "eye")
				}
				.help("Toggle BCC field")
			}

			ToolbarItem(placement: .confirmationAction) {
				Button {
					Task {
						try? await viewModel.send()
						if viewModel.errorMessage == nil {
							dismiss()
						}
					}
				} label: {
					if viewModel.isSending {
						ProgressView()
							.controlSize(.small)
					} else {
						Label("Send", systemImage: "paperplane")
					}
				}
				.keyboardShortcut(.return, modifiers: .command)
				.disabled(viewModel.to.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isSending)
			}
		}
		.confirmationDialog("Discard Draft?", isPresented: $showDiscardConfirmation) {
			Button("Discard", role: .destructive) {
				viewModel.deleteDraft()
				dismiss()
			}
			Button("Save Draft") {
				viewModel.saveDraft()
				dismiss()
			}
			Button("Cancel", role: .cancel) {}
		} message: {
			Text("You have unsaved changes.")
		}
		.onDisappear {
			if viewModel.isDirty && !viewModel.isSending {
				viewModel.saveDraft()
			}
		}
	}

	private var navigationTitle: String {
		switch viewModel.mode {
		case .new: return "New Message"
		case .reply: return "Reply"
		case .replyAll: return "Reply All"
		case .forward: return "Forward"
		case .draft: return "Draft"
		}
	}
}
