////////////////////////////////////////////////////////////////////////////
//
// Copyright 2017 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

/* eslint-env es6, node */

'use strict';

const Realm = require('realm');
const TestCase = require('./asserts');

const isNodeProcess = (typeof process === 'object' && process + '' === '[object process]');

module.exports = {
    testSynced: function() {
        if (!global.enableSyncTests) {
            return Promise.resolve();
        }

        const credentials = Realm.Sync.Credentials.anonymous();
        return Realm.Sync.User.login('http://127.0.0.1:9080', credentials).then(user => {
            const config = user.createConfiguration({ sync: { url: 'realm://127.0.0.1:9080/~/myrealm', fullSynchronization: true },
                             schema: [{ name: 'IntegerPrimaryKey', properties: { int: 'int?' }, primaryKey: 'int' },
                                      { name: 'StringPrimaryKey', properties: { string: 'string?' }, primaryKey: 'string' },
                                      { name: 'NoPrimaryKey', properties: { string: 'string' }},
                                     ],
                           });
            return Realm.open(config).then(realm => {
                var integer, nullInteger;
                var string, nullString;
                var none;
                realm.write(() => {
                    integer = realm.create('IntegerPrimaryKey', [12345]);
                    nullInteger = realm.create('IntegerPrimaryKey', [null]);
                    string = realm.create('StringPrimaryKey', ["hello, world"]);
                    nullString = realm.create('StringPrimaryKey', [null]);
                    none = realm.create('NoPrimaryKey', ["hello, world"]);
                });

                let integerId = integer._objectId();
                let nullIntegerId = nullInteger._objectId();
                let stringId = string._objectId();
                let nullStringId = nullString._objectId();
                let noneId = none._objectId();

                TestCase.assertTrue(integer._isSameObject(realm._objectForObjectId('IntegerPrimaryKey', integerId)));
                TestCase.assertTrue(nullInteger._isSameObject(realm._objectForObjectId('IntegerPrimaryKey', nullIntegerId)));
                TestCase.assertTrue(string._isSameObject(realm._objectForObjectId('StringPrimaryKey', stringId)));
                TestCase.assertTrue(nullString._isSameObject(realm._objectForObjectId('StringPrimaryKey', nullStringId)));
                TestCase.assertTrue(none._isSameObject(realm._objectForObjectId('NoPrimaryKey', noneId)));
            });
        });

    }
};
