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

struct OtherChatContentView: View {
    @State private var isVideoPlayerPresented = false
    var message : ChatMessage
    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 {
                        if let msg = message.message {
                            if msg.contains(".jpg") || msg.contains(".png") {
                                ChatUrlImageView(imageUrl: msg, onLoad: onMediaLoaded)
//                                    .onTapGesture {
//                                        if message.type == 3,
//                                           let videoUrl = message.videoUrl,
//                                           let url = URL(string: videoUrl) {
//                                            isVideoPlayerPresented = 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: 10,
                                            bottomTrailingRadius: 0,
                                            topTrailingRadius: 10
                                        ))
                            }
                        }
                    }

                    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: $isVideoPlayerPresented) {
//            if let videoUrl = message.videoUrl,
//               let url = URL(string: videoUrl) {
//                VideoPlayerView(videoURL: url)
//            }
//        }
    }
}

#Preview {
    OtherChatContentView(message: ChatMessage(
        shipId: 10000003,
        messageId: "92c2dfb5-f5ed-4943-98a3-9848d7f9a962",
        type: 0,
        time: "2023-10-06T01:51:01.872Z",
        location: 1,
        from: "はだだ",
        fromId: "487420489",
        mode: 0,
        message: "999",
        stampId: 0,
        viewer: [
            Viewer(
                time: "2023-10-06T01:51:12.973Z",
                location: 1,
                id: ""),
            Viewer(
                time: "2023-10-06T01:51:12.973Z",
                location: 2,
                id: "")
        ]
    ))
}
