beglaze = {}

beglaze.grid = {
    layouts = {
        default = {
            name = "default",
            rows = 3,
            cols = 4,
            margin = 10,
            padding = 5
        },
        ultrawide = {
            name = "ultrawide",
            rows = 2,
            cols = 6,
            margin = 15,
            padding = 8
        },
        vertical = {
            name = "vertical",
            rows = 4,
            cols = 2,
            margin = 12,
            padding = 6
        }
    },
    current_layout = "default",
    show_overlay = false,
    overlay_opacity = 0.3,
    overlay_color = "#00FF00",
    highlight_color = "#FF0000"
}

beglaze.hotkeys = {
    move_left = "Win+Shift+Left",
    move_right = "Win+Shift+Right",
    move_up = "Win+Shift+Up",
    move_down = "Win+Shift+Down",
    snap_window = "Win+Ctrl+Enter",
    toggle_grid = "Win+Alt+G",
    refresh_grid = "Win+Alt+R",
    presets = {
        ["Win+Ctrl+1"] = {row = 0, col = 0, width = 2, height = 2},
        ["Win+Ctrl+2"] = {row = 0, col = 2, width = 2, height = 2},
        ["Win+Ctrl+3"] = {row = 2, col = 0, width = 2, height = 1},
        ["Win+Ctrl+4"] = {row = 2, col = 2, width = 2, height = 1},
        ["Win+Ctrl+5"] = {row = 0, col = 0, width = 1, height = 3},
        ["Win+Ctrl+6"] = {row = 0, col = 1, width = 2, height = 3},
        ["Win+Ctrl+7"] = {row = 0, col = 3, width = 1, height = 3},
        ["Win+Ctrl+8"] = {row = 0, col = 0, width = 4, height = 1},
        ["Win+Ctrl+9"] = {row = 1, col = 0, width = 4, height = 2}
    }
}

beglaze.widgets = {
    enabled = true,
    auto_load = true,
    directory = "widgets",
    default_transparency = 0.9,
    default_always_on_top = true,
    update_interval = 1000,
    max_widgets = 50
}

beglaze.behavior = {
    auto_snap = true,
    snap_threshold = 50,
    window_padding = 5,
    ignore_minimized = true,
    multi_monitor = true,
    animation_duration = 200,
    smooth_transitions = true
}

beglaze.appearance = {
    theme = "dark",
    accent_color = "#007ACC",
    font_family = "Segoe UI",
    font_size = 12,
    border_radius = 3,
    shadow_enabled = true
}

beglaze.exclusions = {
    window_classes = {
        "Shell_TrayWnd",
        "DV2ControlHost",
        "MsgrIMEWindowClass",
        "SysShadow",
        "Button"
    },
    process_names = {
        "dwm.exe",
        "winlogon.exe",
        "csrss.exe"
    }
}

function beglaze.get_layout(name)
    return beglaze.grid.layouts[name or beglaze.grid.current_layout]
end

function beglaze.set_layout(name)
    if beglaze.grid.layouts[name] then
        beglaze.grid.current_layout = name
        return true
    end
    return false
end

function beglaze.add_layout(layout)
    if layout.name and layout.rows and layout.cols then
        beglaze.grid.layouts[layout.name] = layout
        return true
    end
    return false
end

function beglaze.get_preset_position(hotkey)
    return beglaze.hotkeys.presets[hotkey]
end

function beglaze.is_window_excluded(class_name, process_name)
    for _, excluded_class in ipairs(beglaze.exclusions.window_classes) do
        if class_name == excluded_class then
            return true
        end
    end
    
    for _, excluded_process in ipairs(beglaze.exclusions.process_names) do
        if process_name == excluded_process then
            return true
        end
    end
    
    return false
end

function beglaze.calculate_grid_position(window_rect, layout)
    layout = layout or beglaze.get_layout()
    
    local screen_width = 1920
    local screen_height = 1080
    
    local usable_width = screen_width - (2 * layout.margin)
    local usable_height = screen_height - (2 * layout.margin)
    
    local cell_width = (usable_width - ((layout.cols - 1) * layout.padding)) / layout.cols
    local cell_height = (usable_height - ((layout.rows - 1) * layout.padding)) / layout.rows
    
    local best_row = math.floor((window_rect.top - layout.margin) / (cell_height + layout.padding))
    local best_col = math.floor((window_rect.left - layout.margin) / (cell_width + layout.padding))
    
    best_row = math.max(0, math.min(best_row, layout.rows - 1))
    best_col = math.max(0, math.min(best_col, layout.cols - 1))
    
    return {
        row = best_row,
        col = best_col,
        width = 1,
        height = 1
    }
end

function beglaze.get_cell_rect(position, layout)
    layout = layout or beglaze.get_layout()
    
    local screen_width = 1920
    local screen_height = 1080
    
    local usable_width = screen_width - (2 * layout.margin)
    local usable_height = screen_height - (2 * layout.margin)
    
    local cell_width = (usable_width - ((layout.cols - 1) * layout.padding)) / layout.cols
    local cell_height = (usable_height - ((layout.rows - 1) * layout.padding)) / layout.rows
    
    local x = layout.margin + position.col * (cell_width + layout.padding)
    local y = layout.margin + position.row * (cell_height + layout.padding)
    local width = position.width * cell_width + (position.width - 1) * layout.padding
    local height = position.height * cell_height + (position.height - 1) * layout.padding
    
    return {
        left = math.floor(x),
        top = math.floor(y),
        right = math.floor(x + width),
        bottom = math.floor(y + height)
    }
end

function beglaze.validate_position(position, layout)
    layout = layout or beglaze.get_layout()
    
    if position.row < 0 or position.col < 0 then
        return false
    end
    
    if position.row + position.height > layout.rows then
        return false
    end
    
    if position.col + position.width > layout.cols then
        return false
    end
    
    return true
end

function beglaze.on_window_created(window_info)
    if beglaze.behavior.auto_snap and not beglaze.is_window_excluded(window_info.class_name, window_info.process_name) then
        local position = beglaze.calculate_grid_position(window_info.rect)
        return position
    end
    return nil
end

function beglaze.on_window_moved(window_info)
    if beglaze.behavior.auto_snap then
        local position = beglaze.calculate_grid_position(window_info.rect)
        if beglaze.validate_position(position) then
            return position
        end
    end
    return nil
end

function beglaze.on_layout_changed(old_layout, new_layout)
    print("Layout changed from " .. old_layout .. " to " .. new_layout)
end

function beglaze.on_widget_created(widget_info)
    print("Widget created: " .. widget_info.name)
end

function beglaze.on_hotkey_pressed(hotkey, window_info)
    print("Hotkey pressed: " .. hotkey)
    return beglaze.get_preset_position(hotkey)
end

return beglaze
