const std = @import("std");
const dvui = @import("dvui");
const WindowContext = @import("../WindowContext.zig");
const canvas_view = @import("canvas_view.zig");

pub fn rightPanel(ctx: *WindowContext) void {
    const fill_color = dvui.Color.black.opacity(0.25);
    var back = dvui.box(
        @src(),
        .{ .dir = .horizontal },
        .{ .expand = .both, .padding = dvui.Rect.all(12), .background = true },
    );
    {
        var panel = dvui.box(
            @src(),
            .{ .dir = .vertical },
            .{
                .expand = .both,
                .background = true,
                .padding = dvui.Rect.all(5),
                .corner_radius = dvui.Rect.all(24),
                .color_fill = fill_color,
            },
        );
        {
            const active_doc = ctx.activeDocument();
            if (active_doc) |doc| {
                const content_rect_scale = panel.data().contentRectScale();
                canvas_view.canvasView(&doc.canvas, doc.selected_object_id, content_rect_scale);
            } else {
                noDocView(ctx);
            }
        }
        panel.deinit();
    }
    back.deinit();
}

fn noDocView(ctx: *WindowContext) void {
    var center = dvui.box(
        @src(),
        .{ .dir = .vertical },
        .{
            .expand = .both,
            .padding = dvui.Rect.all(20),
        },
    );
    {
        var box = dvui.box(
            @src(),
            .{ .dir = .vertical },
            .{
                .gravity_x = 0.5,
                .gravity_y = 0.5,
            },
        );
        {
            dvui.label(@src(), "No document open", .{}, .{});
            if (dvui.button(@src(), "New document", .{}, .{})) {
                ctx.addNewDocument() catch |err| {
                    std.debug.print("addNewDocument error: {}\n", .{err});
                };
            }
        }
        box.deinit();
    }
    center.deinit();
}
