import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
        LocalCharacterMakerView()
            .ignoresSafeArea(.keyboard)
            .background(Color(red: 1.0, green: 0.97, blue: 0.925))
    }
}

struct LocalCharacterMakerView: UIViewRepresentable {
    func makeUIView(context: Context) -> WKWebView {
        let configuration = WKWebViewConfiguration()
        configuration.allowsInlineMediaPlayback = true
        configuration.defaultWebpagePreferences.allowsContentJavaScript = true

        let webView = WKWebView(frame: .zero, configuration: configuration)
        webView.isOpaque = false
        webView.backgroundColor = UIColor(red: 1.0, green: 0.97, blue: 0.925, alpha: 1.0)
        webView.scrollView.backgroundColor = webView.backgroundColor
        webView.scrollView.contentInsetAdjustmentBehavior = .never

        guard let indexURL = Bundle.main.url(
            forResource: "index",
            withExtension: "html",
            subdirectory: "www/iphone"
        ) else {
            webView.loadHTMLString("<h1>Missing bundled iPhone app files.</h1>", baseURL: nil)
            return webView
        }

        let readAccessURL = indexURL
            .deletingLastPathComponent()
            .deletingLastPathComponent()
        webView.loadFileURL(indexURL, allowingReadAccessTo: readAccessURL)
        return webView
    }

    func updateUIView(_ webView: WKWebView, context: Context) {}
}
