package managecenterMgr

import (
	"fmt"
	"testing"
)

func init() {
	manageCenterDataSwitchObj := &ManageCenterDataSwitch{
		AllDataSwitch: true,
	}
	Start("https://managecenterapi-sd3.7qule.com/", manageCenterDataSwitchObj)

	item, exist := GetAreaLabelDBByGroupId(82)
	if exist {
		fmt.Printf("%v", item)
	}
}

func TestPartner(t *testing.T) {
	partnerList := GetPartnerList()
	fmt.Printf("PartnerList count:%v\n", len(partnerList))
	if len(partnerList) == 0 {
		t.Errorf("There is no partner.")
		return
	}

	firstPartner := partnerList[0]
	_, exist := GetPartner(firstPartner.Id)
	if !exist {
		t.Errorf("Partner with Id:%d should exist, but now it doesn't.", firstPartner.Id)
		return
	}

	_, exist = GetPartnerByAlias(firstPartner.Alias)
	if !exist {
		t.Errorf("Partner with Alias:%s should exist, but now it doesn't.", firstPartner.Alias)
		return
	}

	_, exist, err := GetOtherConfigInfo(firstPartner.Id, "LoginHandler")
	if err != nil {
		t.Errorf("There should no error for Partner:%d, but now there is one:%v", err, firstPartner.Id)
		return
	}
	if !exist {
		t.Errorf("Partner with Id:%d should have an other config named LoginHandler, but now there isn't.", firstPartner.Id)
		return
	}
}

func TestServer(t *testing.T) {
	serverGroupList := GetServerGroupList()
	if len(serverGroupList) == 0 {
		t.Errorf("There is no ServerGroup.")
		return
	}
	firstServerGroup := serverGroupList[0]

	serverList := GetServerListByGroupId(firstServerGroup.Id)
	fmt.Printf("There are %d servers of GroupId:%d\n", len(serverList), firstServerGroup.Id)

	firstServer := serverList[0]
	serverList = GetServerListByPartnerId(firstServer.PartnerId)
	fmt.Printf("There are %d servers of PartnerId:%d\n", len(serverList), firstServer.PartnerId)

	_, exist := GetServerItem(firstServer.PartnerId, firstServer.Id)
	if !exist {
		t.Errorf("There is no server with PartnerId:%d, ServerId:%d.", firstServer.PartnerId, firstServer.Id)
		return
	}

	distinctServerIdList := GetDistinctServerIdList()
	fmt.Printf("There are %d distinct ServerId\n", len(distinctServerIdList))
}

func TestServerGroup(t *testing.T) {
	serverGroupList := GetServerGroupList()
	if len(serverGroupList) == 0 {
		t.Errorf("There is no ServerGroup.")
		return
	}

	firstServerGroup := serverGroupList[0]
	_, exist := GetServerGroupItem(firstServerGroup.Id)
	if !exist {
		t.Errorf("The ServerGroup with Id:%d should exist, but now it doesn't.", firstServerGroup.Id)
		return
	}

	serverList := GetServerListByGroupId(firstServerGroup.Id)
	if len(serverList) == 0 {
		t.Errorf("There is no server of ServerGroupId:%d", firstServerGroup.Id)
		return
	}
	firstServer := serverList[0]
	_, _, exist = GetServerGroup(firstServer.PartnerId, firstServer.Id)
	if !exist {
		t.Errorf("The ServerGroup with PartnerId:%d, ServerId:%d should exist, but now it doesn't.", firstServer.PartnerId, firstServer.Id)
		return
	}
}

func TestResourceVersion(t *testing.T) {
	list := GetResourceVersionList()
	fmt.Printf("There are %d resource versions.\n", len(list))
}

func TestUserWhiteList(t *testing.T) {
	fmt.Printf("There are %d whiteLists.\n", len(whiteListMap))
	if len(whiteListMap) == 0 {
		return
	}

	var partnerId int32
	var userId string
	for _, v := range whiteListMap {
		for _, _v := range v {
			partnerId = _v.PartnerId
			userId = _v.UserId
			break
		}
		break
	}

	expected := true
	got := IsInWhiteList(partnerId, userId)
	if got != expected {
		t.Errorf("Expected to get %t, but got %t", expected, got)
		return
	}
}
