#!/usr/bin/env python3

from dataclasses import dataclass, field
from typing import List
from enum import Enum
from datetime import date

from webvtt import Caption


YOUTUBE_BASE = "http://www.youtube.com/watch?"


class CaptionType(Enum):
    TRANSCRIPT = 1
    AUTOGENERATED = 2
    NO_CAPTION = 3


@dataclass
class PodcastChapter:
    video_id: str
    start_time: int
    end_time: int
    title: str
    _captions: List[Caption] = field(default_factory=list)
    _full_transcript: str = None

    @property
    def link(self):
        return f'{YOUTUBE_BASE}v={self.video_id}&t={self.start_time}'

    @property
    def full_transcript(self):
        if not self._full_transcript:
            self._full_transcript = " ".join([caption.text for caption in self._captions])
        return self._full_transcript

    def add_caption(self, caption: Caption):
        self._captions.append(caption)


@dataclass
class PodcastInfo:
    video_id: str
    episode_num: int
    title: str
    date: date
    uploader: str
    description: str
    tags: List[str]
    chapters: List[PodcastChapter]
    caption_type: CaptionType

    @property
    def link(self):
        return f'{YOUTUBE_BASE}v={self.video_id}'
