const { writeFileSync, mkdirSync } = require('fs');
const { get } = require('request');

const PROBLEM_URL = slug => `https://leetcode.com/problems/${slug}/`;

const formatSeq = seq => {
  while (seq.length < 3) seq = '0' + seq;
  return seq;
};

const createFiles = (slug, code) => {
  get(PROBLEM_URL(slug)).on('response', res => {
    res.setEncoding('utf8');
    let chunk = '';
    res.on('data', data => chunk += data);
    res.on('end', () => {
      const sequence = /\d+/.exec(/questionId\:\ \'\d+\'/.exec(chunk)[0])[0];

      const submissionName = `${formatSeq(sequence)}_${slug}`;
      mkdirSync(`./problems/${submissionName}`);
      createIndexFile(submissionName, slug, code);
      createTestFile(submissionName);

      console.log(`problems/${submissionName}/index.js & problems/${submissionName}/test-cases.js are successfully created!`);
    });
  });
};

const createIndexFile = (submissionName, slug, code) => {
  // const re = /var\ .*\ =\ function/;
  // const funcName = re.exec(code)[0].split(' ')[1];
  writeFileSync(
    `./problems/${submissionName}/index.js`, `/**
 * Problem: https://leetcode.com/problems/${slug}/description/
 */
` + code + `

module.exports = ;
`
  );
};

const createTestFile = submissionName => {
  writeFileSync(
    `./problems/${submissionName}/test-cases.js`,
    `module.exports = [];`
  );
};

const clearConsole = () => process.stdout.write(
  'win32' === process.platform ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H'
);

const traverseNode = node => {
  let texts = [];
  const dfs = node => {
    if ('text' === node.type) {
      texts.push(node.data);
    } else {
      node.children.forEach(dfs);
    }
  };
  dfs(node);
  return texts;
};

const unicodeToChar = text => text.replace(/\\u[\dA-F]{4}/gi, match => {
  return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
});

module.exports = {
  createFiles,
  clearConsole,
  traverseNode,
  unicodeToChar
};

@@ -1023,16 +1023,165 @@
  ')%5B1%5D;%0A
+  const funcName = slug.split('-').map((str, i) =%3E (%0A    i ? Array.from(str).map((ch, j) =%3E j ? ch : ch.toUpperCase()).join('') : str%0A  )).join('');%0A
   writeF
@@ -1318,16 +1318,48 @@
 code + %60
+%0Aconst $%7BfuncName%7D = () =%3E %7B%0A%0A%7D;
 %0A%0Amodule
@@ -1369,16 +1369,27 @@
 ports = 
+$%7BfuncName%7D
 ;%0A%60%0A  );

update the file utils