const Apify = require('apify');
const request = require('request');
const fetch = require('node-fetch');

Apify.main(async () => {
    
    const input = await Apify.getValue('INPUT');

    const locDataset = await Apify.openDataset("location-list");

    const apiId = "b598e22d24d549b196a6e88eb98e292f";
    
    await locDataset.forEach(async (item, index) => {
        //console.log(`Item at ${index}: ${JSON.stringify(item)}`);
        if(item.city != undefined) {
        console.log(item.city);


        const urlData = "http://api.openweathermap.org/data/2.5/forecast?q=" + item.city + "&appid=" + apiId;
        const urlDt   = "http://api.openweathermap.org/data/2.5/forecast/daily?q=" + item.city +"&cnt=6&appid=" + apiId;


        var data = await Apify.call('zvAs0UdjIOmYK0WGh', { url: urlData });
        var sun = await Apify.call('zvAs0UdjIOmYK0WGh', { url: urlDt });

        console.log(data.output.body.city);
        console.log(sun.output.body.city);
        
        const dataset = await Apify.openDataset("3-hour-" + item.city);
        
        await dataset.pushData({dt0: data.output.body.list[0].dt, temp0: data.output.body.list[0].main.temp, humidity0: data.output.body.list[0].main.humidity, wind0: data.output.body.list[0].wind.speed, sunrise0: getSunrise(data.output.body.list[0].dt, sun.output.body), sunset0: getSunset(data.output.body.list[0].dt, sun.output.body),
                                dt1: data.output.body.list[1].dt, temp1: data.output.body.list[1].main.temp, humidity1: data.output.body.list[1].main.humidity, wind1: data.output.body.list[1].wind.speed, sunrise1: getSunrise(data.output.body.list[1].dt, sun.output.body), sunset1: getSunset(data.output.body.list[1].dt, sun.output.body),
                                dt2: data.output.body.list[2].dt, temp2: data.output.body.list[2].main.temp, humidity2: data.output.body.list[2].main.humidity, wind2: data.output.body.list[2].wind.speed, sunrise2: getSunrise(data.output.body.list[2].dt, sun.output.body), sunset2: getSunset(data.output.body.list[2].dt, sun.output.body),
                                dt3: data.output.body.list[3].dt, temp3: data.output.body.list[3].main.temp, humidity3: data.output.body.list[3].main.humidity, wind3: data.output.body.list[3].wind.speed, sunrise3: getSunrise(data.output.body.list[3].dt, sun.output.body), sunset3: getSunset(data.output.body.list[3].dt, sun.output.body),
                                dt4: data.output.body.list[4].dt, temp4: data.output.body.list[4].main.temp, humidity4: data.output.body.list[4].main.humidity, wind4: data.output.body.list[4].wind.speed, sunrise4: getSunrise(data.output.body.list[4].dt, sun.output.body), sunset4: getSunset(data.output.body.list[4].dt, sun.output.body),
                                dt5: data.output.body.list[5].dt, temp5: data.output.body.list[5].main.temp, humidity5: data.output.body.list[5].main.humidity, wind5: data.output.body.list[5].wind.speed, sunrise5: getSunrise(data.output.body.list[5].dt, sun.output.body), sunset5: getSunset(data.output.body.list[5].dt, sun.output.body),
                                dt6: data.output.body.list[6].dt, temp6: data.output.body.list[6].main.temp, humidity6: data.output.body.list[6].main.humidity, wind6: data.output.body.list[6].wind.speed, sunrise6: getSunrise(data.output.body.list[6].dt, sun.output.body), sunset6: getSunset(data.output.body.list[6].dt, sun.output.body),
                                dt7: data.output.body.list[7].dt, temp7: data.output.body.list[7].main.temp, humidity7: data.output.body.list[7].main.humidity, wind7: data.output.body.list[7].wind.speed, sunrise7: getSunrise(data.output.body.list[7].dt, sun.output.body), sunset7: getSunset(data.output.body.list[7].dt, sun.output.body),
                                dt8: data.output.body.list[8].dt, temp8: data.output.body.list[8].main.temp, humidity8: data.output.body.list[8].main.humidity, wind8: data.output.body.list[8].wind.speed, sunrise8: getSunrise(data.output.body.list[8].dt, sun.output.body), sunset8: getSunset(data.output.body.list[8].dt, sun.output.body),
                                dt9: data.output.body.list[9].dt, temp9: data.output.body.list[9].main.temp, humidity9: data.output.body.list[9].main.humidity, wind9: data.output.body.list[9].wind.speed, sunrise9: getSunrise(data.output.body.list[9].dt, sun.output.body), sunset9: getSunset(data.output.body.list[9].dt, sun.output.body),
                                dt10: data.output.body.list[10].dt, temp10: data.output.body.list[10].main.temp, humidity10: data.output.body.list[10].main.humidity, wind10: data.output.body.list[10].wind.speed, sunrise10: getSunrise(data.output.body.list[10].dt, sun.output.body), sunset10: getSunset(data.output.body.list[10].dt, sun.output.body),
                                dt11: data.output.body.list[11].dt, temp11: data.output.body.list[11].main.temp, humidity11: data.output.body.list[11].main.humidity, wind11: data.output.body.list[11].wind.speed, sunrise11: getSunrise(data.output.body.list[11].dt, sun.output.body), sunset11: getSunset(data.output.body.list[11].dt, sun.output.body),
                                dt12: data.output.body.list[12].dt, temp12: data.output.body.list[12].main.temp, humidity12: data.output.body.list[12].main.humidity, wind12: data.output.body.list[12].wind.speed, sunrise12: getSunrise(data.output.body.list[12].dt, sun.output.body), sunset12: getSunset(data.output.body.list[12].dt, sun.output.body),
                                dt13: data.output.body.list[13].dt, temp13: data.output.body.list[13].main.temp, humidity13: data.output.body.list[13].main.humidity, wind13: data.output.body.list[13].wind.speed, sunrise13: getSunrise(data.output.body.list[13].dt, sun.output.body), sunset13: getSunset(data.output.body.list[13].dt, sun.output.body),
                                dt14: data.output.body.list[14].dt, temp14: data.output.body.list[14].main.temp, humidity14: data.output.body.list[14].main.humidity, wind14: data.output.body.list[14].wind.speed, sunrise14: getSunrise(data.output.body.list[14].dt, sun.output.body), sunset14: getSunset(data.output.body.list[14].dt, sun.output.body),
                                dt15: data.output.body.list[15].dt, temp15: data.output.body.list[15].main.temp, humidity15: data.output.body.list[15].main.humidity, wind15: data.output.body.list[15].wind.speed, sunrise15: getSunrise(data.output.body.list[15].dt, sun.output.body), sunset15: getSunset(data.output.body.list[15].dt, sun.output.body),
                                dt16: data.output.body.list[16].dt, temp16: data.output.body.list[16].main.temp, humidity16: data.output.body.list[16].main.humidity, wind16: data.output.body.list[16].wind.speed, sunrise16: getSunrise(data.output.body.list[16].dt, sun.output.body), sunset16: getSunset(data.output.body.list[16].dt, sun.output.body),
                                dt17: data.output.body.list[17].dt, temp17: data.output.body.list[17].main.temp, humidity17: data.output.body.list[17].main.humidity, wind17: data.output.body.list[17].wind.speed, sunrise17: getSunrise(data.output.body.list[17].dt, sun.output.body), sunset17: getSunset(data.output.body.list[17].dt, sun.output.body),
                                dt18: data.output.body.list[18].dt, temp18: data.output.body.list[18].main.temp, humidity18: data.output.body.list[18].main.humidity, wind18: data.output.body.list[18].wind.speed, sunrise18: getSunrise(data.output.body.list[18].dt, sun.output.body), sunset18: getSunset(data.output.body.list[18].dt, sun.output.body),
                                dt19: data.output.body.list[19].dt, temp19: data.output.body.list[19].main.temp, humidity19: data.output.body.list[19].main.humidity, wind19: data.output.body.list[19].wind.speed, sunrise19: getSunrise(data.output.body.list[19].dt, sun.output.body), sunset19: getSunset(data.output.body.list[19].dt, sun.output.body),
                                dt20: data.output.body.list[20].dt, temp20: data.output.body.list[20].main.temp, humidity20: data.output.body.list[20].main.humidity, wind20: data.output.body.list[20].wind.speed, sunrise20: getSunrise(data.output.body.list[20].dt, sun.output.body), sunset20: getSunset(data.output.body.list[20].dt, sun.output.body),
                                dt21: data.output.body.list[21].dt, temp21: data.output.body.list[21].main.temp, humidity21: data.output.body.list[21].main.humidity, wind21: data.output.body.list[21].wind.speed, sunrise21: getSunrise(data.output.body.list[21].dt, sun.output.body), sunset21: getSunset(data.output.body.list[21].dt, sun.output.body),
                                dt22: data.output.body.list[22].dt, temp22: data.output.body.list[22].main.temp, humidity22: data.output.body.list[22].main.humidity, wind22: data.output.body.list[22].wind.speed, sunrise22: getSunrise(data.output.body.list[22].dt, sun.output.body), sunset22: getSunset(data.output.body.list[22].dt, sun.output.body),
                                dt23: data.output.body.list[23].dt, temp23: data.output.body.list[23].main.temp, humidity23: data.output.body.list[23].main.humidity, wind23: data.output.body.list[23].wind.speed, sunrise23: getSunrise(data.output.body.list[23].dt, sun.output.body), sunset23: getSunset(data.output.body.list[23].dt, sun.output.body),
                                dt24: data.output.body.list[24].dt, temp24: data.output.body.list[24].main.temp, humidity24: data.output.body.list[24].main.humidity, wind24: data.output.body.list[24].wind.speed, sunrise24: getSunrise(data.output.body.list[24].dt, sun.output.body), sunset24: getSunset(data.output.body.list[24].dt, sun.output.body),
                                dt25: data.output.body.list[25].dt, temp25: data.output.body.list[25].main.temp, humidity25: data.output.body.list[25].main.humidity, wind25: data.output.body.list[25].wind.speed, sunrise25: getSunrise(data.output.body.list[25].dt, sun.output.body), sunset25: getSunset(data.output.body.list[25].dt, sun.output.body),
                                dt26: data.output.body.list[26].dt, temp26: data.output.body.list[26].main.temp, humidity26: data.output.body.list[26].main.humidity, wind26: data.output.body.list[26].wind.speed, sunrise26: getSunrise(data.output.body.list[26].dt, sun.output.body), sunset26: getSunset(data.output.body.list[26].dt, sun.output.body),
                                dt27: data.output.body.list[27].dt, temp27: data.output.body.list[27].main.temp, humidity27: data.output.body.list[27].main.humidity, wind27: data.output.body.list[27].wind.speed, sunrise27: getSunrise(data.output.body.list[27].dt, sun.output.body), sunset27: getSunset(data.output.body.list[27].dt, sun.output.body),
                                dt28: data.output.body.list[28].dt, temp28: data.output.body.list[28].main.temp, humidity28: data.output.body.list[28].main.humidity, wind28: data.output.body.list[28].wind.speed, sunrise28: getSunrise(data.output.body.list[28].dt, sun.output.body), sunset28: getSunset(data.output.body.list[28].dt, sun.output.body),
                                dt29: data.output.body.list[29].dt, temp29: data.output.body.list[29].main.temp, humidity29: data.output.body.list[29].main.humidity, wind29: data.output.body.list[29].wind.speed, sunrise29: getSunrise(data.output.body.list[29].dt, sun.output.body), sunset29: getSunset(data.output.body.list[29].dt, sun.output.body),
                                dt30: data.output.body.list[30].dt, temp30: data.output.body.list[30].main.temp, humidity30: data.output.body.list[30].main.humidity, wind30: data.output.body.list[30].wind.speed, sunrise30: getSunrise(data.output.body.list[30].dt, sun.output.body), sunset30: getSunset(data.output.body.list[30].dt, sun.output.body),
                                dt31: data.output.body.list[31].dt, temp31: data.output.body.list[31].main.temp, humidity31: data.output.body.list[31].main.humidity, wind31: data.output.body.list[31].wind.speed, sunrise31: getSunrise(data.output.body.list[31].dt, sun.output.body), sunset31: getSunset(data.output.body.list[31].dt, sun.output.body),
                                dt32: data.output.body.list[32].dt, temp32: data.output.body.list[32].main.temp, humidity32: data.output.body.list[32].main.humidity, wind32: data.output.body.list[32].wind.speed, sunrise32: getSunrise(data.output.body.list[32].dt, sun.output.body), sunset32: getSunset(data.output.body.list[32].dt, sun.output.body),
                                dt33: data.output.body.list[33].dt, temp33: data.output.body.list[33].main.temp, humidity33: data.output.body.list[33].main.humidity, wind33: data.output.body.list[33].wind.speed, sunrise33: getSunrise(data.output.body.list[33].dt, sun.output.body), sunset33: getSunset(data.output.body.list[33].dt, sun.output.body),
                                dt34: data.output.body.list[34].dt, temp34: data.output.body.list[34].main.temp, humidity34: data.output.body.list[34].main.humidity, wind34: data.output.body.list[34].wind.speed, sunrise34: getSunrise(data.output.body.list[34].dt, sun.output.body), sunset34: getSunset(data.output.body.list[34].dt, sun.output.body),
                                dt35: data.output.body.list[35].dt, temp35: data.output.body.list[35].main.temp, humidity35: data.output.body.list[35].main.humidity, wind35: data.output.body.list[35].wind.speed, sunrise35: getSunrise(data.output.body.list[35].dt, sun.output.body), sunset35: getSunset(data.output.body.list[35].dt, sun.output.body),
                                dt36: data.output.body.list[36].dt, temp36: data.output.body.list[36].main.temp, humidity36: data.output.body.list[36].main.humidity, wind36: data.output.body.list[36].wind.speed, sunrise36: getSunrise(data.output.body.list[36].dt, sun.output.body), sunset36: getSunset(data.output.body.list[36].dt, sun.output.body),
                                dt37: data.output.body.list[37].dt, temp37: data.output.body.list[37].main.temp, humidity37: data.output.body.list[37].main.humidity, wind37: data.output.body.list[37].wind.speed, sunrise37: getSunrise(data.output.body.list[37].dt, sun.output.body), sunset37: getSunset(data.output.body.list[37].dt, sun.output.body),
                                dt38: data.output.body.list[38].dt, temp38: data.output.body.list[38].main.temp, humidity38: data.output.body.list[38].main.humidity, wind38: data.output.body.list[38].wind.speed, sunrise38: getSunrise(data.output.body.list[38].dt, sun.output.body), sunset38: getSunset(data.output.body.list[38].dt, sun.output.body),
                                dt39: data.output.body.list[39].dt, temp39: data.output.body.list[39].main.temp, humidity39: data.output.body.list[39].main.humidity, wind39: data.output.body.list[39].wind.speed, sunrise39: getSunrise(data.output.body.list[39].dt, sun.output.body), sunset39: getSunset(data.output.body.list[39].dt, sun.output.body),
                                

                                 
        });
        
        await Apify.utils.sleep(2000);
        }
        
    });
        
        

});
    


