#!/usr/bin/env node
const request = require('request')
const Bacon = require('baconjs').Bacon
const format = require('../format')

const req = (method, url, cookie, form) => {
    var opts = {
        url: `https://timmi.talintenniskeskus.fi/WebTimmi/${url}`,
        headers: {
            Cookie: cookie,
            'Cache-Control': 'no-cache'
        }
    }
    if(cookie) opts.headers = {Cookie: cookie}
    if(form) opts.form = form
    return Bacon.fromNodeCallback(request[method], opts)
}
const get = (url, cookie, form) => req('get', url, cookie, form)
const post = (url, cookie, form) => req('post', url, cookie, form)
const json = observable => observable.map('.body').map(JSON.parse)

const login = () => get('login.do?loginName=GUEST&password=GUEST').flatMap(res => {
    try {
        return res.headers['set-cookie'][0].split(';')[0]
    } catch (e) {
        return new Bacon.Error(e)
    }
})

const getItems = cookie => json(get('weekViewAjaxAction.do?oper=getItems', cookie))
const getProfiles = cookie => json(post('autoCompleteAjax.do', cookie, {actionCode: 'getProfiles'}))
const getRoomPartsForCalendarAjax = cookie => json(post('getRoomPartsForCalendarAjax.do', cookie, {
    actionCode: 'getRoomPartsForProfile',
    id: 2
}))
const getRightsResourcesForCalendar = cookie => json(post('weekViewAjaxAction.do', cookie, {oper: 'getRightsResourcesForCalendar'}))
const updateStructure = (cookie, roomParts) => post('weekViewAjaxAction.do', cookie, {
    oper: 'updateStructure',
    structure: JSON.stringify({
        "structure": [{
            //roomParts.map(x=> x.roomPartBean.roomPartId)
            roomPartIds: roomParts.map(x=> x.roomPartBean.roomPartId).map(String),
            roomPartNames: [],
            roomPartColors: [],
            startTime: '06:30',
            endTime: '22:30',
            periodization: '60',
            calendarSize: 4,
            selectedDays: [0, 0, 0, 0, 0, 0, 1],
            singlePickedDates: false,
            referenceDateMills: 1474885798000
        }]
    }),
    form: JSON.stringify({
        sundaySelected: '1',
        startTime: '06:30',
        endTime: '22:30',
        timePeriod: '60',
        copyMoveDefault: '0',
        copyMoveOption: '0',
        lockTimeOnChoose: '1',
        minTimeChange: '15',
        calendarSize: '4'
    })
})
const getTimeCells = cookie => json(get('weekViewAjaxAction.do?oper=getTimeCells', cookie))
const timeZoneAjax = cookie => json(get('timeZoneAjax.do', cookie))

//login().flatMap(getProfiles).onValue(x => console.log('jee', format.prettyPrint(x)))
//login().flatMap(getRoomParts).map(format.prettyPrint).log()
login().flatMap(cookie =>
    getProfiles(cookie)
    //.flatMap(x => timeZoneAjax(cookie))
    //.flatMap(x => getRightsResourcesForCalendar(cookie))
        .flatMap(x => getProfiles(cookie))
        .flatMap(x => getRoomPartsForCalendarAjax(cookie))
        .flatMap(roomParts => updateStructure(cookie, roomParts))
        .flatMap(x => getRightsResourcesForCalendar(cookie))
        .flatMap(x => getTimeCells(cookie))
        .flatMap(x => getItems(cookie)))
    .map(format.prettyPrint).log()

@@ -1123,30 +1123,43 @@
 endarAjax = 
+(
 cookie
+, profileId)
  =%3E json(pos
@@ -1254,17 +1254,25 @@
     id: 
-2
+profileId
 %0A%7D))%0Acon
@@ -2802,32 +2802,34 @@
 ar(cookie))%0A    
+//
     .flatMap(x =
@@ -2860,33 +2860,40 @@
        .flatMap(
-x
+profiles
  =%3E getRoomParts
@@ -2910,24 +2910,47 @@
 rAjax(cookie
+, profiles%5B0%5D.profileId
 ))%0A        .
@@ -3006,32 +3006,34 @@
 Parts))%0A        
+//
 .flatMap(x =%3E ge
@@ -3069,32 +3069,34 @@
 ookie))%0A        
+//
 .flatMap(x =%3E ge

Simplify calls