#!/usr/bin/env node

var https = require('https');
var fs = require('fs');
var moment = require('moment');
var request = require('request');
var path = require('path');

console.log("*** starting");

var now = moment();

var data = fs.readFileSync(path.resolve(__dirname, '../baggers.js')).toString();

// Remove the "var data = " part and
script = data.replace("var data = ", "").replace(/;([^;]*)$/, '');
bblfrData = JSON.parse(script);

// Store cities as a map so we will be able to replace cities by coordinates
var cities = {};
bblfrData.cities.forEach(function(obj) {
    cities[obj.name] = obj;
} );

var webSiteExists = function(name, url) {
    request(url, {method: 'HEAD', timeout: 10000}, function (err, res, body){
        if (res !== undefined) {
            if (res.statusCode == 404 || res.statusCode >= 500) {
                console.log(name, 'Res incorrect URL', url, 'got', res.statusCode);
            }
        } else {
            console.log(name, 'Err incorrect URL', url, 'got', err);
        }
    });
};

// Read speakers
var websiteTypes = {};
var speakers = [];
bblfrData.baggers.forEach(function(obj) {
    var sessions = [];

    obj.sessions.forEach(function(session) {
        sessions.push({
            "title": session.title,
            "abstract": session.summary,
            "tags": obj.tags
        });
    });

    obj.sessions = sessions;
    delete obj.tags;

    // Generate the new contact
    var contact;
    if (obj.contact === undefined) {
        contact = {
            type: 'twitter',
            url: obj.twitter
        }
    } else {
        contact = {
            url: obj.mail
        }
    }

    if (obj.since === undefined || obj.since === '') {
        // For audit purpose
        console.log('no date for', obj.name);
    }

    delete obj.mail;
    delete obj.contact;

    obj.contact = contact;

    // Rewrite names
    obj.name = obj.name.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});

    // Rewrite website links
    var websites = [];
    if (obj.websites === undefined) {
        // For audit purpose
        console.log('no website for', obj.name);
    } else {
        obj.websites.forEach(function(website) {
            // webSiteExists(obj.name, website.href);

            var name = website.title.toLowerCase();
            switch (name) {
                case 'linkedin':
                case 'linked in':
                case 'profil linkedin':
                    name = 'LinkedIn';
                    break;
                case 'github':
                    name = 'GitHub';
                    break;
                case 'podcast':
                    name = 'Podcast';
                    break;
                case 'google+':
                    name = 'Google+';
                    break;
                case 'slideshare':
                    name = 'Slideshare';
                    break;
                case 'hopwork':
                    name = 'Hopwork';
                    break;
                case 'home page':
                case 'silicium':
                case 'ippon':
                case 'code distillers':
                case 'loxodata':
                case 'use case driven':
                case 'one.hundred.acorns':
                case 'n-fluent.net':
                case 'pragmafabrik':
                case 'homepage':
                case 'iergo':
                case 'easymock':
                case 'objenesis':
                case 'faveod':
                case 'arolla':
                case 'agileit':
                case 'websbite':
                case 'codeurs en seine':
                case 'lean kanban france':
                case 'lesfurets.com':
                case 'duchess france':
                case 'affini-tech':
                case 'random code':
                case 'about me':
                case 'ux-fr':
                case 'carbon it':
                case 'agilexperience':
                case 'human coders':
                case 'human coders formations':
                case 'pragmatic entrepreneurs':
                case 'conseil en business model':
                case 'bloc-notes sur l\'expérience utilisateur':
                case 'mes projets':
                case 'tous sur java, c++, la sécurité et la poo':
                    name = 'Web';
                    break;
                case 'les bouquins que j\'ai écrit':
                    name = 'Book';
                    break;
                case 'infoq':
                    name = 'InfoQ';
                    break;
                case 'viadeo':
                    name = 'Viadeo';
                    break;
                default:
                    if (name.toLowerCase().indexOf('blog') > -1) {
                        name = 'Web';
                    } else if (name.toLowerCase().indexOf('site') > -1) {
                        name = 'Web';
                    } else if (website.href.toLowerCase().indexOf('linkedin') > -1) {
                        name = 'LinkedIn';
                    } else if (website.href.toLowerCase().indexOf('about.me') > -1) {
                        name = 'AboutMe';
                    } else {
                        name = 'Web';
                    }
            }

            // Audit HTTP link


            websites.push({
                "name": name,
                "url": website.href
            });

            obj.websites = websites;

            // For audit purpose
            if (websiteTypes[name] !== undefined) {
                var nb = websiteTypes[name].nb;
                nb++;
                websiteTypes[name].nb = nb;
                websiteTypes[name].list.push(website.href);
            } else {
                websiteTypes[name] =  {
                    nb: 1,
                    list: [ website.href ]
                };
            }

        });
    }

    speakers.push(obj);

//        console.log('speaker', obj.name, JSON.stringify(obj, null, 2));
});

// For audit purpose
//console.log(websiteTypes);
//console.log('different website types', Object.keys(websiteTypes).length);

// Sort speakers
speakers = speakers.sort(function(a, b){
    return a.since == b.since ? 0 : +(a.since > b.since) || -1;
});

// Sort cities
const orderedCities = {};
Object.keys(cities).sort().forEach(function(key) {
    orderedCities[key] = cities[key];
});

// Create the new final object
var newData = {
    speakers: speakers,
    cities: orderedCities
};


var finalString = 'var data = ' + JSON.stringify(newData, null, 2) + ";";

fs.writeFileSync(path.resolve(__dirname, 'baggers-new.js'), finalString);

console.log('file generated');


@@ -2376,181 +2376,8 @@
 ) %7B%0A
-                case 'linkedin':%0A                case 'linked in':%0A                case 'profil linkedin':%0A                    name = 'LinkedIn';%0A                    break;%0A
     
@@ -2403,16 +2403,16 @@
 ithub':%0A
+
         
@@ -2865,1372 +2865,8 @@
 ak;%0A
-                case 'home page':%0A                case 'silicium':%0A                case 'ippon':%0A                case 'code distillers':%0A                case 'loxodata':%0A                case 'use case driven':%0A                case 'one.hundred.acorns':%0A                case 'n-fluent.net':%0A                case 'pragmafabrik':%0A                case 'homepage':%0A                case 'iergo':%0A                case 'easymock':%0A                case 'objenesis':%0A                case 'faveod':%0A                case 'arolla':%0A                case 'agileit':%0A                case 'websbite':%0A                case 'codeurs en seine':%0A                case 'lean kanban france':%0A                case 'lesfurets.com':%0A                case 'duchess france':%0A                case 'affini-tech':%0A                case 'random code':%0A                case 'about me':%0A                case 'ux-fr':%0A                case 'carbon it':%0A                case 'agilexperience':%0A                case 'human coders':%0A                case 'human coders formations':%0A                case 'pragmatic entrepreneurs':%0A                case 'conseil en business model':%0A                case 'bloc-notes sur l%5C'exp%C3%A9rience utilisateur':%0A                case 'mes projets':%0A                case 'tous sur java, c++, la s%C3%A9curit%C3%A9 et la poo':%0A                    name = 'Web';%0A                    break;%0A
     
@@ -3479,18 +3479,16 @@
 ('linked
-in
 ') %3E -1)
@@ -4517,16 +4517,17 @@
 rpose%0A//
+ 
 console.
@@ -4545,18 +4545,16 @@
 Types);%0A
-//
 console.

Simplify code for website links