'use strict';

var frisby = require('frisby');
var config = require('../../test-config.json');

var args = config[process.env.test || 'local'];
var host = args.host,
    clientId = args.clientId,
    usr = args.user2,
    pwd = args.password2;

//Testing that we can insert multiple clubs with a PUT, then do a bulk update with a PUT
//Using a different test user so we don't collide with the regular CRUD tests
frisby.create('Log in')
    .post(host + '/token', {
        grant_type: 'password',
        username: usr,
        password: pwd,
        client_id: clientId
    })
    .afterJSON(function (json) {
        var clubs = [];
        clubs.push({
            clubKey: 1,
            sortOrderNumber: 1,
            approachFlag: false,
            teeballFlag: true
        });
        clubs.push({
            clubKey: 3,
            sortOrderNumber: 2,
            approachFlag: false,
            teeballFlag: true
        });
        clubs.push({
            clubKey: 8,
            sortOrderNumber: 12,
            approachFlag: true,
            teeballFlag: true
        });
        frisby.create('Bulk insert clubs')
            .put(host + '/api/users/clubs', clubs, { json: true, headers: { 'Authorization': 'Bearer ' + json.access_token } })
            .expectStatus(202)
            .after(function () {
                frisby.create('Get inserted clubs')
                    .get(host + '/api/users/clubs', { headers: { 'Authorization': 'Bearer ' + json.access_token }})
                    .expectStatus(200)
                    .expectHeaderContains('content-type', 'application/json')
                    .expectJSONTypes('*', { //weird array checking syntax: http://frisbyjs.com/docs/api/
                        key: Number,
                        userId: String,
                        clubKey: Number,
                        sortOrderNumber: Number,
                        approachFlag: Boolean,
                        teeballFlag: Boolean
                    })
                    .afterJSON(function (data) {
                        expect(data.length).toEqual(3);
                        data[0].sortOrderNumber = 100;
                        data[1].sortOrderNumber = 101;
                        data[2].sortOrderNumber = 1;
                        frisby.create('Bulk update')
                            .put(host + '/api/users/clubs', data, { json: true, headers: { 'Authorization': 'Bearer ' + json.access_token } })
                            .expectStatus(202)
                            .after(function () {
                                frisby.create('Delete a club')
                                    .delete(host + '/api/users/clubs/' + data[0].key, null, { headers: { 'Authorization': 'Bearer ' + json.access_token }})
                                    .expectStatus(200)
                                    .toss();
                                frisby.create('Delete a club')
                                    .delete(host + '/api/users/clubs/' + data[1].key, null, { headers: { 'Authorization': 'Bearer ' + json.access_token }})
                                    .expectStatus(200)
                                    .toss();
                                frisby.create('Delete a club')
                                    .delete(host + '/api/users/clubs/' + data[2].key, null, { headers: { 'Authorization': 'Bearer ' + json.access_token }})
                                    .expectStatus(200)
                                    .toss();
                            })
                            .toss();
                    })
                    .toss();
            })
            .toss();
    })
    .toss();