# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# Copyright 2018-2020 BigML
#
# 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.

import time
import json
import os
from datetime import datetime, timedelta
from world import world
from nose.tools import eq_, assert_less

from bigml.api import HTTP_CREATED
from bigml.api import HTTP_ACCEPTED
from bigml.api import FINISHED
from bigml.api import FAULTY
from bigml.api import get_status

from read_pca_steps import i_get_the_pca


#@step(r'the pca name is "(.*)"')
def i_check_pca_name(step, name):
    pca_name = world.pca['name']
    eq_(name, pca_name)

#@step(r'I create a PCA from a dataset$')
def i_create_a_pca_from_dataset(step):
    dataset = world.dataset.get('resource')
    resource = world.api.create_pca(dataset, {'name': 'new PCA'})
    world.status = resource['code']
    eq_(world.status, HTTP_CREATED)
    world.location = resource['location']
    world.pca = resource['object']
    world.pcas.append(resource['resource'])


#@step(r'I create a PCA from a dataset$')
def i_create_a_pca_with_params(step, params):
    params = json.loads(params)
    dataset = world.dataset.get('resource')
    resource = world.api.create_pca(dataset, params)
    world.status = resource['code']
    eq_(world.status, HTTP_CREATED)
    world.location = resource['location']
    world.pca = resource['object']
    world.pcas.append(resource['resource'])

def i_create_a_pca(step):
    i_create_a_pca_from_dataset(step)


#@step(r'I update the PCA name to "(.*)"$')
def i_update_pca_name(step, name):
    resource = world.api.update_pca(world.pca['resource'],
                                    {'name': name})
    world.status = resource['code']
    eq_(world.status, HTTP_ACCEPTED)
    world.location = resource['location']
    world.pca = resource['object']


#@step(r'I wait until the PCA status code is either (\d) or (-\d) less than (\d+)')
def wait_until_pca_status_code_is(step, code1, code2, secs):
    start = datetime.utcnow()
    delta = int(secs) * world.delta
    pca_id = world.pca['resource']
    i_get_the_pca(step, pca_id)
    status = get_status(world.pca)
    while (status['code'] != int(code1) and
           status['code'] != int(code2)):
           time.sleep(3)
           assert_less(datetime.utcnow() - start, timedelta(seconds=delta))
           i_get_the_pca(step, pca_id)
           status = get_status(world.pca)
    eq_(status['code'], int(code1))


#@step(r'I wait until the PCA is ready less than (\d+)')
def the_pca_is_finished_in_less_than(step, secs):
    wait_until_pca_status_code_is(step, FINISHED, FAULTY, secs)

[((37, 4, 37, 23), 'nose.tools.eq_', 'eq_', ({(37, 8, 37, 12): 'name', (37, 14, 37, 22): 'pca_name'}, {}), '(name, pca_name)', False, 'from nose.tools import eq_, assert_less\n'), ((41, 14, 41, 43), 'world.world.dataset.get', 'world.dataset.get', ({(41, 32, 41, 42): '"""resource"""'}, {}), "('resource')", False, 'from world import world\n'), ((42, 15, 42, 65), 'world.world.api.create_pca', 'world.api.create_pca', ({(42, 36, 42, 43): 'dataset', (42, 45, 42, 64): "{'name': 'new PCA'}"}, {}), "(dataset, {'name': 'new PCA'})", False, 'from world import world\n'), ((44, 4, 44, 35), 'nose.tools.eq_', 'eq_', ({(44, 8, 44, 20): 'world.status', (44, 22, 44, 34): 'HTTP_CREATED'}, {}), '(world.status, HTTP_CREATED)', False, 'from nose.tools import eq_, assert_less\n'), ((47, 4, 47, 43), 'world.world.pcas.append', 'world.pcas.append', ({(47, 22, 47, 42): "resource['resource']"}, {}), "(resource['resource'])", False, 'from world import world\n'), ((52, 13, 52, 31), 'json.loads', 'json.loads', ({(52, 24, 52, 30): 'params'}, {}), '(params)', False, 'import json\n'), ((53, 14, 53, 43), 'world.world.dataset.get', 'world.dataset.get', ({(53, 32, 53, 42): '"""resource"""'}, {}), "('resource')", False, 'from world import world\n'), ((54, 15, 54, 52), 'world.world.api.create_pca', 'world.api.create_pca', ({(54, 36, 54, 43): 'dataset', (54, 45, 54, 51): 'params'}, {}), '(dataset, params)', False, 'from world import world\n'), ((56, 4, 56, 35), 'nose.tools.eq_', 'eq_', ({(56, 8, 56, 20): 'world.status', (56, 22, 56, 34): 'HTTP_CREATED'}, {}), '(world.status, HTTP_CREATED)', False, 'from nose.tools import eq_, assert_less\n'), ((59, 4, 59, 43), 'world.world.pcas.append', 'world.pcas.append', ({(59, 22, 59, 42): "resource['resource']"}, {}), "(resource['resource'])", False, 'from world import world\n'), ((67, 15, 68, 51), 'world.world.api.update_pca', 'world.api.update_pca', ({(67, 36, 67, 57): "world.pca['resource']", (68, 36, 68, 50): "{'name': name}"}, {}), "(world.pca['resource'], {'name': name})", False, 'from world import world\n'), ((70, 4, 70, 36), 'nose.tools.eq_', 'eq_', ({(70, 8, 70, 20): 'world.status', (70, 22, 70, 35): 'HTTP_ACCEPTED'}, {}), '(world.status, HTTP_ACCEPTED)', False, 'from nose.tools import eq_, assert_less\n'), ((77, 12, 77, 29), 'datetime.datetime.utcnow', 'datetime.utcnow', ({}, {}), '()', False, 'from datetime import datetime, timedelta\n'), ((80, 4, 80, 31), 'read_pca_steps.i_get_the_pca', 'i_get_the_pca', ({(80, 18, 80, 22): 'step', (80, 24, 80, 30): 'pca_id'}, {}), '(step, pca_id)', False, 'from read_pca_steps import i_get_the_pca\n'), ((81, 13, 81, 34), 'bigml.api.get_status', 'get_status', ({(81, 24, 81, 33): 'world.pca'}, {}), '(world.pca)', False, 'from bigml.api import get_status\n'), ((84, 11, 84, 24), 'time.sleep', 'time.sleep', ({(84, 22, 84, 23): '(3)'}, {}), '(3)', False, 'import time\n'), ((86, 11, 86, 38), 'read_pca_steps.i_get_the_pca', 'i_get_the_pca', ({(86, 25, 86, 29): 'step', (86, 31, 86, 37): 'pca_id'}, {}), '(step, pca_id)', False, 'from read_pca_steps import i_get_the_pca\n'), ((87, 20, 87, 41), 'bigml.api.get_status', 'get_status', ({(87, 31, 87, 40): 'world.pca'}, {}), '(world.pca)', False, 'from bigml.api import get_status\n'), ((85, 50, 85, 74), 'datetime.timedelta', 'timedelta', (), '', False, 'from datetime import datetime, timedelta\n'), ((85, 23, 85, 40), 'datetime.datetime.utcnow', 'datetime.utcnow', ({}, {}), '()', False, 'from datetime import datetime, timedelta\n')]