//function calculateCn2(temp)
function th_to_w(th){

    var w;

    if(th<-3) 				w = 0.11;
	if(th>=-3 && th<-2) 	w = 0.07;
	if(th>=-2 && th<-1) 	w = 0.08;
	if(th>=-1 && th<0) 		w = 0.06;
	if(th>=0 && th<1)	 	w = 0.05;
	if(th>=1 && th<2)		w = 0.10;
	if(th>=2 && th<3) 		w = 0.51;
	if(th>=3 && th<4) 		w = 0.75;
	if(th>=4 && th<5) 		w = 0.95;
	if(th>=5 && th<6) 		w = 1.00;
	if(th>=6 && th<7) 		w = 0.90;
	if(th>=7 && th<8) 		w = 0.80;
	if(th>=8 && th<9) 		w = 0.59;
	if(th>=9 && th<10) 		w = 0.32;
	if(th>=10 && th<11) 	w = 0.22;
	if(th>=11 && th<12) 	w = 0.10;
	if(th>=12 && th<13) 	w = 0.08;
	if(th>=13)				w = 0.13;

    return w;
}

function calculateCn2(t, rh, ws, dt, sun) {
    var a1 = 3.8E-14;
	var b1 = 2.0E-15;
	var c1 = -2.8E-15;
	var c2 = 2.9E-17;
	var c3 = -1.1E-19;
	var d1 = -2.5E-15;
	var d2 = 1.2E-15;
    var d3 = -8.5E-17;
	var e = -5.3E-13;

    var sunrise = sun.list[0].sunrise;
    var sunset  = sun.list[0].sunset;

    var dayNow = new Date(dt * 1000).getDate();
    
    for(var i=0;i<6;i++) {
        if(new Date( sun.list[i].sunrise * 1000).getDate() == dayNow) {
            sunrise = sun.list[i].sunrise;
            sunset  = sun.list[i].sunset;
        }
    }
    

    var tp = (sunset - sunrise)/12;
    var th = (dt - sunrise)/tp;
    var w = th_to_w(th);

    var Cn2 =  a1*w + b1*t + c1*(rh/100) + c2*(rh/100)*(rh/100) + 
					c3*(rh/100)*(rh/100)*(rh/100) + d1*ws + d2*ws*ws +
					d3*ws*ws*ws + e;
    
    console.log("sunrise: " + new Date(sunrise * 1000) + ", sunset: " + new Date(sunset * 1000) + ", dt: " + new Date(dt * 1000) + ", cn2: " + Cn2);
    return Cn2;
}
function getSunrise(dt, sun) {
    var sunrise = sun.list[0].sunrise;

    var dayNow = new Date(dt * 1000).getDate();

    for(var i=0;i<6;i++) {
        if(new Date( sun.list[i].sunrise * 1000).getDate() == dayNow) {
            sunrise = sun.list[i].sunrise;
        }
    }
    return sunrise;

}
function getSunset(dt, sun) {
    var sunset = sun.list[0].sunset;

    var dayNow = new Date(dt * 1000).getDate();

    for(var i=0;i<6;i++) {
        if(new Date( sun.list[i].sunset * 1000).getDate() == dayNow) {
            sunset = sun.list[i].sunset;
        }
    }
    return sunset;

}