/*
 * 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 (
	"fmt"
	"time"

	"github.com/sirupsen/logrus"

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

// +ioc:autowire=true
// +ioc:autowire:type=singleton

type App struct {
	// inject ServiceImpl1 pointer to Service1 's own interface with proxy wrapper
	// this interface belongs to ServiceImpl1
	Service1OwnInterface ServiceImpl1IOCInterface `singleton:""`
}

func (a *App) Run() {
	for {
		time.Sleep(time.Second * 3)
		fmt.Println(a.Service1OwnInterface.GetHelloString("laurence"))
	}
}

// +ioc:autowire=true
// +ioc:autowire:type=singleton

type ServiceImpl1 struct {
	Service2 ServiceImpl2IOCInterface `singleton:""`
}

func (s *ServiceImpl1) GetHelloString(name string) string {
	logrus.Infof("ServiceImpl1 GetHelloString with name = %s", name)
	logrus.Warningf("ServiceImpl1 GetHelloString with name = %s", name)
	logrus.Errorf("ServiceImpl1 GetHelloString with name = %s", name)
	return s.Service2.GetHelloString(name)
}

// +ioc:autowire=true
// +ioc:autowire:type=singleton

type ServiceImpl2 struct {
}

func (s *ServiceImpl2) GetHelloString(name string) string {
	logrus.Infof("ServiceImpl2 GetHelloString with name = %s", name)
	return fmt.Sprintf("This is ServiceImpl2, hello %s", name)
}

func main() {
	// start
	if err := ioc.Load(); err != nil {
		panic(err)
	}

	app, err := GetAppIOCInterfaceSingleton()
	if err != nil {
		panic(err)
	}
	app.Run()
}
