SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

IF COL_LENGTH(N'dbo.FloorPlanFloors', N'BackgroundImagePath') IS NULL
BEGIN
    ALTER TABLE dbo.FloorPlanFloors ADD BackgroundImagePath nvarchar(max) NULL;
END;
GO

IF COL_LENGTH(N'dbo.FloorPlanFloors', N'BackgroundImageOriginalFileName') IS NULL
BEGIN
    ALTER TABLE dbo.FloorPlanFloors ADD BackgroundImageOriginalFileName nvarchar(255) NULL;
END;
GO

IF COL_LENGTH(N'dbo.FloorPlanFloors', N'BackgroundOpacity') IS NULL
BEGIN
    ALTER TABLE dbo.FloorPlanFloors ADD BackgroundOpacity decimal(5,2) NULL;
END;
GO

IF COL_LENGTH(N'dbo.FloorPlanFloors', N'BackgroundScale') IS NULL
BEGIN
    ALTER TABLE dbo.FloorPlanFloors ADD BackgroundScale decimal(8,3) NULL;
END;
GO

IF COL_LENGTH(N'dbo.FloorPlanFloors', N'BackgroundOffsetX') IS NULL
BEGIN
    ALTER TABLE dbo.FloorPlanFloors ADD BackgroundOffsetX decimal(10,2) NULL;
END;
GO

IF COL_LENGTH(N'dbo.FloorPlanFloors', N'BackgroundOffsetY') IS NULL
BEGIN
    ALTER TABLE dbo.FloorPlanFloors ADD BackgroundOffsetY decimal(10,2) NULL;
END;
GO

IF COL_LENGTH(N'dbo.FloorPlanFloors', N'BackgroundLocked') IS NULL
BEGIN
    ALTER TABLE dbo.FloorPlanFloors
        ADD BackgroundLocked bit NOT NULL
            CONSTRAINT DF_FloorPlanFloors_BackgroundLocked DEFAULT 1;
END;
GO

CREATE OR ALTER PROCEDURE dbo.FloorPlan_GetEditor
    @FloorPlanID int
AS
BEGIN
    SET NOCOUNT ON;

    EXEC dbo.FloorPlan_Get @FloorPlanID;

    SELECT
        fpf.FloorPlanFloorID,
        fpf.FloorPlanID,
        fpf.LocationID,
        lp.LocationName,
        lp.LocationPath,
        fpf.Name,
        fpf.SortOrder,
        fpf.GridWidth,
        fpf.GridHeight,
        fpf.BackgroundImagePath,
        fpf.BackgroundImageOriginalFileName,
        COALESCE(fpf.BackgroundOpacity, CONVERT(decimal(5,2), 0.35)) AS BackgroundOpacity,
        COALESCE(fpf.BackgroundScale, CONVERT(decimal(8,3), 1.000)) AS BackgroundScale,
        COALESCE(fpf.BackgroundOffsetX, CONVERT(decimal(10,2), 0.00)) AS BackgroundOffsetX,
        COALESCE(fpf.BackgroundOffsetY, CONVERT(decimal(10,2), 0.00)) AS BackgroundOffsetY,
        fpf.BackgroundLocked,
        fpf.CreatedDate,
        fpf.ModifiedDate
    FROM dbo.FloorPlanFloors fpf
    LEFT JOIN dbo.LocationPaths lp ON lp.LocationID = fpf.LocationID
    WHERE fpf.FloorPlanID = @FloorPlanID
    ORDER BY fpf.SortOrder, fpf.Name;

    SELECT
        fpb.FloorPlanBlockID,
        fpb.FloorPlanFloorID,
        fpb.LocationID,
        lp.LocationName,
        COALESCE(NULLIF(lp.LocationPath, N''), lp.LocationName) AS LocationPath,
        fpb.X,
        fpb.Y,
        fpb.WidthCells,
        fpb.HeightCells,
        fpb.BlockType,
        fpb.LabelOverride,
        fpb.Notes,
        fpb.CreatedDate,
        fpb.ModifiedDate
    FROM dbo.FloorPlanBlocks fpb
    INNER JOIN dbo.FloorPlanFloors fpf ON fpf.FloorPlanFloorID = fpb.FloorPlanFloorID
    INNER JOIN dbo.LocationPaths lp ON lp.LocationID = fpb.LocationID
    WHERE fpf.FloorPlanID = @FloorPlanID
    ORDER BY fpf.SortOrder, fpb.Y, fpb.X, lp.LocationName;
END;
GO

