#
#    Copyright (c) 2023 Project CHIP Authors
#    All rights reserved.
#
#    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 chip.clusters as Clusters
from chip.clusters.Types import NullValue
from chip.testing.matter_testing import MatterBaseTest, async_test_body, default_matter_test_main
from mobly import asserts

# We don't have a good pipe between the c++ enums in CommissioningDelegate and python
# so this is hardcoded.
# I realize this is dodgy, not sure how to cross the enum from c++ to python cleanly
kConfigureUTCTime = 6
kConfigureTimeZone = 7
kConfigureDSTOffset = 8
kConfigureDefaultNTP = 9
kConfigureTrustedTimeSource = 19

# NOTE: all of these tests require a specific app setup. Please see TestTimeSyncTrustedTimeSourceRunner.py


class TestTestTimeSyncTrustedTimeSource(MatterBaseTest):
    # This test needs to be run against an app that has previously been commissioned, has been reset
    # but not factory reset, and which has been started with the --simulate-no-internal-time flag.
    # This test should be run using the provided "TestTimeSyncTrustedTimeSourceRunner.py" script
    @async_test_body
    async def test_SimulateNoInternalTime(self):
        ret = await self.read_single_attribute_check_success(
            cluster=Clusters.TimeSynchronization, attribute=Clusters.TimeSynchronization.Attributes.UTCTime)
        asserts.assert_equal(ret, NullValue, "Non-null value returned for time")

    @async_test_body
    async def test_HaveInternalTime(self):
        ret = await self.read_single_attribute_check_success(
            cluster=Clusters.TimeSynchronization, attribute=Clusters.TimeSynchronization.Attributes.UTCTime)
        asserts.assert_not_equal(ret, NullValue, "Null value returned for time")

    @async_test_body
    async def test_SetupTimeSourceACL(self):
        # We just want to append to this list
        ac = Clusters.AccessControl
        acl = await self.read_single_attribute_check_success(cluster=ac, attribute=ac.Attributes.Acl)
        new_acl_entry = ac.Structs.AccessControlEntryStruct(privilege=ac.Enums.AccessControlEntryPrivilegeEnum.kView,
                                                            authMode=ac.Enums.AccessControlEntryAuthModeEnum.kCase,
                                                            subjects=NullValue, targets=[ac.Structs.AccessControlTargetStruct(
                                                                cluster=Clusters.TimeSynchronization.id)]
                                                            )
        acl.append(new_acl_entry)
        await self.default_controller.WriteAttribute(nodeid=self.dut_node_id, attributes=[(0, ac.Attributes.Acl(acl))])

    async def ReadFromTrustedTimeSource(self):
        # Give the node a couple of seconds to reach out and set itself up
        # TODO: Subscribe to granularity instead.
        time.sleep(6)
        ret = await self.read_single_attribute_check_success(cluster=Clusters.TimeSynchronization, attribute=Clusters.TimeSynchronization.Attributes.UTCTime)
        asserts.assert_not_equal(ret, NullValue, "Returned time is null")
        ret = await self.read_single_attribute_check_success(cluster=Clusters.TimeSynchronization, attribute=Clusters.TimeSynchronization.Attributes.Granularity)
        asserts.assert_not_equal(ret, Clusters.TimeSynchronization.Enums.GranularityEnum.kNoTimeGranularity,
                                 "Returned Granularity is kNoTimeGranularity")
        # TODO: needs to be gated on the optional attribute
        ret = await self.read_single_attribute_check_success(cluster=Clusters.TimeSynchronization, attribute=Clusters.TimeSynchronization.Attributes.TimeSource)
        asserts.assert_equal(ret, Clusters.TimeSynchronization.Enums.TimeSourceEnum.kNodeTimeCluster,
                             "Returned time source is incorrect")

    @async_test_body
    async def test_SetAndReadFromTrustedTimeSource(self):
        asserts.assert_true('trusted_time_source' in self.matter_test_config.global_test_params,
                            "trusted_time_source must be included on the command line in "
                            "the --int-arg flag as trusted_time_source:<nodeId>")
        trusted_time_source = Clusters.TimeSynchronization.Structs.FabricScopedTrustedTimeSourceStruct(
            nodeID=self.matter_test_config.global_test_params["trusted_time_source"], endpoint=0)
        cmd = Clusters.TimeSynchronization.Commands.SetTrustedTimeSource(trustedTimeSource=trusted_time_source)
        await self.send_single_cmd(cmd)

        await self.ReadFromTrustedTimeSource()

    @async_test_body
    async def test_ReadFromTrustedTimeSource(self):
        await self.ReadFromTrustedTimeSource()


if __name__ == "__main__":
    default_matter_test_main()
