const should = require('should'),
    ST = require('../api/parts/store.js'),
    common = require('../../../api/utils/common.js'),
    pluginManager = require('../../pluginManager.js'),
    db = pluginManager.singleDefaultConnection(),
    N = require('../api/parts/note.js'),
    C = require('../api/parts/credentials.js'),
    E = require('../api/parts/endpoints.js'),
    ProcessJob = require('../api/jobs/process.js'),
    ScheduleJob = require('../api/jobs/schedule.js'),
    momenttz = require('moment-timezone');

let app, credAPN, credFCM, USERS,
    noteCustom,
    returnOutput, returnMessage;

class ResourceMock {
    constructor(/*_id, name, args, db */) {
    }

    async send(msgs) {
        if (this.failImmediately) {
            throw new Error(this.failImmediately);
        }
        else if (this.failAt !== undefined) {
            return [
                msgs.slice(0, this.failAt).map(this.messageMapper),
                this.failAtError || new Error('failAt')
            ];
        }
        else if (this.messageMapper) {
            return [
                msgs.map(this.messageMapper)
            ];
        }
        else {
            return [
                msgs.map(m => [m._id, 200])
            ];
        }
    }
}

class ProcessJobMock extends ProcessJob {
    _run(db) {
        return new Promise(res => {
            this.run(db, res);
        });
    }
}

let jobFind = (name, data, Constr) => {
    return new Promise((res, rej) => {
        let q = {name: name, status: 0};
        if (data) {
            q.data = data;
        }
        db.collection('jobs').findOne(q, (err, job) => err ? rej(err) : res(job ? new Constr(job) : undefined));
    });
};

let collectionCount = (name) => {
    return new Promise((res, rej) => {
        db.collection(name).count((err, count) => err ? rej(err) : res(count));
    });
};

let collectionLoad = (name) => {
    return new Promise((res, rej) => {
        db.collection(name).find().toArray((err, records) => err ? rej(err) : res(records));
    });
};

