import SDWebImageSwiftUI
import SwiftUI

public struct PopupInput: View {
    // MARK: Lifecycle

    public init(isPresented: Binding<Bool>, title: String = "", description: String = "", numberOfButtons: Int = 1, actionButtonTitle: String = "", closeButtonTitle: String = "", onPressAction: @escaping () -> Void = {}, onPressCloseButton: @escaping () -> Void = {}, onClose: @escaping () -> Void = {}, inputPlaceholder: String = "", inputValue: Binding<String> = Binding.constant(""), onTextChange: ((String) -> Void)? = nil) {
        self._isPresented = isPresented
        self._inputValue = inputValue
        self.title = title
        self.description = description
        self.onPressAction = onPressAction
        self.onPressCloseButton = onPressCloseButton
        self.onClose = onClose
        self.inputPlaceholder = inputPlaceholder
        self.onTextChange = onTextChange
        self.numberOfButtons = numberOfButtons
        self.closeButtonTitle = closeButtonTitle
        self.actionButtonTitle = actionButtonTitle
    }

    // MARK: Public

    public var body: some View {
        VStack(spacing: 12) {
            ZStack(alignment: .topTrailing) {
                SwiftUI.Button(action: onPressClose) {
                    Image(systemName: "xmark.circle.fill")
                        .resizable()
                        .frame(width: 16, height: 16)
                }.foregroundColor(Colors.black20)
                    .background(Colors.black01.cornerRadius(8))
                    .offset(x: 6, y: -6)
                    .zIndex(1)

                VStack {
                    VStack(alignment: .leading) {
                        Text(title)
                            .foregroundColor(.black)
                            .font(.headerText1)
                            .padding(.top, 12)
                            .padding(.bottom, 24)

                        Text(description)
                            .foregroundColor(.black)
                            .font(.paragraph)
                            .opacity(0.6)
                            .multilineTextAlignment(.leading)
                            .padding(.bottom, 20)
                    }.padding(.horizontal, 24)

                    Input(text: $inputValue, placeholder: inputPlaceholder, onChangeText: onTextChange)
                        .padding(.horizontal, 24)

                    if numberOfButtons == 1 {
                        Button(title: actionButtonTitle, action: onPressAction)
                            .padding(.horizontal, 24)
                            .padding(.bottom, 24)
                    } else {
                        HStack {
                            Button(title: closeButtonTitle, action: onPressCloseButton, type: .secondary)
                            Button(title: actionButtonTitle, action: onPressAction)
                        }.padding(.horizontal, 24)
                            .padding(.bottom, 24)
                    }

                }.clipped()
            }
        }
        .background(Color.white.cornerRadius(8))
        .shadow(color: .black.opacity(0.08), radius: 2, x: 0, y: 0)
        .shadow(color: .black.opacity(0.16), radius: 24, x: 0, y: 0)
        .padding(.horizontal, 40)
    }

    // MARK: Internal

    @Binding var isPresented: Bool
    @Binding var inputValue: String
    var title: String
    var description: String
    var onPressAction: () -> Void
    var onPressCloseButton: () -> Void
    var onClose: () -> Void
    var inputPlaceholder: String
    var onTextChange: ((String) -> Void)?
    var numberOfButtons: Int
    var actionButtonTitle: String
    var closeButtonTitle: String


    func onPressClose(){
        isPresented = false
        onClose()
    }
}
