//
//  MyChatContentView.swift
//  Sailassist
//
//  Created by Mamoru Sugita on 2023/11/08.
//

import SwiftUI
import AVKit

struct MyChatContentView: View {
    @State private var isVideoPlayerPresented = false
    var message : ChatMessage
    var onMediaLoaded: (() -> Void)? = nil

    var body: some View {
        HStack {
            Spacer()
            VStack(alignment: .trailing, spacing: 6) {
                Group {
                    if let msg = message.message {
                        if msg.contains(".jpg") || msg.contains(".png") {
                            ChatUrlImageView(imageUrl: msg, onLoad: onMediaLoaded)
//                                .onTapGesture {
//                                    if 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: 0,
                                        bottomTrailingRadius: 10,
                                        topTrailingRadius: 10
                                    ))
                        }
                    }
                }

                HStack(spacing: 5){
                    //MARK: - 既読マーク
                    Text(DateTextLib.ISO86012FormatText(message.time, format: "yyyy/MM/dd HH:mm", errFormat: ""))
                        .padding(.trailing, 8)

                    let viewerCnt = viewerCnt()

                    Image("chat_company")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 12, height: 12)
                        .padding(.trailing, 4)

                    Text(String(viewerCnt.1))

                    Image("chat_ship")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 12, height: 12)
                        .padding(.trailing, 4)

                    Text(String(viewerCnt.0))
                }
                .font(FontStyle.DateText.font)
                .foregroundColor(ColorSet.ChatDate.color)
            }
            .padding(.trailing, 20)
        }
//        .fullScreenCover(isPresented: $isVideoPlayerPresented) {
//            if let videoUrl = message.videoUrl,
//               let url = URL(string: videoUrl) {
//                VideoPlayerView(videoURL: url)
//            }
//        }
    }

    func viewerCnt() -> (Int, Int) {
        var shipViewer = 0
        var companyViewr = 0
        for viewer in message.viewer {
            if viewer.location == 1 {
                companyViewr += 1
            } else {
                shipViewer += 1
            }
        }
        return (shipViewer, companyViewr)
    }
}

//struct VideoPlayerView: View {
//    let videoURL: URL
//
//    var body: some View {
//        VideoPlayer(player: AVPlayer(url: videoURL))
//            .navigationTitle("動画再生")
//            .navigationBarTitleDisplayMode(.inline)
//    }
//}

#Preview {
    MyChatContentView(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: "")
        ]
    ))
}
