# Copyright (C) 2024  Christian Ledermann
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
"""Test gx Track and MultiTrack."""

from collections.abc import Iterable
from typing import Optional

from hypothesis import given
from hypothesis import strategies as st
from hypothesis.provisional import urls

import fastkml
import fastkml.data
import fastkml.enums
import fastkml.gx.data
import fastkml.types
from tests.base import Lxml
from tests.hypothesis.common import assert_repr_roundtrip
from tests.hypothesis.common import assert_str_roundtrip
from tests.hypothesis.common import assert_str_roundtrip_terse
from tests.hypothesis.common import assert_str_roundtrip_verbose
from tests.hypothesis.strategies import nc_name
from tests.hypothesis.strategies import track_items
from tests.hypothesis.strategies import xml_text


class TestLxml(Lxml):
    @given(
        id=st.one_of(st.none(), nc_name()),
        target_id=st.one_of(st.none(), nc_name()),
        altitude_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.AltitudeMode)),
        track_items=st.one_of(
            st.none(),
            st.lists(
                track_items(),
            ),
        ),
        extended_data=st.builds(
            fastkml.ExtendedData,
            elements=st.tuples(
                st.builds(
                    fastkml.data.Data,
                    name=xml_text().filter(lambda x: x.strip() != ""),
                    value=xml_text().filter(lambda x: x.strip() != ""),
                    display_name=st.one_of(st.none(), xml_text()),
                ),
                st.builds(
                    fastkml.SchemaData,
                    schema_url=urls(),
                    data=st.lists(
                        st.builds(
                            fastkml.data.SimpleData,
                            name=xml_text().filter(lambda x: x.strip() != ""),
                            value=xml_text().filter(lambda x: x.strip() != ""),
                        ),
                        min_size=1,
                        max_size=3,
                    ),
                    array_data=st.lists(
                        st.builds(
                            fastkml.gx.data.SimpleArrayData,
                            name=xml_text().filter(lambda x: x.strip() != ""),
                            data=st.lists(
                                xml_text().filter(lambda x: x.strip() != ""),
                                min_size=1,
                            ),
                        ),
                        min_size=1,
                        max_size=3,
                    ),
                ),
            ),
        ),
    )
    def test_fuzz_track_track_items(
        self,
        id: Optional[str],
        target_id: Optional[str],
        altitude_mode: Optional[fastkml.enums.AltitudeMode],
        track_items: Optional[Iterable[fastkml.gx.TrackItem]],
        extended_data: Optional[fastkml.ExtendedData],
    ) -> None:
        track = fastkml.gx.Track(
            id=id,
            target_id=target_id,
            altitude_mode=altitude_mode,
            track_items=track_items,
            extended_data=extended_data,
        )

        assert_repr_roundtrip(track)
        assert_str_roundtrip(track)
        assert_str_roundtrip_terse(track)
        assert_str_roundtrip_verbose(track)

    @given(
        id=st.one_of(st.none(), nc_name()),
        target_id=st.one_of(st.none(), nc_name()),
        altitude_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.AltitudeMode)),
        tracks=st.one_of(
            st.none(),
            st.lists(
                st.builds(
                    fastkml.gx.Track,
                    altitude_mode=st.one_of(
                        st.none(),
                        st.sampled_from(fastkml.enums.AltitudeMode),
                    ),
                    track_items=st.lists(
                        track_items(),
                    ),
                ),
            ),
        ),
        interpolate=st.one_of(st.none(), st.booleans()),
    )
    def test_fuzz_multi_track(
        self,
        id: Optional[str],
        target_id: Optional[str],
        altitude_mode: Optional[fastkml.enums.AltitudeMode],
        tracks: Optional[Iterable[fastkml.gx.Track]],
        interpolate: Optional[bool],
    ) -> None:
        multi_track = fastkml.gx.MultiTrack(
            id=id,
            target_id=target_id,
            altitude_mode=altitude_mode,
            tracks=tracks,
            interpolate=interpolate,
        )

        assert_repr_roundtrip(multi_track)
        assert_str_roundtrip(multi_track)
        assert_str_roundtrip_terse(multi_track)
        assert_str_roundtrip_verbose(multi_track)
