'use strict'

const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const { makeLogo: getLogo } = require('../../lib/logos')
const {
  documentation,
  checkErrorResponse: githubCheckErrorResponse,
} = require('./github-helpers')

// This legacy service should be rewritten to use e.g. BaseJsonService.
//
// Tips for rewriting:
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
//
// Do not base new services on this code.
module.exports = class GithubFollowers extends LegacyService {
  static get category() {
    return 'social'
  }

  static get route() {
    return {
      base: 'github/followers',
      pattern: ':user',
    }
  }

  static get examples() {
    return [
      {
        title: 'GitHub followers',
        namedParams: {
          user: 'espadrine',
        },
        staticPreview: {
          label: 'Follow',
          message: '150',
          style: 'social',
        },
        queryParams: { label: 'Follow' },
        documentation,
      },
    ]
  }

  static get defaultBadgeData() {
    return {
      namedLogo: 'github',
    }
  }

  static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
    camp.route(
      /^\/github\/followers\/([^/]+)\.(svg|png|gif|jpg|json)$/,
      cache((data, match, sendBadge, request) => {
        const user = match[1] // eg, qubyte
        const format = match[2]
        const apiUrl = `/users/${user}`
        const badgeData = getBadgeData('followers', data)
        if (badgeData.template === 'social') {
          badgeData.logo = getLogo('github', data)
        }
        githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
          if (githubCheckErrorResponse(badgeData, err, res, 'user not found')) {
            sendBadge(format, badgeData)
            return
          }
          try {
            badgeData.text[1] = JSON.parse(buffer).followers
            badgeData.colorscheme = undefined
            badgeData.colorB = '#4183C4'
            sendBadge(format, badgeData)
          } catch (e) {
            badgeData.text[1] = 'invalid'
            sendBadge(format, badgeData)
          }
        })
      })
    )
  }
}

@@ -17,179 +17,194 @@
 nst 
-LegacyService = require('../legacy-service')%0Aconst %7B makeBadgeData: getBadgeData %7D = require('../../lib/badge-data')%0Aconst %7B makeLogo: getLogo %7D = require('../../lib/logos
+Joi = require('joi')%0Aconst %7B metric %7D = require('../text-formatters')%0Aconst %7B nonNegativeInteger %7D = require('../validators')%0Aconst %7B GithubAuthService %7D = require('./github-auth-service
 ')%0Ac
@@ -209,18 +209,16 @@
 %0Aconst %7B
-%0A 
  documen
@@ -228,307 +228,134 @@
 ion,
-%0A  checkE
+ e
 rror
-Response: githubCheckErrorResponse,%0A%7D = require('./github-helpers')%0A%0A// This legacy service should be rewritten to use e.g. BaseJsonService.%0A//%0A// Tips for rewriting:%0A// https://github.com/badges/shields/blob/master/doc/rewriting-services.md%0A//%0A// Do not base new services on this code.
+MessagesFor %7D = require('./github-helpers')%0A%0Aconst schema = Joi.object(%7B%0A  followers: nonNegativeInteger,%0A%7D).required()%0A
 %0Amod
@@ -398,22 +398,26 @@
 extends 
-Legacy
+GithubAuth
 Service 
@@ -564,32 +564,146 @@
 er',%0A    %7D%0A  %7D%0A%0A
+  static render(%7B followers %7D) %7B%0A    return %7B%0A      message: metric(followers),%0A      color: '4183C4',%0A    %7D%0A  %7D%0A%0A
   static get exa
@@ -790,26 +790,16 @@
 arams: %7B
-%0A         
  user: '
@@ -808,25 +808,16 @@
 padrine'
-,%0A       
  %7D,%0A    
@@ -839,62 +839,56 @@
 ew: 
-%7B%0A          label: 'Follow',%0A          message
+Object.assign(this.render(%7B followers
 : 
-'
 150
-',
+ %7D), %7B
 %0A   
@@ -898,22 +898,22 @@
     
-style: 'social
+label: 'Follow
 ',%0A 
@@ -916,24 +916,25 @@
 ',%0A        %7D
+)
 ,%0A        qu
@@ -1051,24 +1051,50 @@
    return %7B%0A
+      label: 'followers',%0A
       namedL
@@ -1125,1073 +1125,240 @@
 %0A%0A  
-static registerLegacyRouteH
+async h
 andle
-r
 (%7B 
-camp, cache, githubApiProvider %7D) %7B%0A    camp.route(%0A      /%5E%5C/github%5C/followers%5C/(%5B%5E/%5D+)%5C.(svg%7Cpng%7Cgif%7Cjpg%7Cjson)$/,%0A      cache((data, match, sendBadge, request) =%3E %7B%0A        const user = match%5B1%5D // eg, qubyte%0A        const format = match%5B2%5D%0A        const apiUrl = %60/users/$%7Buser%7D%60%0A        const badgeData = getBadgeData('followers', data)%0A        if (badgeData.template === 'social') %7B%0A          badgeData.logo = getLogo('github', data)%0A        %7D%0A        githubApiProvider.request(request, apiUrl, %7B%7D, (err, res, buffer) =%3E %7B%0A          if (githubCheckE
+user %7D) %7B%0A    const %7B followers %7D = await this._requestJson(%7B%0A      url: %60/users/$%7Buser%7D%60,%0A      schema,%0A      errorMessages: e
 rror
-Response(badgeData, err, res, 'user not found')) %7B%0A            sendBadge(format, badgeData)%0A            return%0A          %7D%0A          try %7B%0A            badgeData.text%5B1%5D = JSON.parse(buffer).followers%0A            badgeData.colorscheme = undefined%0A            badgeData.colorB = '#4183C4'%0A            sendBadge(format, badgeData)%0A          %7D catch (e) %7B%0A            badgeData.text%5B1%5D = 'invalid'%0A            sendBadge(format, badgeData)%0A          %7D%0A        %7D)%0A      %7D)%0A    
+MessagesFor('user not found'),%0A    %7D)%0A    return this.constructor.render(%7B followers %7D
 )%0A  

refactor [GitHubFollowers] (#3157)