"""Render an original front-facing native reference before digital squeeze preparation."""

import argparse
import json
import sys
from pathlib import Path

import bpy
from mathutils import Vector

p = argparse.ArgumentParser(description=__doc__)
p.add_argument("output", type=Path)
a = p.parse_args(sys.argv[sys.argv.index("--") + 1 :])
a.output.mkdir()
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=False)
s = bpy.context.scene
s.render.engine = "CYCLES"
s.cycles.samples = 32
s.render.resolution_x = 2400
s.render.resolution_y = 1000
s.render.resolution_percentage = 100
s.render.pixel_aspect_x = s.render.pixel_aspect_y = 1
s.world.color = (0.12, 0.12, 0.12)


def mat(name, color):
    m = bpy.data.materials.new(name)
    m.use_nodes = True
    bsdf = m.node_tree.nodes.get("Principled BSDF")
    bsdf.inputs["Base Color"].default_value = (*color, 1)
    bsdf.inputs["Roughness"].default_value = 0.45
    return m


teal = mat("Original teal", (0.04, 0.32, 0.34))
cream = mat("Cream", (0.9, 0.82, 0.64))
coral = mat("Coral", (0.9, 0.3, 0.18))
navy = mat("Navy", (0.02, 0.05, 0.085))


def cube(name, loc, size, material):
    bpy.ops.mesh.primitive_cube_add(size=1, location=loc)
    o = bpy.context.object
    o.name = name
    o.scale = size
    bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
    o.data.materials.append(material)
    mod = o.modifiers.new("Original soft edges", "BEVEL")
    mod.width = 0.055
    mod.segments = 3
    o.modifiers.new("Weighted normals", "WEIGHTED_NORMAL")
    return o


cube("Ground", (0, 0, -0.15), (200, 200, 0.2), navy)
cube("Courier body", (-1.6, 0, 1.0), (1.0, 0.65, 1.1), teal)
cube("Courier head", (-1.6, 0, 1.94), (1.2, 0.72, 0.65), cream)
for x in [-1.88, -1.32]:
    cube("Original eye", (x, -0.38, 2.0), (0.15, 0.07, 0.15), navy)
for x in [-1.9, -1.3]:
    cube("Original foot", (x, -0.04, 0.21), (0.33, 0.8, 0.35), teal)
cube("Delivery parcel", (-0.7, -0.3, 1.0), (0.65, 0.7, 0.55), coral)
cube("Parcel stripe", (-0.7, -0.66, 1.0), (0.12, 0.03, 0.53), cream)
bpy.ops.mesh.primitive_torus_add(
    major_radius=0.9,
    minor_radius=0.10,
    major_segments=96,
    minor_segments=20,
    location=(1.6, 0.15, 1.6),
    rotation=(1.5707963267948966, 0, 0),
)
o = bpy.context.object
o.name = "Front facing circle reference"
o.data.materials.append(coral)
for poly in o.data.polygons:
    poly.use_smooth = True
cube("Circle stand", (1.6, 0.15, 0.47), (0.12, 0.16, 0.6), cream)
cube("Circle base", (1.6, 0.15, 0.12), (0.9, 0.5, 0.15), cream)
bpy.ops.object.camera_add(location=(0, -10, 1.5))
cam = bpy.context.object
cam.name = "Front orthographic reference camera"
cam.rotation_euler = (Vector((0, 0, 1.5)) - cam.location).to_track_quat("-Z", "Y").to_euler()
cam.data.type = "ORTHO"
cam.data.ortho_scale = 8
s.camera = cam
for loc, power, size in [((1, -4, 6), 1800, 5), ((-4, -1, 4), 1200, 4)]:
    bpy.ops.object.light_add(type="AREA", location=loc)
    o = bpy.context.object
    o.data.energy = power
    o.data.size = size
    o.rotation_euler = (Vector((0, 0, 1)) - o.location).to_track_quat("-Z", "Y").to_euler()
s.render.filepath = str(a.output / "anamorphic-original.png")
bpy.ops.render.render(write_still=True)
bpy.ops.wm.save_as_mainfile(filepath=str(a.output / "anamorphic-original.blend"))
(a.output / "anamorphic-native-record.json").write_text(
    json.dumps(
        {
            "Blender": bpy.app.version_string,
            "scope": "Original orthographic native reference later digitally squeezed not anamorphic lens footage",
            "size": [2400, 1000],
            "pixelAspect": [1, 1],
            "cameraType": cam.data.type,
            "cameraMatrix": [list(row) for row in cam.matrix_world],
        },
        indent=2,
    )
    + "\n"
)
