"""
Sandbox Tests.
"""
import os
import numpy as np
import h5pandas
from h5pandas import dataset_to_dataframe
from h5pandas import HDF5Dtype
from h5pandas import HDF5ExtensionArray
import h5py
import time
import pandas as pd

HDF5Dtype("i8")


def TestH5extensions():
    arr = np.random.rand(30000, 5)
    with h5py.File(
        "toto2.h5", "w", libver="latest", driver="core", backing_store=False
    ) as f:
        t0 = time.time()
        t00 = t0
        d = f.create_dataset("toto", data=arr)

        # very basic transformation in hdf5array
        array = HDF5ExtensionArray(d, 0)
        d[0, 0] = 1.0
        assert array[0] == 1.0

        # transformation into a series
        ser = pd.Series(array, name="toto", copy=False)
        ser.memory_usage()
        d[0, 0] = 2.0
        array[0] = 2.0
        assert ser[0] == 2.0
        assert d[0, 1] != 2.0

        t1 = time.time()
        print(d.dtype, t1 - t0)
        t0 = time.time()

        df = dataset_to_dataframe(d)
        df = dataset_to_dataframe(d, ["a", "b", "c", "d", "e"])
        t1 = time.time()
        print(df, t1 - t0)
        # assert on time to make sure the file is not loaded
        assert t1 - t0 < 0.02
        d[0, 0] = 0
        assert df.loc[0, "a"] == 0
        t0 = time.time()
        b = df["b"]

        ind = b < 0.5
        print(ind, time.time() - t0)
        t0 = time.time()

        b = df["b"]
        sub_df = df[ind]
        mini = df["a"].min()
        cosb = np.cos(df["b"])
        cosb_2 = np.cos(b)
        print(cosb)

        assert all(cosb == cosb_2)
        cosb > cosb_2
        print("Test cos", time.time() - t0)
        t0 = time.time()

        print("Test reduce std")
        res = b.std()
        print("Test std", time.time() - t0)
        t0 = time.time()

        print("Test accumulate cumsum")
        b.cumsum()
        print("Test cumsum", time.time() - t0)
        t0 = time.time()

        # test groupby
        t0 = time.time()
        result = df.groupby("a", as_index=True)
        b = result.b
        b.mean()
        print("Test groupby", time.time() - t0)

        t0 = time.time()
        result = df[(df["a"] < 0.5) & (df["b"] > 0.5)]
        result.c.mean()
        print("test loc :", time.time() - t0)
        t0 = time.time()

        # print(df.h5.file)

        df["z"] = df["a"] + df["b"] * df["c"]
        df["y"] = df["a"] - df["b"] / df["c"]
        df["z"] = df["a"] % df["b"]
        df["z"] = df["a"] % df["b"]
        print("Test basic operations", time.time() - t0)
        t0 = time.time()

        # print(df.h5.attrs.keys())

        # Tester operations with skipna

        # Test hasna

        # Test other types

        # Test casting from a type to another

    # print(b)
    print("Total time : ", time.time() - t00)


def test_rmul():
    arr = np.random.rand(3000, 5)
    with h5py.File(
        "toto2.h5", "w", libver="latest", driver="core", backing_store=False
    ) as f:
        d = f.create_dataset("toto", data=arr)
        df = dataset_to_dataframe(d)
        df[0] * 2.1


def test_op_2EA():
    arr = np.random.rand(3000, 5)
    with h5py.File(
        "toto2.h5", "w", libver="latest", driver="core", backing_store=False
    ) as f:
        d = f.create_dataset("toto", data=arr)
        df = dataset_to_dataframe(d)
        df[0] - df[0]


def test_add():
    arr = np.random.rand(3000, 5)
    with h5py.File(
        "toto2.h5", "w", libver="latest", driver="core", backing_store=False
    ) as f:
        d = f.create_dataset("toto", data=arr)
        df = dataset_to_dataframe(d)

        ser = df[0]
        ser + (-57.0)


def test_write_hdf5():
    arr = np.random.rand(3000, 5)
    with h5py.File(
        "foobar2.h5", "w", libver="latest", driver="core", backing_store=False
    ) as f:
        df0 = pd.DataFrame(arr)
        d = f.create_dataset("toto", data=arr)
        df = dataset_to_dataframe(d)
        df._values
        d2 = h5pandas.dataframe_to_hdf5(df, "foobar3.h5")
        df2 = dataset_to_dataframe(d2)
        assert (df2._values == df0._values).all()
    d2.file.close()
    os.remove("foobar3.h5")


def TestH5Group():
    arr = np.random.rand(3000, 5)
    df = pd.DataFrame(arr)
    df_named = pd.DataFrame(
        arr, columns=["é", "b", "c", "d", "e"], index=range(1000, 4000)
    )

    df.to_hdf("foobar.h5", key="random_fixed", format="fixed", mode="w")
    df.to_hdf("foobar.h5", key="random_table", format="table")
    df_named.to_hdf("foobar.h5", key="named_random_fixed", format="fixed")
    df_named.to_hdf("foobar.h5", key="named_random_table", format="table")

    with h5pandas.File("foobar.h5", "a", libver="latest") as f:
        f.create_dataset("h5pandas", data=df)
        f.create_dataset("h5pandas_named", data=df_named)

    with h5pandas.File("foobar.h5", "r", libver="latest") as f:
        df_random_fixed = f["random_fixed"]
        assert (df._values == df_random_fixed._values).all()

        df_named_random_fixed = f["named_random_fixed"]
        assert (df_named._values == df_named_random_fixed._values).all()
        assert (df_named.columns == df_named_random_fixed.columns).all()

        df_h5pandas = f["h5pandas"]
        assert (df._values == df_h5pandas._values).all()

        df_h5pandas_named = f["h5pandas_named"]
        assert (df_named._values == df_h5pandas_named._values).all()
        assert (df_named.columns == df_h5pandas_named.columns).all()

    os.remove("foobar.h5")


if __name__ == "__main__":
    TestH5extensions()
    TestH5Group()
    test_rmul()
    test_op_2EA()
    test_add()
    test_write_hdf5()