CREATE OR ALTER PROCEDURE dbo.FloorPlanFloor_Get
    @FloorPlanFloorID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        fpf.FloorPlanFloorID,
        fpf.FloorPlanID,
        fpf.LocationID,
        lp.LocationName,
        lp.LocationPath,
        fpf.Name,
        fpf.SortOrder,
        fpf.GridWidth,
        fpf.GridHeight,
        fpf.BackgroundImagePath,
        fpf.BackgroundImageOriginalFileName,
        COALESCE(fpf.BackgroundOpacity, CONVERT(decimal(5,2), 0.35)) AS BackgroundOpacity,
        COALESCE(fpf.BackgroundScale, CONVERT(decimal(8,3), 1.000)) AS BackgroundScale,
        COALESCE(fpf.BackgroundOffsetX, CONVERT(decimal(10,2), 0.00)) AS BackgroundOffsetX,
        COALESCE(fpf.BackgroundOffsetY, CONVERT(decimal(10,2), 0.00)) AS BackgroundOffsetY,
        fpf.BackgroundLocked,
        fpf.CreatedDate,
        fpf.ModifiedDate
    FROM dbo.FloorPlanFloors fpf
    LEFT JOIN dbo.LocationPaths lp ON lp.LocationID = fpf.LocationID
    WHERE fpf.FloorPlanFloorID = @FloorPlanFloorID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.FloorPlanFloor_Save
    @FloorPlanFloorID int,
    @FloorPlanID int,
    @LocationID int = NULL,
    @Name nvarchar(100),
    @SortOrder int,
    @GridWidth int,
    @GridHeight int
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @ProjectID int;
    SELECT @ProjectID = ProjectID FROM dbo.FloorPlans WHERE FloorPlanID = @FloorPlanID;

    IF @ProjectID IS NULL
        THROW 51003, 'Floor plan not found.', 1;

    SET @Name = NULLIF(LTRIM(RTRIM(@Name)), N'');
    IF @Name IS NULL
        THROW 51001, 'Floor name is required.', 1;

    IF @GridWidth NOT BETWEEN 8 AND 80 OR @GridHeight NOT BETWEEN 8 AND 80
        THROW 51002, 'Grid width and height must be between 8 and 80.', 1;

    IF @LocationID IS NOT NULL
       AND NOT EXISTS (SELECT 1 FROM dbo.Locations WHERE LocationID = @LocationID AND ProjectID = @ProjectID AND IsArchived = 0)
        THROW 51008, 'Floor location must belong to this project.', 1;

    IF @FloorPlanFloorID = 0
    BEGIN
        INSERT dbo.FloorPlanFloors (FloorPlanID, LocationID, Name, SortOrder, GridWidth, GridHeight, BackgroundOpacity, BackgroundScale, BackgroundOffsetX, BackgroundOffsetY, BackgroundLocked)
        VALUES (@FloorPlanID, @LocationID, @Name, @SortOrder, @GridWidth, @GridHeight, 0.35, 1.000, 0.00, 0.00, 1);

        SELECT CONVERT(int, SCOPE_IDENTITY());
        RETURN;
    END;

    UPDATE dbo.FloorPlanFloors
    SET LocationID = @LocationID,
        Name = @Name,
        SortOrder = @SortOrder,
        GridWidth = @GridWidth,
        GridHeight = @GridHeight,
        ModifiedDate = SYSUTCDATETIME()
    WHERE FloorPlanFloorID = @FloorPlanFloorID
      AND FloorPlanID = @FloorPlanID;

    SELECT @FloorPlanFloorID;
END;
GO

CREATE OR ALTER PROCEDURE dbo.FloorPlanFloor_UpdateBackground
    @FloorPlanFloorID int,
    @FloorPlanID int,
    @BackgroundImagePath nvarchar(max) = NULL,
    @BackgroundImageOriginalFileName nvarchar(255) = NULL,
    @BackgroundOpacity decimal(5,2) = NULL,
    @BackgroundScale decimal(8,3) = NULL,
    @BackgroundOffsetX decimal(10,2) = NULL,
    @BackgroundOffsetY decimal(10,2) = NULL,
    @BackgroundLocked bit = 1
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE dbo.FloorPlanFloors
    SET BackgroundImagePath = @BackgroundImagePath,
        BackgroundImageOriginalFileName = @BackgroundImageOriginalFileName,
        BackgroundOpacity = @BackgroundOpacity,
        BackgroundScale = @BackgroundScale,
        BackgroundOffsetX = @BackgroundOffsetX,
        BackgroundOffsetY = @BackgroundOffsetY,
        BackgroundLocked = @BackgroundLocked,
        ModifiedDate = SYSUTCDATETIME()
    WHERE FloorPlanFloorID = @FloorPlanFloorID
      AND FloorPlanID = @FloorPlanID;

    SELECT @@ROWCOUNT;
END;
GO
