summaryrefslogtreecommitdiff
path: root/lib/initialize.js
blob: 9552c41a87c5b8daf34692fa77d8827e542d382c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * mongo-edu
 *
 * Copyright (c) 2014-2016 Przemyslaw Pluta
 * Licensed under the MIT license.
 * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
 */

var mdbvideos = require('./login'),
    videoHandler = require('./videos'),
    prompts = require('./prompts'),
    pkg = require('../package'),
    emoji = require('./emoji'),
    colors = require('colors'),
    inquirer = require('inquirer');

var isWin = (/^win/.test(process.platform));

function checkName(profile) {

    'use strict';

    var greetings = 'Hi ';
    return ((profile.firstName !== '') ? greetings + profile.firstName : greetings + profile.userName) + ((!isWin) ? ' ' + emoji.smile + ' ' : '.');
}

module.exports = function run(profile, argv) {

    'use strict';

    var lookFor = ((!argv.h)? 'Videos' : 'Handouts'), videoPreference = 'youtube';

    inquirer.prompt(profile, function prompt(answers) {

        var list = prompts.list, classes = list, check = prompts.check, confirm = prompts.confirm;

        function init(answers) {

            mdbvideos.init(answers, argv, function get(err, data, profile) {

                if (err !== null) { return console.log('i'.red + ' ' + err.stack + '.\n'); }

                if (profile.preset.video[1] === videoPreference || argv.h) { return processList(data, profile); }

                confirm[0].message = 'Your current video preference is set to ' + profile.preset.video[1].underline + '. Switch to: ' + videoPreference.green;

                profileAssesment(confirm, data, profile);

            });
        }

        function profileAssesment(confirm, data, profile) {
            inquirer.prompt(confirm, function prompt(answers) {
                if (!answers.confirm) { return console.log('i'.red + ' ' + pkg.name + ' requires video preferences set to ' + videoPreference + '.\n'); }

                mdbvideos.updateProfile({
                    provider: videoPreference,
                    language: profile.preset.language[1]
                }, function status(err, resp) {
                    if (err !== null) { throw err; }
                    if (resp.success) { processList(data, profile); }
                });

            });
        }

        function getClassContent(content) {
            mdbvideos.getList(content, argv, function get(err, data, pass) {
                if (err !== null) { throw err; }

                if (data.length) {

                    if (pass) { return showDetails(err, data); }

                    list[0].message = 'Found ' + data.length + ' List' + ((data.length > 1)? 's' : '') + '. Select:';
                    list[0].choices = data;

                    return currentVideos();

                } else {
                    if (pass) { return console.log('i'.red + ' Looks like the course is not yet available or has already ended. ' +
                        lookFor + ' list is not available.\n\nCheck the start/end date for selected course.\n'); }
                }

                console.log('i'.red + ' Unable to locate any ' + lookFor.toLowerCase() + ' lists in the wiki. Are ' + lookFor.toLowerCase() + ' list present?');

                if (lookFor === 'Videos' && !argv.cw) {

                    confirm[0].message = 'Default wiki search returned no resuts. Perform courseware search with ' + '--cw'.green + ' ?';

                    inquirer.prompt(confirm, function prompt(answers) {
                        if (!answers.confirm) { return; }
                        argv.cw = true;
                        content.url = content.url.replace(/course_wiki/g, '') + 'courseware';
                        getClassContent(content);
                    });

                }

            });
        }

        function currentList() {
            inquirer.prompt(classes, function prompt(answers) {
                getClassContent(answers);
            });
        }

        function processList(data, profile) {

            if (!data.length) {
                if (profile.noCourses) { return console.log('i'.red + ' ' + checkName(profile) + ' ' + profile.noCourses); }
                return console.log('i'.red + ' ' + checkName(profile) + ' ' + 'Could not locate any courses in your profile. Have you registered already?\n');
            }

            classes[0].message = checkName(profile) + ' Found ' + data.length + ' Course'+ ((data.length > 1)? 's' : '') + '. Select:';
            classes[0].choices = data;
            currentList();

        }

        function currentVideos() {

            inquirer.prompt(list, function prompt(answers) {

                mdbvideos.listVideos(answers, argv, function get(err, data, pass) {
                    if (err !== null) { throw err; }
                    if (!pass) { return videoHandler.details(data, argv, showDetails); }
                    showDetails(err, data);
                });

            });
        }

        function showDetails(err, data) {
            if (err !== null) { throw err; }

            if (data.length) {
                check[0].message = 'Select From ' + (data.length - 2) + ' ' + lookFor + '. Download:';
                check[0].choices = data;

                return inquirer.prompt(check, function prompt(answers) {
                    videoHandler.download(answers, data, argv);
                });

            }

            console.log('i'.red + ' Could not locate any ' + lookFor.toLowerCase() + '.'); process.exit(0);
        }

        init(answers);

    });

};