package common

import (
	"reflect"
	"sync"
	"testing"
)

func TestGetValue_string(t *testing.T) {
	t.Parallel()
	type args struct {
		vals map[string]any
		key  string
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{
			name: "ok",
			args: args{
				vals: map[string]any{
					"1": "123",
					"2": "234",
				},
				key: "1",
			},
			want:    "123",
			wantErr: false,
		},
		{
			name: "value not presented",
			args: args{
				vals: map[string]any{
					"1": "123",
					"2": "234",
				},
				key: "3",
			},
			want:    "",
			wantErr: true,
		},
		{
			name: "nil map",
			args: args{
				vals: nil,
				key:  "1",
			},
			want:    "",
			wantErr: true,
		},
		{
			name: "invalid type",
			args: args{
				vals: map[string]any{
					"1": "123",
					"2": 234,
				},
				key: "2",
			},
			want:    "",
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := GetValue[string](_mapToSyncMap(tt.args.vals), tt.args.key)
			if (err != nil) != tt.wantErr {
				t.Errorf("GetValue() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("GetValue() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestGetValue_struct(t *testing.T) {
	t.Parallel()

	type val struct {
		a int
		b string
		c bool
	}
	type args struct {
		vals map[string]any
		key  string
	}
	tests := []struct {
		name    string
		args    args
		want    val
		wantErr bool
	}{
		{
			name: "ok",
			args: args{
				vals: map[string]any{
					"1": val{
						a: 1,
						b: "2",
						c: true,
					},
					"2": "234",
				},
				key: "1",
			},
			want: val{
				a: 1,
				b: "2",
				c: true,
			},
			wantErr: false,
		},
		{
			name: "value not presented",
			args: args{
				vals: map[string]any{
					"1": "123",
					"2": "234",
				},
				key: "3",
			},
			want:    val{},
			wantErr: true,
		},
		{
			name: "nil map",
			args: args{
				vals: nil,
				key:  "1",
			},
			want:    val{},
			wantErr: true,
		},
		{
			name: "invalid type",
			args: args{
				vals: map[string]any{
					"1": "123",
					"2": 234,
				},
				key: "2",
			},
			want:    val{},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := GetValue[val](_mapToSyncMap(tt.args.vals), tt.args.key)
			if (err != nil) != tt.wantErr {
				t.Errorf("GetValue() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("GetValue() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestGetValue_structptr(t *testing.T) {
	t.Parallel()

	type val struct {
		a int
		b string
		c bool
	}
	type args struct {
		vals map[string]any
		key  string
	}
	tests := []struct {
		name    string
		args    args
		want    *val
		wantErr bool
	}{
		{
			name: "ok",
			args: args{
				vals: map[string]any{
					"1": &val{
						a: 1,
						b: "2",
						c: true,
					},
					"2": "234",
				},
				key: "1",
			},
			want: &val{
				a: 1,
				b: "2",
				c: true,
			},
			wantErr: false,
		},
		{
			name: "value not presented",
			args: args{
				vals: map[string]any{
					"1": "123",
					"2": "234",
				},
				key: "3",
			},
			want:    nil,
			wantErr: true,
		},
		{
			name: "nil map",
			args: args{
				vals: nil,
				key:  "1",
			},
			want:    nil,
			wantErr: true,
		},
		{
			name: "invalid type",
			args: args{
				vals: map[string]any{
					"1": "123",
					"2": 234,
				},
				key: "2",
			},
			want:    nil,
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := GetValue[*val](_mapToSyncMap(tt.args.vals), tt.args.key)
			if (err != nil) != tt.wantErr {
				t.Errorf("GetValue() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("GetValue() = %v, want %v", got, tt.want)
			}
		})
	}
}

func _mapToSyncMap(m map[string]any) *sync.Map {
	out := &sync.Map{}

	for k, v := range m {
		out.Store(k, v)
	}

	return out
}
