//
//  LargeLinkPreviewView.swift
//  SimpleX
//
//  Created by Ian Davies on 07/04/2022.
//  Copyright © 2022 SimpleX Chat. All rights reserved.
//

import SwiftUI
import SimpleXChat

struct CILinkView: View {
    @Environment(\.colorScheme) var colorScheme
    let linkPreview: LinkPreview

    var body: some View {
        VStack(alignment: .center, spacing: 6) {
            if let data = Data(base64Encoded: dropImagePrefix(linkPreview.image)),
               let uiImage = UIImage(data: data) {
                Image(uiImage: uiImage)
                    .resizable()
                    .scaledToFit()
            }
            VStack(alignment: .leading, spacing: 6) {
                Text(linkPreview.title)
                    .lineLimit(3)
//                if linkPreview.description != "" {
//                    Text(linkPreview.description)
//                        .font(.subheadline)
//                        .lineLimit(12)
//                }
                Text(linkPreview.uri.absoluteString)
                    .font(.caption)
                    .lineLimit(1)
                    .foregroundColor(.secondary)
            }
            .padding(.horizontal, 12)
            .frame(maxWidth: .infinity, alignment: .leading)
        }
    }
}

struct LargeLinkPreview_Previews: PreviewProvider {
    static var previews: some View {
        let preview = LinkPreview(
            uri: URL(string: "http://DuckDuckGo.com")!,
            title: "Privacy, simplified.",
            description: "",
            image: "data:image/jpg;base64,[encoded data, 8224 characters]"
        )
        CILinkView(linkPreview: preview)
            .previewLayout(.fixed(width: 360, height: 200))
    }
}
