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

import SwiftUI

struct OtherChatContentView: View {
    var message : ChatMessage
    var body: some View {
        HStack {
            VStack(alignment: .leading, spacing: 4) {
                Text(message.from)
                    .font(FontStyle.EmphasisText.font)
                    .foregroundColor(ColorSet.Body.color)
                VStack(alignment: .leading, spacing: 10) {
                    Group {
                        if message.message.contains("https://") {   //TODO: stampIdが0
                            AsyncImage(url: URL(string: message.message)) { image in
                                image.resizable()
                                    .frame(width: 200, height: 200)
                                    .contextMenu {
                                        Button(action: {
                                            print("fight")
                                        }) {
                                            Text("Share...")
                                            Image(systemName: "square.and.arrow.up")
                                        }

                                        Button(action: {
                                            print("bag")
                                        }) {
                                            Text("Save to Photos")
                                            Image(systemName: "square.and.arrow.down")
                                        }

                                        Button(action: {
                                            print("pokemon")
                                        }) {
                                            Text("Copy")
                                            Image(systemName: "doc.on.doc")
                                        }

                                        Button(action: {
                                            print("run")
                                        }) {
                                            Text("Copy Subject")
                                            Image(systemName: "circle.dashed.rectangle")
                                        }

                                        Button(action: {
                                            print("run")
                                        }) {
                                            Text("Show Text")
                                            Image(systemName: "text.viewfinder")
                                        }
                                    }
                            } placeholder: {
                                ProgressView()
                            }
                        } else {
                            Text(message.message)
                                .font(FontStyle.DefaultText.font)
                                .foregroundColor(ColorSet.BodyChat.color)
                                .padding(15)
                                .background(ColorSet.ChatBaloon.color)
                                .cornerRadius(10, corners: [.tl, .tr, .br])
                        }
                    }

                    HStack(alignment: .top){
                        Text(DateTextLib.ISO86012FormatText(message.time, format: "yyyy/MM/dd hh:mm", errFormat: ""))
                            .font(FontStyle.DateText.font)
                            .foregroundColor(ColorSet.ChatDate.color)
                    }
                }
            }
            .onAppear {
                //既読確認
                let result = message.viewer.first(where: {$0.id == String(SharingData.my.id)})
                if result == nil {
                    let signalRService = SignalR()
                    signalRService.ackMessage(messageId: message.messageId)
                }
            }
            .padding(.leading, 20)
            Spacer()
        }
        .padding(.trailing, 80)
    }
}

#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",
        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: "")
        ]
    ))
}
