#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo "Title of the kata (human readable):"
read title
echo ''

echo "Level of the kata (1-8):"
read level
echo ''
level_color='888888'
case "$level" in
    ("1")    level_color='866cc7';;
    ("2")    level_color='51417c';;
    ("3")    level_color='3c7ebb';;
    ("4")    level_color='314f6b';;
    ("5")    level_color='ac760d';;
    ("6")    level_color='705521';;
    ("7")    level_color='888888';;
    ("8")    level_color='444444';;
esac

echo "Folder name (no spaces, please):"
read folder
echo ''

echo "Url of kata:"
read url
echo ''

mkdir $DIR/katas/$folder

echo '#!/bin/bash' > $DIR/katas/$folder/test
echo 'DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"' >> $DIR/katas/$folder/test
echo '' >> $DIR/katas/$folder/test
echo '$DIR/../../test $DIR' >> $DIR/katas/$folder/test
chmod a+x $DIR/katas/$folder/test

echo '#!/bin/bash' > $DIR/katas/$folder/work
echo 'DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"' >> $DIR/katas/$folder/work
echo '' >> $DIR/katas/$folder/work
echo 'vi -o '$DIR/katas/$folder'/solution.js '$DIR/katas/$folder'/tests.js' >> $DIR/katas/$folder/work
chmod a+x $DIR/katas/$folder/work

echo 'const multiply = (a, b) => {' > $DIR/katas/$folder/solution.js
echo '    return a * b;' >> $DIR/katas/$folder/solution.js
echo '}' >> $DIR/katas/$folder/solution.js

echo 'const chai = require("chai");' > $DIR/katas/$folder/tests.js
echo 'const assert = chai.assert;' >> $DIR/katas/$folder/tests.js
echo '' >> $DIR/katas/$folder/tests.js
echo 'const tests = [' >> $DIR/katas/$folder/tests.js
echo '    [ 0,   1,    0],' >> $DIR/katas/$folder/tests.js
echo '    [16,   2,   32],' >> $DIR/katas/$folder/tests.js
echo '    [70,  -1,  -70],' >> $DIR/katas/$folder/tests.js
echo '    [26, -26, -676],' >> $DIR/katas/$folder/tests.js
echo '];' >> $DIR/katas/$folder/tests.js
echo '' >> $DIR/katas/$folder/tests.js
echo 'describe("Tests", function() {' >> $DIR/katas/$folder/tests.js
echo '' >> $DIR/katas/$folder/tests.js
echo '    for (const [a, b, expected] of tests) {' >> $DIR/katas/$folder/tests.js
echo '        it(`input: ${a}, ${b}`, function() {' >> $DIR/katas/$folder/tests.js
echo '            assert.strictEqual(multiply(a, b), expected);' >> $DIR/katas/$folder/tests.js
echo '        });' >> $DIR/katas/$folder/tests.js
echo '    }' >> $DIR/katas/$folder/tests.js
echo '' >> $DIR/katas/$folder/tests.js
echo '});' >> $DIR/katas/$folder/tests.js

echo '- ![](https://img.shields.io/badge/level-'$level'kyu-'$level_color') '$title' | [Description]('$url') | [Solution](katas/'$folder'/solution.js)' >> $DIR/README.md
