/*
 * Copyright (c) 2022, Alibaba Group;
 * 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 main

import (
	"log"
	"runtime"
	"testing"
	"time"

	"github.com/alibaba/ioc-golang/config"

	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"github.com/stretchr/testify/assert"

	"github.com/alibaba/ioc-golang"
	"github.com/alibaba/ioc-golang/extension/registry/nacos"
	"github.com/alibaba/ioc-golang/test/docker_compose"
)

const (
	ipFoo   = "127.0.0.1"
	portFoo = 1999
)

func (a *App) TestRun(t *testing.T) {
	testGetAndGetService(t, a.NormalNacosClient, "normal-autowire-client-ioc-golang-debug-service")
	testGetAndGetService(t, a.NormalNacosClient2, "normal-autowire-client2-ioc-golang-debug-service")
	testGetAndGetService(t, a.createByAPINacosClient, "createByAPINacosClient-ioc-golang-debug-service")
}

func testGetAndGetService(t *testing.T, client nacos.NamingClientIOCInterface, serviceName string) {
	_, err := client.RegisterInstance(vo.RegisterInstanceParam{
		Ip:          "127.0.0.1",
		Port:        1999,
		ServiceName: serviceName,
	})
	retries := 0
	for err != nil && retries < 3 {
		time.Sleep(time.Second * 10)
		_, err = client.RegisterInstance(vo.RegisterInstanceParam{
			Ip:          "127.0.0.1",
			Port:        1999,
			ServiceName: serviceName,
		})
		retries++
	}
	if err != nil {
		panic(err)
	}

	service, err := client.GetService(vo.GetServiceParam{
		ServiceName: serviceName,
	})
	if err != nil {
		return
	}
	assert.Equal(t, serviceName, service.Name)
	assert.Equal(t, 1, len(service.Hosts))
	assert.Equal(t, ipFoo, service.Hosts[0].Ip)
	assert.Equal(t, uint64(portFoo), service.Hosts[0].Port)
}

func TestNacosClient(t *testing.T) {
	if runtime.GOARCH != "amd64" {
		log.Println("Warning: Nacos image only support amd arch. Skip integration test")
		return
	}
	assert.Nil(t, docker_compose.DockerComposeUp("../docker-compose/docker-compose.yaml", time.Second*10))
	assert.Nil(t, ioc.Load(
		config.WithSearchPath("../conf")))
	app, err := GetAppSingleton(&Param{
		NacosPort: 8848,
		NacosAddr: "localhost",
	})
	assert.Nil(t, err)
	app.TestRun(t)

	assert.Nil(t, docker_compose.DockerComposeDown("../docker-compose/docker-compose.yaml"))
}
