<?php

use Silvanus\includes\blocks\AcfBlockFields;

try {
    $fields = new AcfBlockFields('Video', __FILE__);
    $fields->addContainerSettings();

    $fields->addSelect('Source type', [
        'choices' => [
            'upload' => 'Upload (MP4/WebM)',
            'embed' => 'Embed URL (YouTube/Vimeo etc.)',
        ],
        'default_value' => 'upload',
        'ui' => 1,
        'required' => 1,
    ]);

    $fields->addFile('Video MP4', [
        'label' => 'Video (MP4)',
        'mime_types' => 'mp4',
        'return_format' => 'array',
        'required' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addFile('Video webm', [
        'label' => 'Video (WebM)',
        'mime_types' => 'webm',
        'return_format' => 'array',
        'required' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addOembed('Embed URL', [
        'label' => 'Embed URL',
        'required' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'embed',
                ],
            ],
        ],
    ]);

    $fields->addImage('Poster image', [
        'label' => 'Poster image',
        'return_format' => 'id',
        'preview_size' => 'medium',
        'required' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addTrueFalse('Minimal controls (play only)', [
        'label' => 'Minimal controls (play only)',
        'ui' => 1,
        'default_value' => 0,
        'instructions' => 'Shows a custom play button overlay and hides native controls (self-hosted videos only).',
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addTrueFalse('Controls', [
        'label' => 'Show controls',
        'ui' => 1,
        'default_value' => 1,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addTrueFalse('AutoPlay', [
        'label' => 'Autoplay',
        'ui' => 1,
        'default_value' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addTrueFalse('Loop', [
        'label' => 'Loop',
        'ui' => 1,
        'default_value' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addTrueFalse('Muted', [
        'label' => 'Muted',
        'ui' => 1,
        'default_value' => 0,
        'instructions' => 'Recommended if Autoplay is enabled (most browsers require muted autoplay).',
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addTrueFalse('PlaysInline', [
        'label' => 'Plays inline (mobile)',
        'ui' => 1,
        'default_value' => 1,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addSelect('Preload', [
        'label' => 'Preload',
        'choices' => [
            'none' => 'none',
            'metadata' => 'metadata',
            'auto' => 'auto',
        ],
        'default_value' => 'metadata',
        'ui' => 1,
        'required' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'source_type',
                    'operator' => '==',
                    'value' => 'upload',
                ],
            ],
        ],
    ]);

    $fields->addText('Caption', [
        'label' => 'Caption (optional)',
        'required' => 0,
    ]);

    return $fields->build();
} catch (\StoutLogic\AcfBuilder\FieldNameCollisionException $e) {
    // Avoid fatal errors on collision
}