import SwiftUI

// MARK: - Email Input Component
struct EmailInputView: View {
    @Binding var emailAddress: String
    @Binding var emailSubject: String
    @Binding var emailBody: String
    @Binding var emailCc: String
    @Binding var emailBcc: String
    @FocusState var focusedEmailField: EmailField?
    
    // Email field enum
    enum EmailField: Hashable {
        case address, subject, body, cc, bcc
    }
    
    var body: some View {
        VStack(spacing: 16) {
            // Email address (required)
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("email_address".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Text("*")
                        .foregroundColor(.red)
                    Spacer()
                }
                
                TextField("contact_email_placeholder".localized, text: $emailAddress)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .keyboardType(.emailAddress)
                    .autocapitalization(.none)
                    .focused($focusedEmailField, equals: .address)
            }
            
            // Subject (required)
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("email_subject".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Text("*")
                        .foregroundColor(.red)
                    Spacer()
                }
                
                TextField("email_subject_placeholder".localized, text: $emailSubject)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .focused($focusedEmailField, equals: .subject)
            }
            
            // Body (required)
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("email_body".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Text("*")
                        .foregroundColor(.red)
                    Spacer()
                }
                
                ZStack {
                    TextEditor(text: $emailBody)
                        .frame(minHeight: 120)
                        .padding(8)
                        .background(Color(.systemBackground))
                        .cornerRadius(8)
                        .overlay(
                            RoundedRectangle(cornerRadius: 8)
                                .stroke(focusedEmailField == .body ? Color.blue : Color(.systemGray4), lineWidth: 1)
                        )
                        .focused($focusedEmailField, equals: .body)
                        .onChange(of: emailBody) { newValue in
                            // Limit maximum characters to 1200
                            if newValue.count > 1200 {
                                emailBody = String(newValue.prefix(1200))
                            }
                        }
                    
                    // Placeholder text
                    if emailBody.isEmpty && focusedEmailField != .body {
                        VStack {
                            HStack {
                                Text("email_body_placeholder".localized)
                                    .foregroundColor(.secondary)
                                    .font(.body)
                                Spacer()
                            }
                            Spacer()
                        }
                        .padding(16)
                        .allowsHitTesting(false)
                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
                    }
                }
                
                // Character count
                HStack {
                    Spacer()
                    Text("\(emailBody.count)/1200")
                        .font(.caption)
                        .foregroundColor(emailBody.count >= 1200 ? .orange : .secondary)
                }
            }
            
            // CC address (optional)
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("cc_address".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Spacer()
                }
                
                TextField("cc_email_placeholder".localized, text: $emailCc)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .keyboardType(.emailAddress)
                    .autocapitalization(.none)
                    .focused($focusedEmailField, equals: .cc)
            }
            
            // BCC address (optional)
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("bcc_address".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Spacer()
                }
                
                TextField("bcc_email_placeholder".localized, text: $emailBcc)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .keyboardType(.emailAddress)
                    .autocapitalization(.none)
                    .focused($focusedEmailField, equals: .bcc)
            }
        }
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Spacer()
                Button("complete".localized) {
                    focusedEmailField = nil
                }
                .foregroundColor(.blue)
                .font(.system(size: 16, weight: .medium))
            }
        }
    }
}

#Preview {
    EmailInputView(
        emailAddress: .constant(""),
        emailSubject: .constant(""),
        emailBody: .constant(""),
        emailCc: .constant(""),
        emailBcc: .constant("")
    )
} 