#!/usr/bin/env python

import pytest
import pandas as pd
import xarray as xr
import numpy as np
from mapies.util.func_tools import time_converter, time_domain_selection, geo_to_rot, quality_assurance



@pytest.mark.parametrize(
    "test_input,expected", 
    [
        ("2024079", pd.to_datetime("2024079", format="%Y%j")), 
        ("2024-06-07", pd.to_datetime("20240607", format="%Y%m%d")), 
        ("2024-06-(%$07", pd.to_datetime("20240607", format="%Y%m%d")),
        ("202406071411", pd.to_datetime("202406071411", format="%Y%m%d%H%M")),
    ]
)
def test_time_converter(test_input, expected):
    assert time_converter(test_input) == expected

def test_time_converter_error():
    with pytest.raises(ValueError):
        time_converter("20240607141")

@pytest.mark.parametrize(
    "test_input,expected",
    [
        ((0, 0, 20, 35), (-23.95680324, -32.61460715)),
        ((10, 10, 20, 35), (-10.82847335, -24.45866947)),
        ((-10, -10, 20, 35), (-39.42034458, -39.15567129)),
        ((-100, -50, 20, 35), (-141.61239392, -26.30586515))
    ]
)
def test_geo_to_rot(test_input, expected):
    assert (round(geo_to_rot(*test_input)[0], 8), round(geo_to_rot(*test_input)[1], 8))  == expected

ds = xr.Dataset(
    data_vars={"flag1":np.array([0, 0, 0, 1]), "flag2":np.array([1, 0, 1, 1])}
)
obs = np.array([1, 2, 3, 4])
lon = np.array([1, 2, 3, 4])
lat = np.array([1, 2, 3, 4])
qualityFlags1 = {"qa_flag1":{"qa_name":"flag1", "qa_values":[1], "qa_combined":False}, "qa_flag2":{"qa_name":"flag2", "qa_values":[1], "qa_combined":False}}
qualityFlags2 = {"qa_flag1":{"qa_name":"flag1", "qa_values":[1], "qa_combined":True}, "qa_flag2":{"qa_name":"flag2", "qa_values":[1], "qa_combined":True}}
qualityFlags3 = {"qa_flag1":{"qa_name":"flag1", "qa_values":[1], "qa_combined":True}, "qa_flag2":{"qa_name":"flag2", "qa_values":[1], "qa_combined":False}}

@pytest.mark.parametrize(
    "test_input,expected", 
    [
        ((ds, obs, lat, lon, qualityFlags1), (np.array([1, 3, 4]), np.array([1, 3, 4]), np.array([1, 3, 4]))),
        ((ds, obs, lat, lon, qualityFlags2), (np.array([4]), np.array([4]), np.array([4]))),
        ((ds, obs, lat, lon, qualityFlags3), (np.array([1, 3, 4]), np.array([1, 3, 4]), np.array([1, 3, 4]))),
    ]
)
def test_quality_assurance(test_input, expected):
    assert quality_assurance(*test_input)[0].all() == expected[0].all()
    assert quality_assurance(*test_input)[1].all() == expected[1].all()
    assert quality_assurance(*test_input)[2].all() == expected[2].all()
"""
@pytest.mark.parametrize(
    "test_input,expected", 
    [
        ([], np.array([])),
    ]
)
def test_time_domain_selection(test_input, expected):
    assert time_domain_selection(test_input) == expected
"""