# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (c) 2020, Intel Corporation All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#
#     Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************

import time

import pandas
import numpy as np

from sdc.tests.test_utils import test_global_input_data_float64
from sdc.tests.tests_perf.test_perf_base import TestBase
from sdc.tests.tests_perf.test_perf_utils import perf_data_gen_fixed_len
from .generator import generate_test_cases
from .generator import TestCase as TC
from .data_generator import gen_series

rolling_usecase_tmpl = """
def series_rolling_{method_name}_usecase(data, {extra_usecase_params}):
    start_time = time.time()
    for i in range({ncalls}):
        res = data.rolling({rolling_params}).{method_name}({method_params})
    end_time = time.time()
    return end_time - start_time, res
"""


def get_rolling_params(window=100, min_periods=None):
    """Generate supported rolling parameters"""
    rolling_params = [f'{window}']
    if min_periods:
        rolling_params.append(f'min_periods={min_periods}')

    return ', '.join(rolling_params)


def gen_series_rolling_usecase(method_name, rolling_params=None,
                               extra_usecase_params='',
                               method_params='', ncalls=1):
    """Generate series rolling method use case"""
    if not rolling_params:
        rolling_params = get_rolling_params()

    func_text = rolling_usecase_tmpl.format(**{
        'method_name': method_name,
        'extra_usecase_params': extra_usecase_params,
        'rolling_params': rolling_params,
        'method_params': method_params,
        'ncalls': ncalls
    })

    global_vars = {'np': np, 'time': time}
    loc_vars = {}
    exec(func_text, global_vars, loc_vars)
    _series_rolling_usecase = loc_vars[f'series_rolling_{method_name}_usecase']

    return _series_rolling_usecase


# python -m sdc.runtests sdc.tests.tests_perf.test_perf_series_rolling.TestSeriesRollingMethods
class TestSeriesRollingMethods(TestBase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.map_ncalls_dlength = {
            'corr': (100, [4 * 10 ** 5]),
            'count': (100, [8 * 10 ** 5]),
            'cov': (100, [4 * 10 ** 5]),
            'kurt': (100, [8 * 10 ** 5]),
            'max': (100, [4 * 10 ** 5]),
            'mean': (100, [8 * 10 ** 5]),
            'min': (100, [4 * 10 ** 5]),
            'skew': (100, [8 * 10 ** 5]),
            'sum': (100, [8 * 10 ** 5]),
            'std': (100, [8 * 10 ** 5]),
            'var': (100, [8 * 10 ** 5]),
        }

    def _test_case(self, pyfunc, name, total_data_length, input_data=None, data_num=1, data_gens=None):
        test_name = 'Series.rolling.{}'.format(name)

        data_num = len(data_gens) if data_gens is not None else data_num
        default_data_gens = [gen_series] * data_num
        data_gens = data_gens or default_data_gens
        default_input_data = [np.asarray(test_global_input_data_float64).flatten()] + [None] * (data_num - 1)
        input_data = input_data or default_input_data

        for data_length in total_data_length:
            base = {
                'test_name': test_name,
                'data_size': data_length,
            }

            args = tuple(gen(data_length, input_data=input_data[i]) for i, gen in enumerate(data_gens))
            self._test_jit(pyfunc, base, *args)
            self._test_py(pyfunc, base, *args)

    def _test_series_rolling_method(self, name, rolling_params=None,
                                    extra_usecase_params='', method_params=''):
        ncalls, total_data_length = self.map_ncalls_dlength[name]
        usecase = gen_series_rolling_usecase(name, rolling_params=rolling_params,
                                             extra_usecase_params=extra_usecase_params,
                                             method_params=method_params, ncalls=ncalls)
        data_num = 1
        if extra_usecase_params:
            data_num += len(extra_usecase_params.split(', '))
        self._test_case(usecase, name, total_data_length, data_num=data_num)

    def test_series_rolling_corr(self):
        self._test_series_rolling_method('corr', extra_usecase_params='other',
                                         method_params='other=other')

    def test_series_rolling_count(self):
        self._test_series_rolling_method('count')

    def test_series_rolling_cov(self):
        self._test_series_rolling_method('cov', extra_usecase_params='other',
                                         method_params='other=other')

    def test_series_rolling_kurt(self):
        self._test_series_rolling_method('kurt')

    def test_series_rolling_max(self):
        self._test_series_rolling_method('max')

    def test_series_rolling_mean(self):
        self._test_series_rolling_method('mean')

    def test_series_rolling_min(self):
        self._test_series_rolling_method('min')

    def test_series_rolling_skew(self):
        self._test_series_rolling_method('skew')

    def test_series_rolling_sum(self):
        self._test_series_rolling_method('sum')

    def test_series_rolling_std(self):
        self._test_series_rolling_method('std')

    def test_series_rolling_var(self):
        self._test_series_rolling_method('var')


cases = [
    TC(name='apply', size=[10 ** 7], params='func=lambda x: np.nan if len(x) == 0 else x.mean()'),
    TC(name='median', size=[10 ** 7]),
    TC(name='quantile', size=[10 ** 7], params='0.2'),
    TC(name='std', size=[10 ** 7]),
]


generate_test_cases(cases, TestSeriesRollingMethods, 'series', 'rolling({})'.format(get_rolling_params()))
