const std = @import("std");
const Canvas = @import("Canvas.zig");
const CpuRenderEngine = @import("render/CpuRenderEngine.zig");
const RenderEngine = @import("render/RenderEngine.zig").RenderEngine;
const Document = @import("models/Document.zig");
const random_document = @import("random_document.zig");
const basic_models = @import("models/basic_models.zig");

const WindowContext = @This();

pub const OpenDocument = struct {
    document: Document,
    cpu_render: CpuRenderEngine,
    canvas: Canvas,
    /// Выбранный объект в дереве (id объекта).
    selected_object_id: ?u64 = null,

    pub fn init(allocator: std.mem.Allocator, self: *OpenDocument) void {
        initWithDocument(allocator, self, .init(.{
            .w = 800,
            .h = 600,
        }));
    }

    pub fn initWithDocument(allocator: std.mem.Allocator, self: *OpenDocument, doc: Document) void {
        self.document = doc;
        self.cpu_render = CpuRenderEngine.init(allocator, .Squares);
        self.canvas = Canvas.init(
            allocator,
            &self.document,
            (&self.cpu_render).renderEngine(),
        );
        self.selected_object_id = null;
    }

    pub fn deinit(self: *OpenDocument, allocator: std.mem.Allocator) void {
        self.document.deinit(allocator);
        self.canvas.deinit();
    }
};

allocator: std.mem.Allocator,
documents: std.ArrayList(*OpenDocument),
active_document_index: ?usize,

pub fn init(allocator: std.mem.Allocator) !WindowContext {
    const documents = std.ArrayList(*OpenDocument).empty;
    const active_document_index: ?usize = null;
    return .{
        .allocator = allocator,
        .documents = documents,
        .active_document_index = active_document_index,
    };
}

pub fn deinit(self: *WindowContext) void {
    for (self.documents.items) |ptr| {
        ptr.deinit(self.allocator);
        self.allocator.destroy(ptr);
    }
    self.documents.deinit(self.allocator);
}

pub fn activeDocument(self: *WindowContext) ?*OpenDocument {
    const i = self.active_document_index orelse return null;
    if (i >= self.documents.items.len) return null;
    return self.documents.items[i];
}

pub fn addNewDocument(self: *WindowContext) !void {
    const ptr = try self.allocator.create(OpenDocument);
    errdefer self.allocator.destroy(ptr);
    OpenDocument.init(self.allocator, ptr);
    //try random_document.addRandomShapes(&ptr.document, std.crypto.random);
    try self.documents.append(self.allocator, ptr);
    self.active_document_index = self.documents.items.len - 1;
}

pub fn addDocument(self: *WindowContext, doc: Document) !void {
    const ptr = try self.allocator.create(OpenDocument);
    errdefer self.allocator.destroy(ptr);
    var doc_mut = doc;
    errdefer doc_mut.deinit(self.allocator);
    OpenDocument.initWithDocument(self.allocator, ptr, doc_mut);
    try self.documents.append(self.allocator, ptr);
    self.active_document_index = self.documents.items.len - 1;
}

pub fn setActiveDocument(self: *WindowContext, index: usize) void {
    if (index < self.documents.items.len) {
        self.active_document_index = index;
    }
}

pub fn closeDocument(self: *WindowContext, index: usize) void {
    if (index >= self.documents.items.len) return;
    const open_doc = self.documents.items[index];
    open_doc.deinit(self.allocator);
    self.allocator.destroy(open_doc);
    _ = self.documents.orderedRemove(index);

    if (self.active_document_index) |*active| {
        if (index < active.*) {
            active.* -= 1;
        } else if (index == active.*) {
            if (self.documents.items.len > 0) {
                active.* = @min(index, self.documents.items.len - 1);
            } else {
                self.active_document_index = null;
            }
        }
    }
}
