/*
  Copyright Amazon.com, Inc. or its affiliates. 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.
*/

package test

import (
	"database/sql/driver"

	"github.com/aws/aws-advanced-go-wrapper/awssql/driver_infrastructure"
	"github.com/aws/aws-advanced-go-wrapper/awssql/host_info_util"
	"github.com/aws/aws-advanced-go-wrapper/awssql/plugin_helpers"
	"github.com/aws/aws-advanced-go-wrapper/awssql/plugins"
	"github.com/aws/aws-advanced-go-wrapper/awssql/utils"
)

type BenchmarkPluginFactory struct{}

func (b BenchmarkPluginFactory) GetInstance(
	pluginService driver_infrastructure.PluginService,
	props *utils.RWMap[string, string]) (driver_infrastructure.ConnectionPlugin, error) {
	return NewBenchmarkPlugin(pluginService, props), nil
}

type BenchmarkPlugin struct {
	plugins.BaseConnectionPlugin
	resources     []string
	pluginService driver_infrastructure.PluginService
	props         *utils.RWMap[string, string]
}

func NewBenchmarkPlugin(pluginService driver_infrastructure.PluginService, props *utils.RWMap[string, string]) *BenchmarkPlugin {
	return &BenchmarkPlugin{
		pluginService: pluginService,
		props:         props,
	}
}

func (b *BenchmarkPlugin) GetSubscribedMethods() []string {
	return []string{plugin_helpers.ALL_METHODS}
}

func (b *BenchmarkPlugin) Connect(
	_ *host_info_util.HostInfo,
	props *utils.RWMap[string, string],
	_ bool,
	connectFunc driver_infrastructure.ConnectFunc) (driver.Conn, error) {
	b.resources = append(b.resources, "connect")
	return connectFunc(props)
}

func (b *BenchmarkPlugin) ForceConnect(
	_ *host_info_util.HostInfo,
	props *utils.RWMap[string, string],
	_ bool,
	connectFunc driver_infrastructure.ConnectFunc) (driver.Conn, error) {
	b.resources = append(b.resources, "forceConnect")
	return connectFunc(props)
}

func (b *BenchmarkPlugin) Execute(
	_ driver.Conn,
	_ string,
	executeFunc driver_infrastructure.ExecuteFunc,
	_ ...any) (wrappedReturnValue any, wrappedReturnValue2 any, wrappedOk bool, wrappedErr error) {
	b.resources = append(b.resources, "execute")
	return executeFunc()
}

func (b *BenchmarkPlugin) AcceptsStrategy(_ string) bool {
	return false
}
func (b *BenchmarkPlugin) GetHostInfoByStrategy(role host_info_util.HostRole, _ string, _ []*host_info_util.HostInfo) (*host_info_util.HostInfo, error) {
	b.resources = append(b.resources, "getHostInfoByStrategy")
	return host_info_util.NewHostInfoBuilder().SetHost("host").SetPort(1234).SetRole(role).Build()
}

func (b *BenchmarkPlugin) NotifyConnectionChanged(_ map[driver_infrastructure.HostChangeOptions]bool) driver_infrastructure.OldConnectionSuggestedAction {
	return driver_infrastructure.NO_OPINION
}

func (b *BenchmarkPlugin) NotifyHostListChanged(_ map[string]map[driver_infrastructure.HostChangeOptions]bool) {
	b.resources = append(b.resources, "notifyHostListChanged")
}

func (b *BenchmarkPlugin) InitHostProvider(
	_ *utils.RWMap[string, string],
	_ driver_infrastructure.HostListProviderService,
	_ func() error) error {
	b.resources = append(b.resources, "initHostProvider")
	return nil
}

func (b *BenchmarkPlugin) ReleaseResources() {
	b.resources = append(b.resources, "releaseResources")
}
