<?php

/**
 * Class with reusable methods for Local videos
 */
class MetaExternalVideoHelper
{
    /**
     * Get all the external videos from a single slide 
     * aka. video slide have more than one source. e.g. mp4, mov, etc.
     * 
     * @since 2.36
     * 
     * @param int $slide_id
     * 
     * @return array
     */
    public function get_all_sources( $slide_id )
    {
        $data       = array();
        $video_urls = get_post_meta( $slide_id, 'ml-slider_video_url' );

        if ( $video_urls !== false && is_array( $video_urls ) ) {
            
            foreach ( $video_urls as $url ) {
                $data[] = array(
                    'url' => $url,
                    'type' => $this->get_video_mime_type( $url ),
                    'format' => $this->get_video_format( $url )
                );
            }
        }

        return $data;
    }

    /**
     * Check if video type is allowed (mp4, webm and mov)
     * 
     * @since 2.35
     * 
     * @param string $video_url Full video url. e.g. 'video/mp4'
     * 
     * @return string|boolean The file format. e.g. 'mp4' or false if format is not valid/allowed
     */
    public function get_video_format( $video_url )
    {
        $allowed = array(
            'mov',
            'mp4',
            'webm'
        );

        /* Extract extension from $video_url. 
         * e.g. 'https://website.any/path/to/video.mp4' -> 'mp4' */
        $path       = parse_url( $video_url, PHP_URL_PATH );
        $extension  = pathinfo( $path, PATHINFO_EXTENSION );
 
        if( ! in_array( $extension, $allowed ) ) {
            return false;
        }

        return $extension;
    }

    /**
     * Return the video type. e.g. 'video/mp4'
     *
     * @param string $video_url The URL of the video. e.g. 'https://website.any/path/to/video.mp4'
     * 
     * @return string|boolean The mime type. e.g. 'video/mp4' or false if format is not valid/allowed
     */
    public function get_video_mime_type( $video_url )
    {
        $extension = $this->get_video_format( $video_url );

        if( ! $extension ) {
            return false;
        }

        // mov extension mime is different
        if ( $extension === 'mov' ) {
            $mime_type = 'video/quicktime';
        } else {
            // 'video/webm' or 'video/mp4'
            $mime_type = 'video/' . $extension;
        }

        return $mime_type;
    }

    /**
     * Video source HTML for admin screen
     * 
     * @since 2.36
     * 
     * @param array $video  Array with video data including post id, url, format
     * @param int $slide_id
     * @param string $slide_type e.g. 'layer_slide'
     * 
     * @return html
     */
    public function admin_video_source_row( $video, $slide_id, $slide_type = 'external_video' )
    {
        // name="attachment[' . esc_attr( $this->slide->ID ) . '][url]" 
        $row = '<div class="row">
            <div class="flex">
                <span class="external-video-format" data-format="' . 
                    esc_attr( $video['format'] ) . '"></span>
                <input class="url external_video_url border-l-0 border-r-0 m-0" type="text" data-slide-id="' . 
                    esc_attr( $slide_id ) . '" data-format="' . 
                    esc_attr( $video['format'] ) . '" placeholder="' . 
                    esc_attr__( 'Paste a video URL', 'ml-slider-pro' ) . '" value="' . 
                    esc_url( $video['url'] ) . '" data-video-url="' . 
                    esc_url( $video['url'] ) . '" />
                <button data-slide-id="' . 
                    esc_attr( $slide_id ) . '" data-slide-type="' . 
                    esc_attr( $slide_type ) . '" class="remove-external-video-source button button-secondary ms-button-danger">
                    <i>
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x">
                            <line x1="18" y1="6" x2="6" y2="18"></line>
                            <line x1="6" y1="6" x2="18" y2="18"></line>
                        </svg>
                    </i>
                </button>
            </div>
        </div>';

        return $row;
    }

    /**
     * Check if video type is allowed (mp4, webm and mov)
     * 
     * @param string $video_url The URL of the video. e.g. 'https://website.any/path/to/video.mp4'
     * 
     * @return boolean
     */
    public function video_type_is_allowed( $video_url )
    {
        $allowed = array(
            'video/quicktime',
            'video/mp4',
            'video/webm'
        );

        if ( ! in_array( $this->get_video_mime_type( $video_url ), $allowed ) ) {
            return false;
        }

        return true;
    }
}
