const SekshiModule = require('../Module')
const debug = require('debug')('sekshi:youtube-available')
const request = require('request')

export default class YouTubeAvailable extends SekshiModule {

  constructor(sekshi, options) {
    super(sekshi, options)

    this.author = 'ReAnna'
    this.version = '0.0.1'
    this.description = 'Notifies staff if a video might not be available.'

    this.onAdvance = this.onAdvance.bind(this)
  }

  defaultOptions() {
    return {
      key: false,
      api: 'https://www.googleapis.com/youtube/v3/videos'
    }
  }

  init() {
    if (!this.options.key) {
      this.sekshi.sendChat(`@staff The YouTube Availability module needs a YouTube API key. Please set it using '!set youtubeavailable key API_KEY_HERE'.`)
    }

    this.sekshi.on(this.sekshi.ADVANCE, this.onAdvance)
  }
  destroy() {
    this.sekshi.removeListener(this.sekshi.ADVANCE, this.onAdvance)
  }

  onAdvance() {
    const media = this.sekshi.getCurrentMedia()
    if (media && media.format === 1 && this.options.key) {
      let url = `${this.options.api}?id=${media.cid}&part=contentDetails&key=${this.options.key}`
      request(url, (e, _, body) => {
        if (e) return debug('yt-api-err', e)
        try {
          let json = JSON.parse(body)
          let restriction = json.items[0].contentDetails.regionRestriction
          if (!restriction || restriction.length === 0) return
          if (restriction.blocked) {
            this.sekshi.sendChat(`@ReAnna This video is unavailable in ${restriction.blocked.length} countries.`)
          }
          if (restriction.allowed) {
            this.sekshi.sendChat(`@ReAnna This video is only available in ${restriction.allowed.length} countries.`)
          }
        }
        catch (e) {}
      })
    }
  }

}
@@ -293,51 +293,62 @@
 his.
-version = '0.0.1'%0A    this.description = 'N
+description = 'Autoskips videos that are blocked and n
 otif
@@ -390,16 +390,27 @@
 vailable
+ everywhere
 .'%0A%0A    
@@ -566,16 +566,136 @@
 /videos'
+,%0A%0A      skipTerminated: true,%0A      notifyRegionBlocked: false,%0A      allowedThreshold: 50,%0A      blockedThreshold: 100
 %0A    %7D%0A 
@@ -765,16 +765,25 @@
 endChat(
+%0A        
 %60@staff 
@@ -839,16 +839,27 @@
 PI key. 
+%60%0A        %60
 Please s
@@ -1132,16 +1132,110 @@
 Media()%0A
+    const modSkip = this.sekshi.getModule('modskip')%0A    const sekshi = this.sekshi.getSelf()%0A
     if (
@@ -1357,16 +1357,23 @@
 tDetails
+,status
 &key=$%7Bt
@@ -1524,16 +1524,513 @@
 e(body)%0A
+          if (this.options.skipTerminated) %7B%0A            if (json.items.length === 0) %7B%0A              modSkip.lockskip(sekshi, 'unavailable')%0A              return%0A            %7D%0A%0A            let status = json.items%5B0%5D.status%0A            if (status.uploadStatus === 'rejected' %7C%7C status.embeddable === false) %7B%0A              modSkip.lockskip(sekshi, 'unavailable')%0A              return%0A            %7D%0A          %7D%0A          debug('not terminated')%0A%0A          if (this.options.notifyRegionBlocked) %7B%0A  
         
@@ -2102,24 +2102,26 @@
 n%0A          
+  
 if (!restric
@@ -2128,78 +2128,263 @@
 tion
- %7C%7C restriction.length === 0) return%0A          if (restric
+) return%0A%0A            let %7B blocked, allowed %7D = restriction%0A            debug('blocked in', blocked ? blocked.length : null)%0A            debug('allowed in', allowed ? allowed.length : null)%0A            if (blocked && blocked.length %3E this.op
 tion
+s
 .blocked
 ) %7B%0A
@@ -2375,28 +2375,39 @@
 ions.blocked
+Threshold
 ) %7B%0A
+  
             
@@ -2423,32 +2423,48 @@
 hi.sendChat(
-%60@ReAnna
+%0A                %60@staff
  This video 
@@ -2515,66 +2515,174 @@
 th%7D 
-countries.%60)%0A          %7D%0A          if (restric
+%60 +%0A                %60countries. It may have to be skipped.%60)%0A              return%0A            %7D%0A            else if (allowed && allowed.length %3C= this.op
 tion
+s
 .allowed
 ) %7B%0A
@@ -2677,20 +2677,31 @@
 .allowed
+Threshold
 ) %7B%0A
+  
         
@@ -2729,16 +2729,32 @@
 hat(
-%60@ReAnna
+%0A                %60@staff
  Thi
@@ -2816,32 +2816,153 @@
 th%7D 
-countries.%60)%0A          %7D
+%60 +%0A                %60countries. It may have to be skipped.%60)%0A              return%0A            %7D%0A          %7D%0A          debug('not region blocked')
 %0A   

add autoskip for nonexistent videos (thanks timberOwl!)