//
//  InputSearch.swift
//  Pods
//
//  Created by dung.pham2 on 3/12/24.
//
import SwiftUI

// MARK: - RenderRightIconSearch

struct RenderRightIconSearch: View {
    let loading: Bool
    let icon: String
    let color: Color
    let onClick: () -> Void
    
    var body: some View {
        HStack(spacing: 0) {
            if loading {
                ActivityIndicator(isAnimating: .constant(true), style: .medium)
                    .frame(width: 16, height: 16)
                    .padding(.horizontal, Spacing.S)
            }
            if !icon.isEmpty {
                Divider()
                    .frame(width: 1)
                    .background(Colors.primary)
                    .padding(.leading, Spacing.S)
                
                Image(systemName: icon)
                    .foregroundColor(color)
                    .frame(width: 24, height: 24)
                    .padding(.leading, Spacing.S)
                    .onTapGesture(perform: onClick)
            }
        }
    }
}

public struct InputSearchProps {
    @Binding var text: String
    var buttonText: String = ""
    var showButtonText: Bool = false
    var showBorder: Bool = true
    var placeholder: String = ""
    var onChangeText: (String) -> Void = { _ in }
    var onPressButtonText: () -> Void = {}
    var error: String = ""
    var disabled: Bool = false
    var icon: String = ""
    var iconColor: Color = Colors.text
    var onRightIconPressed: () -> Void = {}
    var onFocus: () -> Void = {}
    var onBlur: () -> Void = {}
    var loading: Bool = false
    var fontWeight: Font.Weight = .regular
    var keyboardType: UIKeyboardType = .default
    
    public init(text: Binding<String>,
                buttonText: String = "",
                showButtonText: Bool = false,
                showBorder: Bool = true,
                placeholder: String = "",
                onChangeText: @escaping (String) -> Void = { _ in },
                onPressButtonText: @escaping () -> Void = {},
                error: String = "",
                disabled: Bool = false,
                icon: String = "",
                iconColor: Color = Colors.text,
                onRightIconPressed: @escaping () -> Void = {},
                onFocus: @escaping () -> Void = {},
                onBlur: @escaping () -> Void = {},
                loading: Bool = false,
                fontWeight: Font.Weight = .regular,
                keyboardType: UIKeyboardType = .default) {
        self._text = text
        self.buttonText = buttonText
        self.showButtonText = showButtonText
        self.showBorder = showBorder
        self.placeholder = placeholder
        self.onChangeText = onChangeText
        self.onPressButtonText = onPressButtonText
        self.error = error
        self.disabled = disabled
        self.icon = icon
        self.iconColor = iconColor
        self.onRightIconPressed = onRightIconPressed
        self.onFocus = onFocus
        self.onBlur = onBlur
        self.loading = loading
        self.fontWeight = fontWeight
        self.keyboardType = keyboardType
        }
}

// MARK: - InputSearch

public struct InputSearch: View {
    @Binding var text: String
    var buttonText: String = ""
    var showButtonText: Bool = false
    var showBorder: Bool = true
    var placeholder: String = ""
    var onChangeText: (String) -> Void = { _ in }
    var onPressButtonText: () -> Void = {}
    var error: String = ""
    var disabled: Bool = false
    var icon: String = ""
    var iconColor: Color = Colors.text
    var onRightIconPressed: () -> Void = {}
    var onFocus: () -> Void = {}
    var onBlur: () -> Void = {}
    var loading: Bool = false
    var fontWeight: Font.Weight = .regular
    var keyboardType: UIKeyboardType = .default
    
    @State private var isFocused = false
    @State private var isBlurred = false
    
    public init(text: Binding<String>,
                buttonText: String = "",
                showButtonText: Bool = false,
                showBorder: Bool = true,
                placeholder: String = "",
                onChangeText: @escaping (String) -> Void = { _ in },
                onPressButtonText: @escaping () -> Void = {},
                error: String = "",
                disabled: Bool = false,
                icon: String = "",
                iconColor: Color = Colors.text,
                onRightIconPressed: @escaping () -> Void = {},
                onFocus: @escaping () -> Void = {},
                onBlur: @escaping () -> Void = {},
                loading: Bool = false,
                fontWeight: Font.Weight = .regular,
                keyboardType: UIKeyboardType = .default) {
        self._text = text
        self.buttonText = buttonText
        self.showButtonText = showButtonText
        self.showBorder = showBorder
        self.placeholder = placeholder
        self.onChangeText = onChangeText
        self.onPressButtonText = onPressButtonText
        self.error = error
        self.disabled = disabled
        self.icon = icon
        self.iconColor = iconColor
        self.onRightIconPressed = onRightIconPressed
        self.onFocus = onFocus
        self.onBlur = onBlur
        self.loading = loading
        self.fontWeight = fontWeight
        self.keyboardType = keyboardType
        }

    
    public var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            HStack(spacing: 0) {
                HStack {
                    Image(systemName: "magnifyingglass")
                        .foregroundColor(Colors.black12)
                        .frame(width: 24, height: 24)
                        .padding(.trailing, Spacing.XS)
                    
                    ZStack(alignment: .leading) {
                        if text.isEmpty {
                            Text(placeholder)
                                .foregroundColor(disabled ? Colors.black08 : Colors.black12)
                                .font(.system(size: 16, weight: fontWeight))
                        }
                        TextField("", text: $text, onEditingChanged: { editing in
                            isFocused = editing
                            if editing {
                                onFocus()
                            } else if isBlurred {
                                onBlur()
                            }
                            if editing && !isBlurred {
                                isBlurred = true
                            }
                        }, onCommit: {})
                        .disabled(disabled)
                        .foregroundColor(disabled ? Colors.black08 : Colors.black17)
                        .font(.system(size: 15, weight: fontWeight))
                        .keyboardType(keyboardType)
                    }
                    
                    if isFocused && !text.isEmpty {
                        SwiftButton(action: { text = "" }) {
                            Image(systemName: "xmark.circle.fill")
                                .foregroundColor(Colors.black12)
                                .frame(width: 16, height: 16)
                        }
                        .padding(.leading, Spacing.S)
                    }
                    
                    RenderRightIconSearch(loading: loading, icon: icon, color: disabled ? Colors.black08 : iconColor, onClick: onRightIconPressed)
                }
                .padding(.horizontal, Spacing.M)
                .padding(.vertical, Spacing.S)
                .background(Colors.black01)
                .cornerRadius(Radius.XL)
                .overlay(
                    RoundedRectangle(cornerRadius: Radius.XL)
                        .stroke(getBorderColor(isFocused: isFocused, error: error, disabled: disabled), lineWidth: showBorder ? 1 : 0)
                )
                .frame(maxWidth: showButtonText ? .infinity : nil, alignment: .leading)
                
                if showButtonText {
                    SwiftButton(action: onPressButtonText) {
                        Text(buttonText)
                            .foregroundColor(Colors.black17)
                            .font(.system(size: 14, weight: .medium))
                    }
                    .padding(.leading, Spacing.L)
                }
            }
        }
    }
}

// MARK: - Helper Functions

func getBorderColor(isFocused: Bool, error: String, disabled: Bool) -> Color {
    if disabled {
        return Colors.black08
    } else if !error.isEmpty {
        return Colors.red03
    } else if isFocused {
        return Colors.primary
    } else {
        return Colors.black04
    }
}



