//const page=CONFIG("logopage");
const dbase=CONFIG("dbase");
const conURL=CONFIG("conURL");

exports.install = function($) {
	ON('request_begin', visit);

}
async function visit($) {
    var self = this;
	//const mongo=MODULE('https_modules/HmongoDB');
	//const valid=mongo.ISvalidate;
	//var result={status:200,data:""};
// 	console.log("---------------------------eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee------------------------------------");
	
//var s=await nosINS2('ddd',{url:$.url,uri:$.headers['x-forwarded-host'],protocol:$.uri.protocol,ip:$.ip,mobile:$.mobile,ua:$.headers['user-agent']});
//console.log(s);
//console.log("s");
// let d=new Date()+"";
// let p= d.indexOf("GMT");
// var storage = NOSQL('Hvisitor/dailyvisit').storage;
//var sss=storage.insert({url:$.url,uri:$.headers['x-forwarded-host'],protocol:$.uri.protocol,ip:$.ip,time:d.slice(p-9,p-1),mobile:$.mobile,ua:$.headers['user-agent']});
// console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//console.log(sss);
var db=NOSQL('ddd');
// db.counter.count(['/login/'],function(err, response) {
//     console.log(response);
//     /*
//         { '16083014300001idn1': 
//           [ { id: '20160801', year: 2016, month: 8, day: 1, value: 19 },
//              { id: '20160802', year: 2016, month: 9, day: 2, value: 29 } ],
//           '16090123150001clu1': [ { id: '20160901', year: 2016, month: 9, day: 1  value: 36 } ] }
//       */
// });

// console.log("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
var q={"url":"/forgetaaV","uri":"www.https-co.com","protocol":"6699"};
var w={"url":"/forget/hh","uri":"www.https-co.com","protocol":"ss","ip":"136.243.228.177","mobile":false,"ua":"Mozilla/5.0 (compatible; DataForSeoBot/1.0; +https://dataforseo.com/dataforseo-bot)"};

// db.update(q,true).make(function(builder) {
//     // builder.first(); --> modifies only the one document
//         console.log(builder);
//     builder.where("url","/forgetaa");
//         console.log(builder);
//     builder.callback(function(err,response, count) {
//         console.log('modified documents:', response);
//         console.log('modified documents:', count);
//     });
// });
// db.update(q, true).where("url","/forgetaaV").callback(function(err, response) {
				
//     console.log("aaaaaaaaaaaaaaaaaaaaaaa");
//     console.log("err");
//     console.log(err);
//     console.log("response");
//     console.log(response);
// //   // console.log(db);
// //     console.log("-------------------------------");
//   });


//var m=await db.find2().where("url","/forgetaaV");

//console.log(m);

db.find2().where("url","/forgetaaV22").callback(function(err, response) {
				
//     console.log("aaaaaaaaaaaaaaaaaaaaaaa");
//     console.log("err");
//     console.log(err);
    console.log("response");
    console.log(response);
  // console.log(db);
  if(response.length>0){
      
  }else{
    console.log("-------------------------------");
    db.insert(q).callback(function(err, response) {
            console.log("responseeee");

 });  }

 });

    //  console.log("hhhhhhhhhhhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxhhhhhhhhhhhh");
     /////////// ok
//   db.modify({protocol:"azazafffzaz"}).where("url","/xpx").callback(function(err, response) {
				
//      console.log("aaaaaaaaaaaaaaaaaaaaaaa");
//      console.log("err");
//      console.log(err);
//      console.log("response2");
//      console.log(response);
//  			});

// var aa=db.modify({ protocol:"azazazaz"}).where("url","/forget").callback(function(err, response) {
				
//     console.log("aaaaaaaaaaaaaaaaaaaaaaa");
//     console.log("err");
//     console.log(err);
//     console.log("response");
//     console.log(response);
// 			});




}



nosINS2=async function(db,arg){
	var nosql = NOSQL(db);
	return await new  Promise(async(resolve, reject) => {
		try{
			nosql.insert(arg).callback(function(err, response) {
				if(err)
					resolve(false)
				resolve(true);
			});
		}catch(err){
				// output.status=500;
				// output.data=err;
				resolve(false);

		}
	});
}


