//
//  TextViewWrapper.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import SwiftUI

struct TextViewWrapper: UIViewRepresentable {
    
    @Binding var text: String
    
    var placeholder: String
    var textColorHex: String
    var backgroundColorHex: String
    
    var notice: String? = nil
    
    func makeUIView(context: Context) -> UITextView {
        let view = UITextView()
        
        if let notice = notice {
            view.text = notice.trimmingCharacters(in: .whitespacesAndNewlines) != "" ? notice : placeholder
            view.textColor = notice.trimmingCharacters(in: .whitespacesAndNewlines) != "" ? UIColor(hex: textColorHex) : .placeholderText
        } else {
            view.text = text.trimmingCharacters(in: .whitespacesAndNewlines) != "" ? text : placeholder
            view.textColor = text.trimmingCharacters(in: .whitespacesAndNewlines) != "" ? UIColor(hex: textColorHex) : .placeholderText
        }
        
        view.font = UIFont(name: Font.medium.rawValue, size: 13.3)
        view.backgroundColor = backgroundColorHex.isEmpty ? .clear : UIColor(hex: backgroundColorHex)
        view.layer.cornerRadius = 6.7
        view.textContainerInset = UIEdgeInsets(top: 20, left: 13.3, bottom: 20, right: 13.3)
        
        view.delegate = context.coordinator
        
        return view
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        if text.trimmingCharacters(in: .whitespacesAndNewlines) != "" {
            uiView.text = text
            uiView.textColor = UIColor(hex: textColorHex)
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator($text, placeholder: placeholder, textColorHex: textColorHex)
    }
    
    class Coordinator: NSObject {
        var text: Binding<String>
        var placeholder: String
        var textColorHex: String
        
        init(_ text: Binding<String>, placeholder: String, textColorHex: String) {
            self.text = text
            self.placeholder = placeholder
            self.textColorHex = textColorHex
        }
    }
}

extension TextViewWrapper.Coordinator: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == .placeholderText {
            DispatchQueue.main.async { [unowned self] in
                self.text.wrappedValue = ""
                textView.text = ""
                textView.textColor = UIColor(hex: textColorHex)
            }
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
            DispatchQueue.main.async { [unowned self] in
                self.text.wrappedValue = ""
                textView.text = self.placeholder
                textView.textColor = .placeholderText
            }
        }
    }
    
    func textViewDidChange(_ textView: UITextView) {
        self.text.wrappedValue = textView.text
    }
}

struct TextViewWrapper_Previews: PreviewProvider {
    static var previews: some View {
        TextViewWrapper(
            text: .constant(""),
            placeholder: "입력하세요",
            textColorHex: "777777",
            backgroundColorHex: "222222"
        )
    }
}