"""
Tutorial 2: a small house on a raised foundation, built with the builders.

Run it with::

    mciwb run house.py
"""

from pathlib import Path

from mciwb.imports import (
    BlockState,
    Direction,
    Item,
    Placer,
    Schematic,
    Vec3,
    Volume,
    arch,
    column,
    foundation,
    gable_roof,
    get_client,
    get_world,
    road,
    stairway,
)

# the north west corner of the site, one block above the ground
ORIGIN = Vec3(632, 73, -1658)
# the render of the house, beside this script
PNG = Path(__file__).with_suffix(".png")

world = get_world()

# -- site --
# clear the site first: 13 blocks east, 22 south and 13 up from ORIGIN
site = Volume.from_corners(ORIGIN, ORIGIN + Vec3(12, 12, 21))
site.fill(get_client(), Item.AIR)

# -- foundation --
# a level platform 3 blocks above the ground, filled down to the ground in
# each column. It is 11 x 13: the house plus a porch on its south side
floor = ORIGIN + Vec3(1, 2, 1)
foundation(floor, floor + Vec3(10, 0, 12), Item.COBBLESTONE)

# -- walls --
# the walls stand one block in from the edge of the platform: nw is their
# bottom north west corner and se their top south east corner
nw = floor + Vec3(1, 1, 1)
se = nw + Vec3(8, 3, 8)
ring = {
    (x, z): Item.OAK_LOG if x in (nw.x, se.x) and z in (nw.z, se.z) else Item.BRICKS
    for x in range(nw.x, se.x + 1)
    for z in range(nw.z, se.z + 1)
    if x in (nw.x, se.x) or z in (nw.z, se.z)
}
placer = Placer()
for y in range(nw.y, se.y + 1):
    placer.layer(y, ring)
# a window in the east wall and one in the west wall
for x in (nw.x, se.x):
    placer.fill(
        Vec3(x, nw.y + 1, nw.z + 3), Vec3(x, nw.y + 2, se.z - 3), Item.GLASS_PANE
    )
walls = placer.volume

# -- door --
# an arched doorway through the south wall: the arch is 5 blocks wide,
# feet included, and clear=True cuts the opening under it
foot = Vec3(nw.x + 2, nw.y, se.z)
arch(foot, foot + Direction.EAST * 4, Item.STONE_BRICKS, style="pointed", clear=True)
# a sign on the wall beside it. Block entity data, here the sign's text,
# goes with the block state
sign = BlockState.of(Item.OAK_WALL_SIGN, facing=Direction.SOUTH).with_nbt(
    '{front_text:{messages:["","Tutorial","house",""]}}'
)
placer.setblock(Vec3(se.x - 1, nw.y + 1, se.z + 1), sign)

# -- roof --
# the roof sits on the walls: give it their top corners. Its ridge runs
# north to south, so there is a gable over the door
roof = gable_roof(
    Vec3(walls.start.x, walls.end.y, walls.start.z),
    walls.end,
    Item.DARK_OAK_STAIRS,
    gable=Item.DARK_OAK_PLANKS,
    axis="z",
    overhang=1,
)

# -- columns --
# a column at each end of the porch
for x in (nw.x, se.x):
    column(
        Vec3(x, nw.y, se.z + 2),
        4,
        Item.QUARTZ_PILLAR,
        plinth=Item.SMOOTH_QUARTZ,
        capital=Item.SMOOTH_QUARTZ,
    )

# -- stairs --
# steps 3 wide from the ground up to the porch. The points are the blocks
# that you walk on: the bottom one is level with the ground
bottom = ORIGIN + Vec3(6, -1, 17)
top = Vec3(bottom.x, floor.y, bottom.z - 3)
stairway([bottom, top], Item.STONE_BRICK_STAIRS, width=3, support=Item.COBBLESTONE)
# a path over the ground from the bottom of the steps
road([bottom + Direction.SOUTH, bottom + Direction.SOUTH * 4], Item.GRAVEL)

# -- check --
# render the whole site, with a layer of the ground under it
grid = world.render(
    site.start + Direction.DOWN, site.end, PNG, views=["iso", "south", "top"]
)
assert grid[foot + Direction.EAST * 2] == "minecraft:air", "the doorway is blocked"
print(f"walls {walls.size.x} x {walls.size.z}, roof up to y = {roof.end.y}")
# renders draw stairs as full blocks, so read back the lowest row of the
# roof to see which way its stairs face
row = Volume.from_corners(
    Vec3(roof.start.x, roof.start.y, nw.z), Vec3(roof.end.x, roof.start.y, nw.z)
)
eaves = Schematic.from_world(row).blocks[:, 0, 0]
print(f"eaves face {eaves[0].props['facing']} and {eaves[-1].props['facing']}")
