const fs = require('fs');
const JSZip = require('jszip');

var schema;
var pages = [];
var errors = {};
var instances = {};
var output = [];

function parseSketch(schemaJson, result) {
  schema = schemaJson;

  let pages = result.pages;
  for (var id in pages) {
    let page = pages[id];

    if (page.name == 'Symbols') {
      continue;
    }

    errors[page.name] = [];

    checkPage(page.name, page.layers);


    const outputJson = JSON.stringify(output, null, 2);

    fs.writeFile("output.json", outputJson, (err) => {
      if (err) {
        return console.log(err);
      }
    });

    //reportErrors(page.name);
  }

  return result;
}

function checkPage(pageName, artboards) {
  for (var artboard of artboards) {

    var stack = {
      page: pageName,
      layerPath: []
    }

    validateSketchObject(artboard, schema.hierarchy, stack);
  }
}

/*
 * Takes an object and compares it to an array of possible schemas.
 * When it finds a match, it continues to traverse down the tree.
 * If it doesn't find a match, an error is recorded.
 */
function validateSketchObject(obj, schemas, stack) {
  if (obj.name == 'meta') {
    return;
  }

  // Copy stack for local use
  let localStack = JSON.parse(JSON.stringify(stack));

  for (let schema of schemas) {

    // If it passes this check, the obj has the right class
    if (schema.class && obj['_class'] != schema.class) {
      continue;
    }

    // If it passes this check, the obj name matches the pattern
    if (schema.pattern) {
      let regex = new RegExp(schema.pattern, "g");
      if (!regex.test(obj.name)) {
        continue;
      }
    }

    //how to handle count?

    if (schema.output) {
      let objOutput = JSON.parse(JSON.stringify(localStack));

      objOutput['name'] = obj.name;
      objOutput['properties'] = [];
      objOutput['properties'] = parseProperties(obj, schema.output);
      console.log(objOutput);
      output.push(objOutput);
    }

    // Add the object to the stack
    if (obj['_class'] == 'artboard' && !localStack.artboard) {
      localStack['artboard'] = obj.name;
    } else {
      localStack['layerPath'].push(obj.name);
    }

    if (obj.layers && schema.layers) {
      for (let layer of obj.layers) {
        validateSketchObject(layer, schema.layers, localStack);
      }
    }
  }
}

function parseProperties(obj, properties) {
  let objOutput = {};
  for (let property of properties) {
    if (property == 'fills.color') {
      let color = obj.style.fills[0].color;
      delete color['_class'];
      objOutput[property] = obj.style.fills[0].color;
    }
  }
  return objOutput;
}

module.exports = parseSketch;

@@ -1236,16 +1236,257 @@
 ack));%0A%0A
+  // If none of the schema's have a %60pattern%60 key, don't check names%0A  let checkName = schemas.some((o) =%3E %7B%0A    return 'pattern' in o;%0A  %7D);%0A  let nameValidated = !checkName;%0A%0A  // TODO: Add support for schema.pattern to be string OR list%0A%0A
   for (l
@@ -1505,24 +1505,24 @@
  schemas) %7B%0A
-
 %0A    // If i
@@ -1719,24 +1719,37 @@
 ern%0A    if (
+checkName && 
 schema.patte
@@ -1815,17 +1815,16 @@
     if (
-!
 regex.te
@@ -1835,24 +1835,69 @@
 bj.name)) %7B%0A
+        nameValidated = true;%0A      %7D else %7B%0A
         cont
@@ -2178,38 +2178,8 @@
 t);%0A
-      console.log(objOutput);%0A
     
@@ -2561,24 +2561,24 @@
 k);%0A      %7D%0A
-
     %7D%0A  %7D%0A%7D%0A
@@ -2570,24 +2570,350 @@
  %7D%0A    %7D%0A  %7D
+%0A%0A  if (!nameValidated) %7B%0A    let patterns = %5B%5D;%0A    for (let schema of schemas) %7B%0A      patterns.push(schema.pattern);%0A    %7D%0A    console.log('Incorrect ' + obj%5B'_class'%5D + ' name: %22' + obj.name +%0A        '%22 in artboard: %22' + stack.artboard + '%22');%0A    console.log('Expected format(s): %22' + patterns.join('%22, %22') + '%22%5Cn');%0A  %7D
 %0A%7D%0A%0Afunction

Add name validation back in