import {Component, Input} from '@angular/core';
import {ObjectsService} from "@app/_services";
import {DomSanitizer} from "@angular/platform-browser";

@Component({
  selector: 'video-section',
  templateUrl: 'video-section.component.html',
  styleUrls: ['video-section.component.scss']
})
export class VideoSectionComponent {
  @Input() section: any;

  constructor(
    private objectsService: ObjectsService,
    private sanitizer:  DomSanitizer
  ) {
  }

  public url:any = '';

  ngOnInit() {
    const url = this.parsedUrl(this.objectsService.getValue(this.section, 'video-url'));
    this.url = this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }

  parsedUrl(url: any) {
    let res = '';
    let videoId = '';
    if (/https:\/\/youtu.be\//.test(url)) {
      videoId = url.replace('https://youtu.be/','');
      res = 'https://www.youtube.com/embed/' + videoId;
    } else if (/https:\/\/www\.youtube\.com\/watch\?v\=/.test(url)) {
      videoId = url.replace('https://www.youtube.com/watch?v=','');
      res = 'https://www.youtube.com/embed/' + videoId;
    } else if (/https:\/\/rutube.ru\/video\//.test(url)) {
      videoId = url.replace('https://rutube.ru/video/','');
      res = 'https://rutube.ru/play/embed/' + videoId;
    } else {
      res = url;
    }
    return res;
  }
}
