package service

import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"

	"gitlab.com/tensorsecurity-rd/waf-console/internal/model"
)

func TestIPService_QueryIP(t *testing.T) {
	type fields struct {
		URL    string
		ApiKey string
	}
	type args struct {
		ip string
	}
	tests := []struct {
		name    string
		fields  fields
		args    args
		want    *model.IPInfo
		wantErr bool
	}{
		// TODO: Add test cases.
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := &ipService{
				URL:    tt.fields.URL,
				ApiKey: tt.fields.ApiKey,
			}
			got, err := s.QueryIP(tt.args.ip)
			if (err != nil) != tt.wantErr {
				t.Errorf("IPService.QueryIP() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("IPService.QueryIP() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestParseIPInfo(t *testing.T) {
	t.Run("test 1", func(t *testing.T) {
		var respData RespData
		err := json.Unmarshal([]byte(fakeData), &respData)
		if err != nil {
			t.Errorf("Unmarshal err %v", err)
		}
		if info, ok := respData.Data["87.236.176.199"]; ok {
			var ipInfo IpInfo
			d, err := info.MarshalJSON()
			if err != nil {
				t.Error(err)
			}
			err = json.Unmarshal(d, &ipInfo)
			if err != nil {
				t.Error(err)
			}
			fmt.Println(ipInfo.Judgments)
		}
		fmt.Println("ddddd")

	})
}

func TestParseIPInfoPrivate(t *testing.T) {
	t.Run("test 1", func(t *testing.T) {
		var respData IPInfoPrivateResp
		err := json.Unmarshal([]byte(fakePrivateData), &respData)
		if err != nil {
			t.Errorf("Unmarshal err %v", err)
		}
		if len(respData.Data) > 0 {
			for _, v := range respData.Data {
				fmt.Println(v)
			}
		}
		fmt.Println("ddddd")
	})
}
