package kern

import (
	"fmt"
	"os"
	"testing"

	"gitp78su.ipnodns.ru/svi/kern/v2/mock/mock_env"
)

type tester struct {
	t  *testing.T
	me *mock_env.MockEnv
}

func TestBuilders(t *testing.T) {
	sf := &tester{
		t:  t,
		me: mock_env.MakeEnv(),
	}
	_ = os.Unsetenv("LOCAL_STORE_PATH")
	_ = os.Setenv("LOCAL_STORE_PATH", "/store/store_kern")
	_ = os.Unsetenv("LOCAL_HTTP_URL")
	os.Setenv("LOCAL_HTTP_URL", "http://localhost:18311/")
	fnClear := func() {
		pwd := sf.me.Pwd() + "/store/store_kern"
		_ = os.RemoveAll(pwd)
	}
	fnClear()
	defer fnClear()
	sf.new()
	sf.newModBad()
}

func (sf *tester) newModBad() {
	sf.t.Log("newModBad")
	defer func() {
		if _panic := recover(); _panic == nil {
			sf.t.Fatalf("newModBad(): panic==nil")
		}
	}()
	_ = NewKernelModule("")
}

// создание компонентов
func (sf *tester) new() {
	sf.t.Log("new")
	ctx := GetKernelCtx()
	{
		if ctx == nil {
			sf.t.Fatalf("new(): IKernelCtx==nil")
		}
		store := GetKernelStoreKv()
		res := store.Delete("test_builders")
		if res.IsErr() {
			sf.t.Fatalf("new(): get empty key, store, err=%v", res.Err())
		}
		safeBool := NewSafeBool()
		if safeBool == nil {
			sf.t.Fatalf("new(): ISafeBool==nil")
		}

		kernBus := GetKernelBusLocal()
		if kernBus == nil {
			sf.t.Fatalf("new(): (local) IKernelBus==nil")
		}

		kernBusHttp := GetKernelBusHttp()
		if kernBusHttp == nil {
			sf.t.Fatalf("new(): (http) IKernelBus==nil")
		}

	}
	{
		monLocal := GetMonolitLocal("mon_local")
		if monLocal == nil {
			sf.t.Fatalf("new(): (local) IKernelMonolit==nil")
		}

		monHttp := GetMonolitHttp("mon_http")
		if monHttp == nil {
			sf.t.Fatalf("new(): (http) IKernelMonolit==nil")
		}

		mod := NewKernelModule("test_mod")
		if mod == nil {
			sf.t.Fatalf("new(): IKernelModule==nil")
		}

		clientLocal := NewClientBusLocal()
		if clientLocal == nil {
			sf.t.Fatalf("new(): (local) IBusClient==nil")
		}
		clientHttp := NewClientBusHttp("test_url")
		if clientHttp == nil {
			sf.t.Fatalf("new(): (http) IBusClient==nil")
		}

	}
	{
		modServHttp := GetModuleServHttp()
		if modServHttp == nil {
			sf.t.Fatalf("new(): modServHttp==nil")
		}

		modKernelCtx := GetModuleKernelCtx()
		if modKernelCtx == nil {
			sf.t.Fatalf("new(): modKernelCtx==nil")
		}
		modKernelKeeper := GetModuleKernelKeeper()
		if modKernelKeeper == nil {
			sf.t.Fatalf("new(): modKernelKeeper==nil")
		}
		modWui := GetModuleWui()
		if modWui == nil {
			sf.t.Fatalf("new(): modWui==nil")
		}

		logBuf := NewLogBuf(true)
		if logBuf == nil {
			sf.t.Fatalf("new(): ILogBuf==nil")
		}
		boolReact := NewSafeBoolReact()
		if boolReact == nil {
			sf.t.Fatalf("new(): ISafeBoolReact==nil")
		}
		safeInt := NewSafeInt()
		if safeInt == nil {
			sf.t.Fatalf("new(): ISafeInt==nil")
		}
		lCtx := NewLocalCtx(ctx.Ctx())
		if lCtx == nil {
			sf.t.Fatalf("new(): ILocalCtx==nil")
		}
	}
	{
		safeStr := NewSafeString()
		if safeStr == nil {
			sf.t.Fatalf("new(): ISafeStr==nil")
		}

		fnHassert := GetFnHassert()
		if fnHassert == nil {
			sf.t.Fatalf("new(): Hassert==nil")
		}

		fnAssert := GetFnAssert()
		if fnAssert == nil {
			sf.t.Fatalf("new(): Assert==nil")
		}
	}

	_ = MakeOk("test ok")
	_ = MakeErr[bool](fmt.Errorf("test err"))
	_ = MakeSome("test err")
	_ = MakeNone[int64]()

	kernServHttp := GetKernelServerHttp()
	go kernServHttp.Run()
	ctx.Cancel()
	ctx.Wg().Wait()
	ctx.Cancel()
	ctx.Wg().Wait()
}