describe('PUSH API', () => {
    describe('message note', async() => {
        it('should validate correctly', async() => {
            let [note, prepared, apps] = await E.validate({qstring: {args: noteCustom}});
            (note instanceof N.Note).should.equal(true);
            Array.isArray(apps).should.equal(true);
            should.not.exist(prepared);
        });

        it('should create & schedule note correctly without preparation', async() => {
            await E.create({qstring: {args: noteCustom}, res: {}, member: {global_admin: [app._id.toString()]}});
            let json = common.returnOutput;
            json.should.be.Object();
            should.exist(json._id);
            should.not.exist(json.error);
            json.result.status.should.equal(N.Status.Created);

            let note = await N.Note.load(db, json._id);
            should.exist(note);
            should.exist(note.date);
            should.exist(note.result);
            should.exist(note.build);
            should.exist(note.build.count);
            should.exist(note.build.count.es);
            note.test.should.be.false();
            note.apps.length.should.equal(noteCustom.apps.length);
            note.appNames.length.should.equal(noteCustom.apps.length);
            note.appNames[0].should.equal(app.name);
            note.platforms.length.should.equal(noteCustom.platforms.length);
            note.data.a.should.equal(noteCustom.data.a);
            note.result.status.should.equal(N.Status.Created);
            note.result.total.should.equal(0);
            note.build.total.should.equal(6);
            note.build.count.ru.should.equal(1);
            note.build.count.es.should.equal(1);
            noteCustom = json;

            let sg = new ST.StoreGroup(db),
                stores = await sg.stores(note),
                store = stores[0];

            stores.length.should.equal(1);

            let count = await collectionCount(store.collectionName);
            count.should.equal(0);
        });

        it('should store 4 test users in collections', async() => {
            let note = await N.Note.load(db, noteCustom._id),
                sg = new ST.StoreGroup(db),
                stores = await sg.stores(note),
                store = stores[0];
            stores.length.should.equal(1);

            let job = await jobFind('push:schedule', {mid: note._id}, ScheduleJob),
                hasBeenRun = false;
            should.exist(job);
            await job.prepare(null, db);

            await job.run(db, err => {
                hasBeenRun = err;
            });
            (hasBeenRun === undefined).should.be.true();

            note = await N.Note.load(db, note._id);
            note.result.status.should.equal(N.Status.READY);

            job = await jobFind('push:process', {cid: credAPN._id, aid: app._id, field: 'ip'}, ProcessJobMock);
            should.exist(job);

            let count = await collectionCount(store.collectionName);
            count.should.equal(note.build.total);

            let records = await collectionLoad(store.collectionName);
            console.log(records);
            records.filter(r => r.d === records[0].d).length.should.equal(6);
            records.filter(r => r.t === USERS.ru.tkip).length.should.equal(1);
            records.filter(r => r.t === USERS.es.tkip).length.should.equal(1);
        });

        it('should send notifications', async() => {
            let resource = new ResourceMock(),
                RUTOKEN = 'ru2';

            resource.messageMapper = msg => {
                should.exist(msg.m);

                let json = JSON.parse(msg.m);
                should.exist(json.aps.alert.body);
                should.exist(json.aps.alert.title);

                if (msg.t === USERS.ru.tkip) {
                    json.aps.alert.title.should.equal('Здравствуйте!');
                    json.aps.alert.body.should.equal('Привет, Артем! Пока Вы сделали 2 сессий, может еще одну?');
                    return [msg._id, -200, '', RUTOKEN];
                }
                else if (msg.t === USERS.lv.tkip) {
                    json.aps.alert.title.should.equal('Hello, Arturs!');
                    json.aps.alert.body.should.equal('Arturs! You have made a few sessions so far, how about another one? Countly LTD is waiting for you!');
                    return [msg._id, 200];
                }
                else if (msg.t === USERS.tk.tkip) {
                    json.aps.alert.title.should.equal('Hello, my friend!');
                    json.aps.alert.body.should.equal('My friend! You have made 3 sessions so far, how about another one? Your company is waiting for you!');
                    return [msg._id, 200];
                }
                else if (msg.t === USERS.gb.tkip) {
                    json.aps.alert.title.should.equal('Hello, my friend!');
                    json.aps.alert.body.should.equal('My friend! You have made 5 sessions so far, how about another one? Your company is waiting for you!');
                    return [msg._id, 200];
                }
                else if (msg.t === USERS.us.tkip) {
                    json.aps.alert.title.should.equal('Hello, my friend!');
                    json.aps.alert.body.should.equal('My friend! You have made a few sessions so far, how about another one? Countly LTD is waiting for you!');
                    return [msg._id, 200];
                }
                else if (msg.t === USERS.es.tkip) {
                    json.aps.alert.title.should.equal('Hello, my friend!');
                    json.aps.alert.body.should.equal('My friend! You have made a few sessions so far, how about another one? Your company is waiting for you!');
                    return [msg._id, 200];
                }
                else {
                    should.fail('wrong token');
                }
            };

            let job = await jobFind('push:process', {cid: credAPN._id, aid: app._id, field: 'ip'}, ProcessJobMock);
            console.log('Job %s is about to run at %s', job._id, new Date(job.next));
            await job.prepare(null, db);
            job.resource = resource;
            await job._run(db, () => {});

            let note = await N.Note.load(db, noteCustom._id),
                sg = new ST.StoreGroup(db),
                stores = await sg.stores(note),
                store = stores[0];
            stores.length.should.equal(1);

            let count = await collectionCount(store.collectionName);
            count.should.equal(0);
            note.result.processed.should.equal(6);
            note.result.sent.should.equal(6);
            note.result.errors.should.equal(0);
            note.result.status.should.equal(N.Status.DONE_SUCCESS);
        });
    });

    beforeEach(() => {
        returnOutput = common.returnOutput;
        returnMessage = common.returnMessage;

        common.returnOutput = (params, out) => {
            common.returnOutput = out;
            params.res.finished = true;
        };
        common.returnMessage = (params, code, message) => {
            common.returnMessage = [code, message];
            params.res.finished = true;
        };
    });

    afterEach(() => {
        common.returnOutput = returnOutput;
        common.returnMessage = returnMessage;
    });

    before((done) => {

        common.db = db;

        credAPN = new C.Credentials(new db.ObjectID());
        credAPN.platform = N.Platform.IOS;
        credAPN.type = C.CRED_TYPE[N.Platform.IOS].TOKEN;
        credAPN.key = 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR1RBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJIa3dkd0lCQVFRZ1N1V2xDdU1QR2JTRkpvWXE3bjQwdmh1d1lBc0dpZDAybDRUbWcxcHN1U09nQ2dZSUtvWkl6ajBEQVFlaFJBTkNBQVFqUm9YZDN3TEk4cE0wWStCbTRqVGFZMG11REpQd0IzekF4M3RYQ043SWFpS1lmTzJNSkZIZmI0cEhJMnZVTWI5a3dPa0VHckNObVc0UklvdGh5dnhQCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0=';
        credAPN.secret = '7GH992U9Z7[CLY]EQ43JUC8GV[CLY]ly.count.Countly-Tester';

        credFCM = new C.Credentials(new db.ObjectID());
        credFCM.platform = N.Platform.ANDROID;
        credFCM.type = C.CRED_TYPE[N.Platform.ANDROID].FCM;
        credFCM.key = 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR1RBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJIa3dkd0lCQVFRZ1N1V2xDdU1QR2JTRkpvWXE3bjQwdmh1d1lBc0dpZDAybDRUbWcxcHN1U09nQ2dZSUtvWkl6ajBEQVFlaFJBTkNBQVFqUm9YZDN3TEk4cE0wWStCbTRqVGFZMG11REpQd0IzekF4M3RYQ043SWFpS1lmTzJNSkZIZmI0cEhJMnZVTWI5a3dPa0VHckNObVc0UklvdGh5dnhQCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0=';

        app = {_id: db.ObjectID(), name: 'push test', timezone: 'Europe/Berlin', plugins: {push: {i: {_id: credAPN._id.toString(), type: 'apn_token'}, a: {_id: credFCM._id.toString(), type: 'fcm'}}}};

        // locales, timezones
        noteCustom = {
            // _id: db.ObjectID(),
            apps: [app._id.toString()],
            appNames: [],
            platforms: ['i'],
            data: {a: 'mess'},
            test: false,
            type: 'message',
            messagePerLocale: {
                'default': '! You have made  sessions so far, how about another one?  is waiting for you!',
                'default|p': {
                    0: {
                        f: 'My friend',
                        c: true,
                        k: 'name'
                    },
                    ['! You have made '.length]: {
                        f: 'a few',
                        c: false,
                        k: 'sc'
                    },
                    ['! You have made  sessions so far, how about another one? '.length]: {
                        f: 'Your company',
                        c: true,
                        k: 'custom.company'
                    }
                },
                'default|t': 'Hello, !',
                'default|tp': {
                    ['Hello, '.length]: {
                        f: 'my friend',
                        c: true,
                        k: 'name'
                    }
                },
                'ru': 'Привет, ! Пока Вы сделали  сессий, может еще одну?',
                'ru|p': {
                    ['Привет, '.length]: {
                        f: 'друг',
                        c: true,
                        k: 'name'
                    },
                    ['Привет, ! Пока Вы сделали '.length]: {
                        f: 'немного',
                        c: false,
                        k: 'sc'
                    }
                },
                'ru|t': 'Здравствуйте!',
            }
        };

        USERS = {
            // 'Здравствуйте!'
            // 'Привет, Артем! Пока Вы сделали 2 сессий, может еще одну?'
            'ru': {tkip: 'ios_ru', la: 'ru', tz: 180, sc: 2, name: 'Артем'},
            // 'Hello, Arturs!'
            // 'Arturs! You have made a few sessions so far, how about another one? Countly LTD is waiting for you!'
            'lv': {tkip: 'ios_lv', la: 'lv', tz: 120, name: 'Arturs', custom: {company: 'countly LTD'}},
            // 'Hello, my friend!'
            // 'My friend! You have made 3 sessions so far, how about another one? Your company is waiting for you!'
            'tk': {tkip: 'ios_tk', la: 'tk', tz: 180, sc: 3},
            // 'Hello, my friend!'
            // 'My friend! You have made 5 sessions so far, how about another one? Your company is waiting for you!'
            'gb': {tkip: 'ios_gb', la: 'gb', tz: 0, sc: 5},
            // 'Hello, my friend!'
            // 'My friend! You have made a few sessions so far, how about another one? Countly LTD is waiting for you!'
            'us': {tkip: 'ios_us', la: 'us', tz: -420, custom: {company: 'Countly LTD'}},
            // 'Hello, my friend!'
            // 'My friend! You have made a few sessions so far, how about another one? Your company is waiting for you!'
            'es': {tkip: 'ios_es', la: 'es', tz: 60},
        };

        db.collection('apps').insertOne(app, (err) => {
            err ? done(err) : db.collection('credentials').insertMany([credAPN, credFCM], err => {
                if (err) {
                    return done(err);
                }
                db.collection(`app_users${app._id}`).insertMany(Object.keys(USERS).map(uid => {
                    let u = {
                        uid: uid,
                        tk: {
                        },
                        la: USERS[uid].la
                    };

                    if (USERS[uid].tkip) {
                        u.tk.ip = USERS[uid].tkip;
                        u.tkip = true;
                    }
                    if (USERS[uid].tkid) {
                        u.tk.id = USERS[uid].tkid;
                        u.tkid = true;
                    }
                    if (USERS[uid].tkia) {
                        u.tk.ia = USERS[uid].tkia;
                        u.tkia = true;
                    }
                    if (USERS[uid].tkap) {
                        u.tk.ap = USERS[uid].tkap;
                        u.tkap = true;
                    }
                    if (USERS[uid].tkat) {
                        u.tk.at = USERS[uid].tkat;
                        u.tkat = true;
                    }
                    if (USERS[uid].sc) {
                        u.sc = USERS[uid].sc;
                    }
                    if (USERS[uid].name) {
                        u.name = USERS[uid].name;
                    }
                    if (USERS[uid].custom) {
                        u.custom = USERS[uid].custom;
                    }
                    if (USERS[uid].tz !== undefined) {
                        u.tz = USERS[uid].tz;
                    }
                    return u;
                }), done);
            });
        });
    });

    after(done => {
        Promise.all([
            noteCustom._id ? new ST.StoreGroup(db).clear(noteCustom) : Promise.resolve(),
            new Promise((res, rej) => {
                db.collection('messages').deleteMany({_id: {$in: [noteCustom].map(x => x && x._id).filter(x => !!x)}}, (err) => {
                    if (err) {
                        rej(err);
                    }
                    else {
                        db.collection('credentials').deleteMany({_id: {$in: [credAPN._id, credFCM._id]}}, err => {
                            if (err) {
                                rej(err);
                            }
                            else {
                                db.collection('apps').deleteOne({_id: app._id}, () => {
                                    db.collection(`app_users${app._id}`).drop(res);
                                });
                            }
                        });

                    }
                });
            })
        ]).then(() => {
            db.close();
            common.db && common.db.close();
            done();
        });
    });
});