var datetime = require('node-datetime');

module.exports = {
    insertTodayDomains : function (db, domains){
        return new Promise((resolve,reject)=>{
            db.collection('today').insert(domains, ()=>{
                resolve();
            });
        });
    },

    getYesterdayDomains : function (db){
        return getArrayFromDatabase(db, 'yesterday');
    },

    getExpiredDomains : function (db){
        return getArrayFromDatabase(db, 'expired_list');
    },

    insertExpired : function (db, domains){
        return new Promise((resolve, reject)=>{
            let domainsForInsertion = [];
            let waitingPromises = [];
    
            domains.map(domain=>{
                
                waitingPromises.push(checkDate(db,domain).then(res=>{
                    if (res!==null){
                        domainsForInsertion.push(res);
                    }
                }));
                
            });
    
            Promise.all(waitingPromises).then(()=>{
                if (domainsForInsertion.length>0){
                    db.collection('expired_list').insert(domainsForInsertion, (err,res)=>{
                        resolve();
                    });
                }else{
                    resolve();
                }
            });
        });
    },
};

const getArrayFromDatabase = function(db, collection){
    return new Promise((resolve,reject)=>{
        db.collection(collection).find({}).toArray((err,result)=>{
            if (err){
                reject('Error reading collection');
            }else{
                resolve(result);
            }
        });
    });
}

const checkDate = function(db, domain){
    return new Promise((resolve,reject)=>{ 
        db.collection('today').findOne({domainName: domain.domainName}, (err,result)=>{
            if ((!err) && (result===null) &&(datetime.create().format('Y-m-d')===domain.expirationDate)){
                //domainsForInsertion.push(domain);
                resolve(domain);
            }else{
                resolve(null);
            }
        });
    });
}