/*
 * Copyright 2024 CloudWeGo Authors
 *
 * 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.
 */

package remote

import (
	"github.com/cloudwego/kitex/internal/mocks"
	"github.com/cloudwego/kitex/pkg/serviceinfo"
)

type MockSvcSearcher struct {
	svcMap        map[string]*serviceinfo.ServiceInfo
	methodSvcMap  map[string]*serviceinfo.ServiceInfo
	targetSvcInfo *serviceinfo.ServiceInfo
}

func NewMockSvcSearcher(svcMap map[string]*serviceinfo.ServiceInfo) *MockSvcSearcher {
	methodSvcMap := make(map[string]*serviceinfo.ServiceInfo)
	for _, svc := range svcMap {
		for method := range svc.Methods {
			methodSvcMap[method] = svc
		}
	}
	s := &MockSvcSearcher{svcMap: svcMap, methodSvcMap: methodSvcMap}
	if len(svcMap) == 1 {
		for _, svc := range svcMap {
			s.targetSvcInfo = svc
			break
		}
	}
	return s
}

func NewDefaultSvcSearcher() *MockSvcSearcher {
	svcInfo := mocks.ServiceInfo()
	s := map[string]*serviceinfo.ServiceInfo{
		mocks.MockServiceName: svcInfo,
	}
	m := map[string]*serviceinfo.ServiceInfo{
		mocks.MockMethod:          svcInfo,
		mocks.MockExceptionMethod: svcInfo,
		mocks.MockErrorMethod:     svcInfo,
		mocks.MockOnewayMethod:    svcInfo,
		mocks.MockStreamingMethod: svcInfo,
	}
	return &MockSvcSearcher{svcMap: s, methodSvcMap: m, targetSvcInfo: svcInfo}
}

func (s *MockSvcSearcher) SearchService(svcName, methodName string, strict bool, codecType serviceinfo.PayloadCodec) *serviceinfo.ServiceInfo {
	if strict {
		if svc := s.svcMap[svcName]; svc != nil {
			return svc
		}
		return nil
	}
	if s.targetSvcInfo != nil {
		return s.targetSvcInfo
	}
	var svc *serviceinfo.ServiceInfo
	if svcName == "" {
		svc = s.methodSvcMap[methodName]
	} else {
		svc = s.svcMap[svcName]
	}
	if svc != nil {
		return svc
	}
	return nil
}
