import json
import re

import project_sets
from django.urls import reverse
from django.utils.html import strip_tags


def create_videoobject(video_path, name, description, DT):
    video_id = video_path.split('/')[-1]
    thumbs = list(map(lambda s: "https://img.youtube.com/vi/{0}/{1}.jpg".format(video_id, str(s)), range(1, 5)))

    data = {
        "@context": "https://schema.org",
        "@type": "VideoObject",
        "name": name,
        "description": description,
        "thumbnailUrl": thumbs,
        "uploadDate": DT.isoformat(),
        # "duration": "PT1M54S", # продолжительность видео
        # "contentUrl": "https://www.example.com/video/123/file.mp4", # адрес к видеофайлу
        "embedUrl": video_path,
        # "interactionStatistic": {    # количество просмотров
        #     "@type": "InteractionCounter",
        #     "interactionType": { "@type": "WatchAction" },
        #     "userInteractionCount": 5647018
        # },
        # "regionsAllowed": "US,NL" # разрешенные регионы
    }
    return data


def get_ld_videoobjects_for_page_html(obj, name, description, DT, content):
    from BaseModels.inter import get_all_videos_from_html_content
    res_list = []

    if obj.video:
        data = create_videoobject(obj.video, name, description, DT)
        res_list.append(json.dumps(data))

    if not content:
        return res_list

    videos_list = get_all_videos_from_html_content(content)
    # if videos_list:
    #     img_list = list(map(lambda img: "{0}{1}".format(project_sets.domain, img), videos_list))

    for video_path in videos_list:
        if not video_path in obj.video and not obj.video in video_path:
            data = create_videoobject(video_path, name, description, DT)
            res_list.append(json.dumps(data))

    return res_list
