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

import SwiftUI

struct TextMessageWriteView: View {
    
    @StateObject var viewModel = TextMessageViewModel()
    @StateObject var appState = AppState.shared
    
    var replySenderId: Int? = nil
    var replySenderNickname: String? = nil
    
    @State var isShowSearchUser = false
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                HStack(spacing: 0) {
                    Text("취소")
                        .font(.custom(Font.medium.rawValue, size: 16.7))
                        .foregroundColor(Color(hex: "3bb9f1").opacity(0))
                    
                    Spacer()
                    
                    Text("새로운 메시지")
                        .font(.custom(Font.bold.rawValue, size: 18.3))
                        .foregroundColor(Color(hex: "eeeeee"))
                    
                    Spacer()
                    
                    Text("취소")
                        .font(.custom(Font.medium.rawValue, size: 16.7))
                        .foregroundColor(Color(hex: "3bb9f1"))
                        .onTapGesture {
                            AppState.shared.back()
                        }
                }
                .padding(.horizontal, 13.3)
                .frame(height: 50)
                
                VStack(spacing: 0) {
                    Rectangle()
                        .frame(height: 1)
                        .foregroundColor(Color(hex: "909090").opacity(0))
                    
                    Spacer()
                    
                    HStack(spacing: 13.3) {
                        Text("받는 사람")
                            .font(.custom(Font.medium.rawValue, size: 16.7))
                            .foregroundColor(Color(hex: "777777"))
                        
                        Text(viewModel.recipientNickname)
                            .font(.custom(Font.medium.rawValue, size: 16.7))
                            .foregroundColor(Color(hex: "eeeeee"))
                        
                        Spacer()
                        
                        if replySenderId == nil && replySenderNickname == nil {
                            Image("btn_plus_round")
                                .resizable()
                                .frame(width: 27, height: 27)
                                .onTapGesture {
                                    isShowSearchUser = true
                                }
                        }
                    }
                    .padding(.horizontal, 13.3)
                    
                    Spacer()
                    
                    Rectangle()
                        .frame(height: 1)
                        .foregroundColor(Color(hex: "909090").opacity(0.5))
                }
                .frame(height: 50)
                
                TextViewWrapper(
                    text: $viewModel.message,
                    placeholder: viewModel.placeholder,
                    textColorHex: "eeeeee",
                    backgroundColorHex: "333333"
                )
                .frame(width: screenSize().width - 26.7, height: 150)
                .cornerRadius(6.7)
                .overlay(
                    RoundedRectangle(cornerRadius: 6.7)
                        .stroke(Color(hex: "3bb9f1"), lineWidth: 1.3)
                )
                .padding(.top, 13.3)
                
                Spacer()
                
                Text(viewModel.sendText)
                    .font(.custom(Font.bold.rawValue, size: 14.7))
                    .foregroundColor(Color(hex: "eeeeee"))
                    .frame(width: screenSize().width - 26.7, height: 48.7)
                    .background(Color(hex: "3bb9f1"))
                    .cornerRadius(6.7)
                    .padding(.bottom, 13.3)
                    .onTapGesture {
                        hideKeyboard()
                        viewModel.write()
                    }
            }
            
            if isShowSearchUser {
                SelectRecipientView(isShowing: $isShowSearchUser) {
                    viewModel.recipientId = $0.id
                    viewModel.recipientNickname = $0.nickname
                }
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .padding(.horizontal, 6.7)
                        .frame(width: geo.size.width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color(hex: "3bb9f1"))
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .fixedSize(horizontal: false, vertical: true)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
        .onAppear {
            UITextView.appearance().backgroundColor = .clear
            
            if let replySenderId = replySenderId, let replySenderNickname = replySenderNickname {
                viewModel.recipientId = replySenderId
                viewModel.recipientNickname = replySenderNickname
            }
        }
    }
}

struct TextMessageWriteView_Previews: PreviewProvider {
    static var previews: some View {
        TextMessageWriteView()
    }
}
