import SwiftUI

// MARK: - Social Platform Input Component
struct SocialInputView: View {
    @Binding var username: String
    @Binding var message: String
    let platform: SocialPlatform
    @FocusState var focusedField: SocialField?
    
    // Social platform enum
    enum SocialPlatform: String, CaseIterable {
        case instagram = "Instagram"
        case facebook = "Facebook"
        case twitter = "X"
        case tiktok = "Tiktok"
        case snapchat = "Snapchat"
        case whatsapp = "Whatsapp"
        case viber = "Viber"
        case spotify = "Spotify"
        
        var displayName: String {
            switch self {
            case .instagram: return "Instagram"
            case .facebook: return "Facebook"
            case .twitter: return "X"
            case .tiktok: return "TikTok"
            case .snapchat: return "Snapchat"
            case .whatsapp: return "WhatsApp"
            case .viber: return "Viber"
            case .spotify: return "Spotify"
            }
        }
        
        var icon: String {
            switch self {
            case .instagram: return "camera"
            case .facebook: return "person.2"
            case .twitter: return "bird"
            case .tiktok: return "music.note"
            case .snapchat: return "ghost"
            case .whatsapp: return "message"
            case .viber: return "phone"
            case .spotify: return "music.note.list"
            }
        }
        
        var placeholder: String {
            switch self {
            case .instagram: return "instagram_placeholder".localized
            case .facebook: return "facebook_placeholder".localized
            case .twitter: return "twitter_placeholder".localized
            case .tiktok: return "tiktok_placeholder".localized
            case .snapchat: return "snapchat_placeholder".localized
            case .whatsapp: return "whatsapp_placeholder".localized
            case .viber: return "viber_placeholder".localized
            case .spotify: return "spotify_placeholder".localized
            }
        }
        
        var hint: String {
            switch self {
            case .instagram: return "instagram_hint".localized
            case .facebook: return "facebook_hint".localized
            case .twitter: return "twitter_hint".localized
            case .tiktok: return "tiktok_hint".localized
            case .snapchat: return "snapchat_hint".localized
            case .whatsapp: return "whatsapp_hint".localized
            case .viber: return "viber_hint".localized
            case .spotify: return "spotify_hint".localized
            }
        }
    }
    
    // Social field enum
    enum SocialField: Hashable {
        case username, message
    }
    
    var body: some View {
        VStack(spacing: 16) {
            // Platform information
            HStack {
                Image(systemName: platform.icon)
                    .font(.title2)
                    .foregroundColor(.blue)
                
                VStack(alignment: .leading, spacing: 2) {
                    Text(platform.displayName)
                        .font(.headline)
                        .foregroundColor(.primary)
                    
                    Text(platform.hint)
                        .font(.caption)
                        .foregroundColor(.secondary)
                }
                
                Spacer()
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
            .background(
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color.blue.opacity(0.1))
            )
            
            // Input field section
            if platform == .spotify {
                // Spotify specific: Artist and Song input fields
                VStack(alignment: .leading, spacing: 12) {
                                         // Artist input field
                     VStack(alignment: .leading, spacing: 8) {
                         HStack {
                             Text("artist".localized)
                                 .font(.subheadline)
                                 .foregroundColor(.primary)
                             Text("*")
                                 .foregroundColor(.red)
                             Spacer()
                         }
                         
                         TextField("enter_artist_name".localized, text: $username)
                             .textFieldStyle(RoundedBorderTextFieldStyle())
                             .autocapitalization(.none)
                             .focused($focusedField, equals: .username)
                     }
                     
                     // Song input field
                     VStack(alignment: .leading, spacing: 8) {
                         HStack {
                             Text("song_name".localized)
                                 .font(.subheadline)
                                 .foregroundColor(.primary)
                             Text("*")
                                 .foregroundColor(.red)
                             Spacer()
                         }
                         
                         TextField("enter_song_name".localized, text: $message)
                             .textFieldStyle(RoundedBorderTextFieldStyle())
                             .autocapitalization(.none)
                            .focused($focusedField, equals: .message)
                    }
                }
            } else {
                // Single input field for other platforms
                VStack(alignment: .leading, spacing: 8) {
                    HStack {
                        // Display different hints based on platform
                        Text(getInputLabel())
                            .font(.subheadline)
                            .foregroundColor(.primary)
                        Text("*")
                            .foregroundColor(.red)
                        Spacer()
                    }
                    
                    TextField(platform.placeholder, text: $username)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .autocapitalization(.none)
                        .focused($focusedField, equals: .username)
                }
            }
            

            
            // Format instructions
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Image(systemName: "info.circle")
                        .font(.caption)
                        .foregroundColor(.blue)
                    
                                         Text("format_instructions".localized)
                         .font(.caption)
                         .foregroundColor(.primary)
                    
                    Spacer()
                }
                
                Text(getFormatHint())
                    .font(.caption)
                    .foregroundColor(.secondary)
                    .lineLimit(nil)
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
            .background(
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color.blue.opacity(0.1))
            )
        }
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Spacer()
                                 Button("done".localized) {
                     focusedField = nil
                 }
                .foregroundColor(.blue)
                .font(.system(size: 16, weight: .medium))
            }
        }
    }
    
         // MARK: - Get input label based on platform
     private func getInputLabel() -> String {
         switch platform {
         case .instagram:
             return String(format:"instagram_username".localized, "")
         case .facebook:
             return "user_id_or_link".localized
         case .twitter:
             return "x_username".localized
         case .tiktok:
             return String(format:"tiktok_username".localized, "")
         case .snapchat:
             return String(format: "snapchat_username".localized, "")
         case .whatsapp:
             return String(format: "whatsapp_phone_number".localized, "")
         case .viber:
             return String(format: "viber_phone_number".localized, "")
         case .spotify:
             return "song_link_or_id".localized
         }
     }
    
         private func getFormatHint() -> String {
         return "social_format_hint".localized
     }
}

#Preview {
    SocialInputView(
        username: .constant(""),
        message: .constant(""),
        platform: .tiktok
    )
} 
