<craft name="chair">
    <parameter name="legHeight" type="int" default="14"/>
    <parameter name="length" type="int" default="15"/>
    <parameter name="width" type="int" default="15"/>
    <script>

        function main() {
            var c, d;
            var backMain, backDome, backSupportL, backSupportR;
            var leg, legSupport, legSupportL, legSupportR, legFL, legBL, legFR, legBR;

            if (params.legHeight < 5)
                params.legHeight = 5;

            // seat components
            // seat base
            c = cube({size: [params.length, params.width, 1.5]}).translate([0, 0, 14]);

            // seat back
            backMain = cube({size: [1, params.width, 7.5]});
            backDome = cylinder({
                r: (params.width - 2) / 2, 
                h: 1
            });
            backSupportL = cube({size: [2, 2, 16]});
            backSupportR = backSupportL;
            
            // seat back transformations
            backMain = backMain.translate([0.2, 0, 10]);
            backDome = difference(
                backDome,
                cube({size: [params.width / 2, params.width, 1]}).translate([1, -params.width / 2, 0])
            );
            backDome = rotate([0, 90, 0], backDome).translate([0.2, params.width / 2, 18.5]);
            backSupportL = backSupportL.translate([0, 0, 7]);
            backSupportR = backSupportR.translate([0, params.width - 2, 7]);

            d = union(
                backMain, 
                backDome, 
                backSupportL, 
                backSupportR
            ).translate([0, 0, 8]);

            d = union(c, d);    // seat base + back

            // leg components
            // legs
            leg = cube({size: [2, 2, params.legHeight]});
            legSupport = cube({size: [params.length - 1, 1.5, 1.5]});
            
            // leg transformations
            legFL = leg.translate([params.length - 2, 0, 14 - params.legHeight]);
            legFR = leg.translate([params.length - 2, params.width - 2, 14 - params.legHeight]);
            legBL = leg.translate([0, 0, 14 - params.legHeight]);
            legBR = leg.translate([0, params.width - 2, 14 - params.legHeight]);
            
            legSupportL = legSupport.translate([1, 0, 17 - params.legHeight]);
            legSupportR = legSupport.translate([1, params.width - 1.5, 17 - params.legHeight]);
            
            return union(
                d, 
                legFL, 
                legFR, 
                legBL, 
                legBR, 
                legSupportL, 
                legSupportR
            );
        }
        
    </script>
</craft>
