# -*- coding: utf-8 -*- {{{
# ===----------------------------------------------------------------------===
#
#                 Installable Component of Eclipse VOLTTRON
#
# ===----------------------------------------------------------------------===
#
# Copyright 2022 Battelle Memorial Institute
#
# 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.
#
# ===----------------------------------------------------------------------===
# }}}

from __future__ import annotations

import logging
from typing import Optional

from volttron.types.factories import CoreBuilder
from volttron.types import CoreLoop, Connection
from volttron.types.agent_context import AgentContext
from volttron.client.decorators import core_builder

from volttrontesting.mock_core import MockCore
from volttrontesting.mock_connection import MockConnection

_log = logging.getLogger(__name__)


@core_builder
class MockCoreBuilder(CoreBuilder):
    """Builder for creating mock cores for testing"""
    
    class Meta:
        name = "mock"
    
    def __init__(self, **kwargs):
        self._kwargs = kwargs
        _log.debug(f"MockCoreBuilder initialized with kwargs: {kwargs}")
    
    def build(self, *, context: AgentContext, owner=None) -> CoreLoop:
        """Build a MockCore instance - implements abstract method"""
        _log.info(f"Building MockCore for identity: {context.credentials.identity}")
        core = MockCore(context, owner)
        return core
    
    def build_core(self, context: AgentContext, owner=None) -> CoreLoop:
        """Build a MockCore instance"""
        return self.build(context=context, owner=owner)
    
    def build_connection(self, identity: str, address: str = None, **kwargs) -> Connection:
        """Build a MockConnection instance"""
        _log.info(f"Building MockConnection for identity: {identity}")
        return MockConnection(identity)