//
//  OtherChatContentView.swift
//  Sailassist
//
//  Created by Mamoru Sugita on 2023/11/09.
//
import SwiftUI
import AVKit

struct OtherChatContentView: View {
    @EnvironmentObject private var selectedTabModel: SelectedTabModel
    @State private var safariUrl: URL? = nil
    @State private var isSafariPresented = false
    var message : ChatMessage
    var contentWidth: CGFloat
    var onMediaLoaded: (() -> Void)? = nil

    var body: some View {
        HStack {
            VStack(alignment: .leading, spacing: 4) {
                if let from = message.from {
                    Text(from)
                        .font(FontStyle.EmphasisText.font)
                        .foregroundColor(ColorSet.Body.color)
                }
                VStack(alignment: .leading, spacing: 10) {
                    Group {
                        HStack(alignment: .top, spacing: 6) {
                            if let msg = message.message {
                                if msg.contains(".jpg") || msg.contains(".png") {
                                    ChatUrlImageView(imageUrl: msg, onLoad: onMediaLoaded)
                                        .onTapGesture {
                                            isSafariPresented = true
                                        }
                                } else {
                                    Text(msg)
                                        .font(FontStyle.DefaultText.font)
                                        .foregroundColor(ColorSet.BodyChat.color)
                                        .padding(15)
                                        .background(ColorSet.ChatBaloon.color)
                                        .border(Color.red.gradient.opacity(0.8), width: (message.mode == ChatMode.warningProgress.rawValue ? 2 : 0))
                                        .clipShape(
                                            .rect(
                                                topLeadingRadius: 10,
                                                bottomLeadingRadius: 0,
                                                bottomTrailingRadius: 10,
                                                topTrailingRadius: 10
                                            ))
                                        .frame(maxWidth: contentWidth * 0.8, alignment: .leading)
                                        .multilineTextAlignment(.leading)
                                        .onAppear {
                                            onMediaLoaded?()
                                        }
                                }
                            }
                            if message.Latitude != nil && message.Longitude != nil {
                                Image("map_pin")
                                    .resizable()
                                    .frame(width: 20, height: 20)
                            }
                        }
                    }
                    .onAppear {
                        if message.type >= 1 {
                            var urlString: String = HttpRequestType.RawImage.rawValue
                            urlString = urlString.replacingOccurrences(of: "XXXXX", with: String(message.shipId))
                            urlString = urlString.replacingOccurrences(of: "YYYYY", with: String(message.messageId))

                            if let url = URL(string: urlString) {
                                safariUrl = url
                            }
                        }
                    }
                    .onChange(of: selectedTabModel.activeTab) { newValue in
                        if newValue != Tab.chat {
                            isSafariPresented = false
                        }
                    }

                    HStack(alignment: .top) {
                        Text(DateTextLib.ISO86012FormatText(message.time, format: "yyyy/MM/dd HH:mm", errFormat: ""))
                            .font(FontStyle.DateText.font)
                            .foregroundColor(ColorSet.ChatDate.color)
                    }
                }
            }
            .padding(.leading, 20)
            Spacer()
        }
        .fullScreenCover(isPresented: $isSafariPresented) {
            if let url = safariUrl {
                SafariView(url: url) {
                    isSafariPresented = false
                }
            } else {
                Text("URLが無効です")
            }
        }
    }
}

