//
//  ChatView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/16.
//

import SwiftUI

struct ChatView: View {
    @EnvironmentObject private var selectedTabModel: SelectedTabModel
    @ObservedObject var message = SharingData.message
    @State var isShowMember: Bool = false

    var body: some View {
        ZStack {
            if message.mode == true{
                LinearGradient(gradient: Gradient(colors: [.chatEmargencyColor1,  .chatEmargencyColor2]), startPoint: .top, endPoint: .bottom)
                    .ignoresSafeArea()
            }
            VStack(spacing: 0){
                ChatTitleView(isShowMember: $isShowMember)

                ZStack{
                    ScrollViewReader { proxy in
                        ScrollView(.vertical) {
                            VStack {
                                Spacer()
                                    .frame(height: 20)

                                ForEach(message.messages, id: \.messageId) { msg in
                                    if msg.from == Preferences.UserName {
                                        //自分のメッセージ
                                        MyChatContentView(message: msg)
                                            .padding(.bottom, 24)
                                    }else{
                                        //他人のメッセージ
                                        OtherChatContentView(message: msg)
                                            .padding(.bottom, 24)
                                    }
                                }
                            }
                            .onAppear {
                                if let id = message.messages.last?.messageId {
                                    print(debug: "ChatView:onAppear")
                                    proxy.scrollTo(id, anchor: .center)
                                }
                            }
                            .onChange(of: message.messages.count) { newValue in
                                if let id = message.messages.last?.messageId {
                                    withAnimation {
                                        print(debug: "ChatView:onChange")
                                        proxy.scrollTo(id, anchor: .center)
                                    }
                                }
                            }
                            .onReceive(message.$messages) { (value) in
                                withAnimation {
                                    guard !value.isEmpty else {return}
                                    if let id = message.messages.last?.messageId {
                                        print(debug: "ChatView:onReceive")
                                        proxy.scrollTo(id, anchor: .center)
                                    }
                                }
                            }
                        }
                    }
                    HStack {
                        Spacer()
                        VStack{
                            Button{
                                selectedTabModel.isShowChangeEmrMode.toggle()
                            }label: {
                                Image(systemName: "exclamationmark.triangle.fill")
                                    .foregroundColor(.white)
                                    .frame(width: 48, height: 48)
                                    .background(ColorSet.Notice.color)
                                    .cornerRadius(50)
                            }.padding(9)
                            Spacer()
                        }
                    }
                    if isShowMember{
                        ChatMemberView()
                    }
                }
                ChatInputView()
            }
        }
        .background(ColorSet.BackgroundPrimary.color)
    }
}

#Preview {
    ChatView()
}
