/*
 * blinkgtk_browser.c — BlinkGTK の最小構成サンプル
 *                      A minimal BlinkGTK sample
 *
 * ウィンドウを 1 つ開き、BlinkWebView で https://google.com/ を表示するだけの
 * プログラムです。BlinkGTK を組み込むのに最低限必要なものだけを示します。
 *
 * Opens a single window and displays https://google.com/ in a BlinkWebView.
 * It shows only what is strictly required to embed BlinkGTK.
 *
 * ビルド / Build:
 *     make
 * または / or:
 *     cc -o blinkgtk_browser blinkgtk_browser.c $(pkg-config --cflags --libs blinkgtk-0.1)
 *
 * 実行 / Run:
 *     ./blinkgtk_browser
 *     ./blinkgtk_browser https://example.com/     (URL を指定する場合 / to specify a URL)
 *
 * Copyright 2026 BlinkGTK Project
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <blink_gtk/blink_gtk.h>
#include <gtk/gtk.h>

/* 起動時に開く URL / The URL opened at startup */
#define DEFAULT_URL "https://google.com/"

/* ウィンドウを閉じたらメインループを抜ける。
 * Quit the main loop when the window is closed. */
static gboolean on_close_request(GtkWindow *window, gpointer user_data)
{
    (void)window;
    (void)user_data;
    blink_gtk_quit_main_loop();
    return FALSE;
}

int main(int argc, char **argv)
{
    /* 1. Chromium のリソース (icudtl.dat / *.pak / V8 スナップショット) の場所を
     *    教える。BlinkGTK はこれらを **実行ファイルのあるディレクトリ基準**で
     *    探すため、自分のアプリを別の場所に置く場合は明示が必要になる。
     *    指定しないと "Invalid file descriptor to ICU data" で起動に失敗する。
     *
     *    BLINKGTK_RESOURCES 環境変数があればそれを使い、無ければ標準の
     *    インストール先を試す。
     *
     *    Tell BlinkGTK where Chromium's resources (icudtl.dat, *.pak, the V8
     *    snapshots) are. They are looked up relative to the directory containing
     *    the executable, so an application placed elsewhere has to say where they
     *    are. Without this, startup fails with
     *    "Invalid file descriptor to ICU data".
     *
     *    Uses $BLINKGTK_RESOURCES if set, otherwise the standard install path. */
    const char *res = g_getenv("BLINKGTK_RESOURCES");
    if (!res) {
        res = "/usr/lib64/blinkgtk-0.1/chromium";
    }
    blink_gtk_set_resources_path(res);
    blink_gtk_set_icu_data_path(res);

    /* 2. BlinkGTK (Chromium) を初期化する。GTK より先に呼ぶ。
     *    Initialize BlinkGTK (Chromium). Call this before GTK. */
    if (!blink_gtk_init(&argc, &argv)) {
        g_printerr("blink_gtk_init() に失敗しました / failed\n");
        return 1;
    }

    gtk_init();

    const char *url = (argc > 1 && argv[1][0] != '-') ? argv[1] : DEFAULT_URL;

    /* 3. ウィンドウと BlinkWebView を作る。
     *    blink_web_view_new() は GtkWidget * を返すので、BlinkGTK の API に
     *    渡すときは BLINK_WEB_VIEW() でキャストする。
     *
     *    Create the window and the BlinkWebView. blink_web_view_new() returns a
     *    GtkWidget *, so cast with BLINK_WEB_VIEW() when passing it to the
     *    BlinkGTK API. */
    GtkWidget *window = gtk_window_new();
    gtk_window_set_title(GTK_WINDOW(window), "BlinkGTK");
    gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);
    g_signal_connect(window, "close-request", G_CALLBACK(on_close_request), NULL);

    GtkWidget *view = blink_web_view_new();
    gtk_window_set_child(GTK_WINDOW(window), view);

    /* 4. ウィンドウを表示してから URL を読み込む。
     *    Present the window, then load the URL. */
    gtk_window_present(GTK_WINDOW(window));
    blink_web_view_load_uri(BLINK_WEB_VIEW(view), url);

    /* 5. Chromium のメインループを回す。
     *    GTK の g_application_run() ではなく、必ずこちらを使う。
     *    BlinkGTK は Chromium のメインループ上で動くため、GTK のループを
     *    使うとレンダラからフレームが届かず、画面が白いままになる。
     *
     *    Run Chromium's main loop. Always use this instead of GTK's
     *    g_application_run(): BlinkGTK runs on Chromium's main loop, and using
     *    GTK's loop means no frames arrive from the renderer, leaving the
     *    window blank. */
    return blink_gtk_run_main_loop();
}