<?php

namespace bedwars\util;

use pocketmine\level\Position;
use pocketmine\block\Block;
use pocketmine\math\Vector3;

class Cage {

    const TYPES = array(
        'default' => array(Block::INVISIBLE_BEDROCK, Block::INVISIBLE_BEDROCK), 
        'iron' => array(Block::IRON_BLOCK, Block::IRON_BAR),
        'nether' => array(Block::NETHER_BRICKS, Block::IRON_BAR),
        'ice' => array(Block::ICE, Block::GLASS_PANEL),
        'air' => array(Block::AIR, Block::AIR)
    );

    /**
    * @param Position $position
    * @param strign   $type
    * @param bool     $double
    */
    public static function spawn(Position $position, string $type = 'default', bool $double = false) {
        $width = $double ? 2 : 2;
        $height = $double ? 4 : 3;
        
        if (isset(self::TYPES[$type])) {
            $type = self::TYPES[$type];
        } else {
            $type = self::TYPES['default'];
        }
        
        Cage::set($position, $type, $width, $height);

        if ($type[0] != Block::AIR) {
            $height--;
            Cage::set($position->add(0, 1), 'air', --$width, --$height);
        }
    }

    /**
    * @param Position  $position
    * @param string    $type
    * @param int|float $width
    * @Param int|float $height
    */
    private static function set(Position $position, $type, $width, $height) {
        for($x = $position->x - $width; $x <= $position->x + $width; $x++) {
            for($y = $position->y + $height; $y >= $position->y - 1; $y--) {
                for($z = $position->z + $width; $z >= $position->z - $width; $z--){
                    $blockId = (int) $type[0];
                    if($y >= $position->y and $y <= $position->y + ($height - 1)){
                        $blockId = (int) $type[1];
                    }
                    $position->level->setBlockIdAt($x, $y, $z, $blockId);
                }
            }
        }
    }
}
?>