//
//  ChatUrlImageView.swift
//  Sailassist
//
//  Created by 三浦薫巳 on 2024/07/14.
//

import SwiftUI

struct ChatUrlImageView: View {
    let imageUrl: String
    var onLoad: (() -> Void)? = nil

    var body: some View {
        if let url = URL(string: imageUrl) {
            AsyncImage(url: url) { phase in
                switch phase {
                case .success(let image):
                    image
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 250)
                        .onAppear {
                            onLoad?()
                        }
                case .failure(_):
                    VStack {
                        Image(systemName: "exclamationmark.triangle")
                        Text("Image loading failed")
                            .font(.caption)
                    }
                    .frame(width: 250, height: 250)
                    .background(Color.gray.opacity(0.2))
                case .empty:
                    ProgressView()
                        .frame(width: 250, height: 250)
                        .background(Color.gray.opacity(0.2))
                @unknown default:
                    EmptyView()
                }
            }
        } else {
            Color.gray.opacity(0.2)
                .frame(width: 250, height: 250)
                .overlay(Text("Invalid URL"))
        }
    }
}

