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

import SwiftUI

struct TextMessageView: View {
    
    @StateObject var viewModel = TextMessageViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            ZStack(alignment: .bottomTrailing) {
                VStack(spacing: 13.3) {
                    
                    MessageFilterTabView(currentFilterTab: $viewModel.currentFilter)
                        .padding(.top, 20)
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        if viewModel.items.count > 0 {
                            LazyVStack(spacing: 26.7) {
                                ForEach(0..<viewModel.items.count, id: \.self) { index in
                                    let item = viewModel.items[index]
                                    TextMessageItemView(item: item)
                                        .frame(width: screenSize().width - 26.7)
                                        .contentShape(Rectangle())
                                        .onTapGesture {
                                            AppState.shared.setAppStep(
                                                step: .textMessageDetail(
                                                    messageItem: item,
                                                    messageBox: viewModel.currentFilter,
                                                    refresh: {
                                                        viewModel.page = 1
                                                        viewModel.isLast = false
                                                        switch viewModel.currentFilter {
                                                        case .receive:
                                                            viewModel.getReceivedTextMessage()
                                                        case .sent:
                                                            viewModel.getSentTextMessage()
                                                        case .keep:
                                                            viewModel.getKeepTextMessage()
                                                        }
                                                    }
                                                )
                                            )
                                        }
                                        .onAppear {
                                            if index == viewModel.items.count - 1 && !viewModel.isLast {
                                                switch viewModel.currentFilter {
                                                case .receive:
                                                    viewModel.getReceivedTextMessage()
                                                case .sent:
                                                    viewModel.getSentTextMessage()
                                                case .keep:
                                                    viewModel.getKeepTextMessage()
                                                }
                                            }
                                        }
                                }
                            }
                            .padding(.top, 26.7)
                        } else {
                            VStack(spacing: 6.7) {
                                Image("ic_no_item")
                                    .resizable()
                                    .frame(width: 60, height: 60)
                                
                                Text("메시지가 없습니다.\n친구들과 소통해보세요!")
                                    .multilineTextAlignment(.center)
                                    .font(.custom(Font.medium.rawValue, size: 10.7))
                                    .foregroundColor(Color(hex: "bbbbbb"))
                            }
                            .frame(width: screenSize().width - 26.7, height: screenSize().height / 2)
                            .background(Color(hex: "13181b"))
                            .cornerRadius(4.7)
                        }
                    }
                }
                
                Image("ic_make_message")
                    .resizable()
                    .padding(13.3)
                    .frame(width: 53.3, height: 53.3)
                    .background(Color(hex: "3bb9f1"))
                    .cornerRadius(26.7)
                    .padding(.bottom, 33.3)
                    .padding(.trailing, 6.7)
                    .onTapGesture {
                        AppState.shared.setAppStep(step: .writeTextMessage(userId: nil, nickname: nil))
                    }
            }
            .onAppear {
                viewModel.getReceivedTextMessage()
            }
        }
    }
}

struct TextMessageView_Previews: PreviewProvider {
    static var previews: some View {
        TextMessageView()
    }
}
