const walk = require('walk')
const fs = require('fs')
const path = require('path')
const htmlparser = require('htmlparser')
const cheerio = require('cheerio')
const sassLint = require('sass-lint')
const Reporter = require('./reporter.js')

class Linter {
  constructor (options) {
    this.lintErrors = []
  }

  checkPaths (pathsToCheck) {
    pathsToCheck.forEach((pathToCheck) => {
      this.checkPath(pathToCheck)
    })
  }

  checkPath (arg) {
    const walker = walk.walk(arg, { followLinks: false })
    walker.on('file', this.walkerFileHandler.bind(this))
    walker.on('end', this.walkerEndHandler.bind(this))
  }

  walkerFileHandler (root, fileStat, next) {
    const filename = `${root}/${fileStat.name}`

    if (filename.substr(-3) !== 'vue') {
      return next()
    }

    fs.readFile(path.resolve(root, fileStat.name), (error, fileData) => {
      if (error) {
        return console.log(error)
      }

      const fileTemplates = this.extractFileTemplates(fileData)

      fileTemplates.forEach((template) => {
        const fileErrors = sassLint.lintText({
          text: template,
          filename: filename,
          format: 'scss'
        })

        if (fileErrors.messages.length) {
          this.lintErrors = this.lintErrors.concat(fileErrors)
        }
      })

      next()
    })
  }

  walkerEndHandler () {
    const reporter = new Reporter()
    reporter.report(this.lintErrors)
  }

  extractFileTemplates (fileData) {
    let templates = []

    const handler = new htmlparser.DefaultHandler((error, dom) => {
      if (error) {
        return console.log(error)
      }

      const $ = cheerio.load(dom)
      templates = templates.concat($('style[lang="sass"]').text())
      templates = templates.concat($('style[lang="scss"]').text())
    })

    var parser = new htmlparser.Parser(handler)
    parser.parseComplete(fileData)
    return templates
  }
}

module.exports = Linter

@@ -1097,16 +1097,24 @@
 template
+.content
 ,%0A      
@@ -1212,24 +1212,138 @@
 s.length) %7B%0A
+          fileErrors.messages.forEach((message) =%3E %7B%0A            message.line += template.lineOffset%0A          %7D)%0A
           th
@@ -1766,32 +1766,38 @@
 (dom)%0A      
+const 
 template
 s = template
@@ -1784,37 +1784,19 @@
 template
-s
  = 
-templates.concat(
 $('style
@@ -1803,17 +1803,17 @@
 %5Blang=%22s
-a
+c
 ss%22%5D').t
@@ -1821,24 +1821,28 @@
 xt()
-)
+%0A
 %0A      
+if (
 template
 s = 
@@ -1841,172 +1841,498 @@
 late
-s = templates.concat($('style%5Blang=%22scss%22%5D').text())%0A    %7D)%0A%0A    var parser = new htmlparser.Parser(handler)%0A    parser.parseComplete(fileData)%0A    return templates
+.length %3C= 0) %7B%0A        return%0A      %7D%0A%0A      templates.push(%7B%0A        content: template,%0A        lineOffset: this.getLineOffset(template, $.text())%0A      %7D)%0A    %7D)%0A%0A    var parser = new htmlparser.Parser(handler)%0A    parser.parseComplete(fileData)%0A    return templates%0A  %7D%0A%0A  getLineOffset (needle, haystack) %7B%0A    const targetPosition = haystack.indexOf(needle)%0A    const untilTargetPosition = haystack.substring(0, targetPosition)%0A    return untilTargetPosition.split(/%5Cr?%5Cn/).length - 1
 %0A  %7D

add correct line offset