package alicloud

import (
	"fmt"
	"strings"
	"testing"

	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudSLBDomainExtensionsDataSource_basic(t *testing.T) {
	rand := acctest.RandInt()
	basicConf := dataSourceTestAccConfig{
		existConfig: testAccCheckAlicloudDomainExtensionDataSourceConfig(rand, map[string]string{
			"load_balancer_id": `"${alicloud_slb_domain_extension.default.load_balancer_id}"`,
			"frontend_port":    `"${alicloud_slb_domain_extension.default.frontend_port}"`,
		}),
	}
	allConf := dataSourceTestAccConfig{
		existConfig: testAccCheckAlicloudDomainExtensionDataSourceConfig(rand, map[string]string{
			"load_balancer_id": `"${alicloud_slb_domain_extension.default.load_balancer_id}"`,
			"frontend_port":    `"${alicloud_slb_domain_extension.default.frontend_port}"`,
			"ids":              `["${alicloud_slb_domain_extension.default.id}"]`,
		}),
		fakeConfig: testAccCheckAlicloudDomainExtensionDataSourceConfig(rand, map[string]string{
			"load_balancer_id": `"${alicloud_slb_domain_extension.default.load_balancer_id}"`,
			"frontend_port":    `"${alicloud_slb_domain_extension.default.frontend_port}"`,
			"ids":              `["${alicloud_slb_domain_extension.default.id}_fake"]`,
		}),
	}

	var existSLBDomainExtensionsMapFunc = func(rand int) map[string]string {
		return map[string]string{
			"extensions.#":                       "1",
			"ids.#":                              "1",
			"extensions.0.id":                    CHECKSET,
			"extensions.0.domain":                "www.test.com",
			"extensions.0.server_certificate_id": CHECKSET,
		}
	}

	var fakeSLBDomainExtensionsMapFunc = func(rand int) map[string]string {
		return map[string]string{
			"slb_rules.#": "0",
			"ids.#":       "0",
			"names.#":     "0",
		}
	}

	var slbDomainExtensionsCheckInfo = dataSourceAttr{
		resourceId:   "data.alicloud_slb_domain_extensions.default",
		existMapFunc: existSLBDomainExtensionsMapFunc,
		fakeMapFunc:  fakeSLBDomainExtensionsMapFunc,
	}

	slbDomainExtensionsCheckInfo.dataSourceTestCheck(t, rand, basicConf, allConf)
}

func testAccCheckAlicloudDomainExtensionDataSourceConfig(rand int, attrMap map[string]string) string {
	var pairs []string
	for k, v := range attrMap {
		pairs = append(pairs, k+" = "+v)
	}
	return fmt.Sprintf(`
	variable "name" {
		default = "tf-testAccCheckAlicloudSlbsDataSourceBasic-%d"
	}
	resource "alicloud_slb_load_balancer" "instance" {
  		load_balancer_name           = "${var.name}"
  		internet_charge_type = "PayByTraffic"
  		address_type       = "internet"
		load_balancer_spec        = "slb.s2.small"
	}
	resource "alicloud_slb_server_certificate" "foo" {
  		name               = "${var.name}"
  		server_certificate = "-----BEGIN CERTIFICATE-----\nMIIDdjCCAl4CCQCcm+erkcKN7DANBgkqhkiG9w0BAQsFADB9MQswCQYDVQQGEwJj\nbjELMAkGA1UECAwCYmoxEDAOBgNVBAcMB2JlaWppbmcxDzANBgNVBAoMBmFsaXl1\nbjELMAkGA1UECwwCc2MxFTATBgNVBAMMDHd3dy50ZXN0LmNvbTEaMBgGCSqGSIb3\nDQEJARYLMTIzQDEyMy5jb20wHhcNMTkwNDI2MDM0ODAxWhcNMjQwNDI1MDM0ODAx\nWjB9MQswCQYDVQQGEwJjbjELMAkGA1UECAwCYmoxEDAOBgNVBAcMB2JlaWppbmcx\nDzANBgNVBAoMBmFsaXl1bjELMAkGA1UECwwCc2MxFTATBgNVBAMMDHd3dy50ZXN0\nLmNvbTEaMBgGCSqGSIb3DQEJARYLMTIzQDEyMy5jb20wggEiMA0GCSqGSIb3DQEB\nAQUAA4IBDwAwggEKAoIBAQDKMKF5qmN/uoMjdH3D8aPRcUOA0s8rZpYhG8zbkF1j\n8gHYoB/FDvM7G7dfVsyjbMwLOxKvAhWvHHSpEz/t7gB+QdwrAMiMJwGmtCnXrh2E\nWiXgalMe1y4a/T5R7q+m4T1zFATf+kbnHWfkSGF4W7b6UBoaH+9StQ95CnqzNf/2\np/Of7+S0XzCxFXw8GIVzZk0xFe6lHJzaq06f3mvzrD+4rpO56tTUvrgTY/n61gsF\nZP7f0CJ2JQh6eNRFOEUSfxKu/Dy/+IsQxorCJY2Q59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A\n-----END RSA PRIVATE KEY-----"
	}
	resource "alicloud_slb_listener" "https" {
  		load_balancer_id          = "${alicloud_slb_load_balancer.instance.id}"
  		backend_port              = 80
  		frontend_port             = 443
  		protocol                  = "https"
  		sticky_session            = "on"
  		sticky_session_type       = "insert"
  		cookie                    = "testslblistenercookie"
  		cookie_timeout            = 86400
  		health_check              = "on"
  		health_check_uri          = "/cons"
  		health_check_connect_port = 20
  		healthy_threshold         = 8
  		unhealthy_threshold       = 8
  		health_check_timeout      = 8
  		health_check_interval     = 5
  		health_check_http_code    = "http_2xx,http_3xx"
  		bandwidth                 = 10
  		ssl_certificate_id        = "${alicloud_slb_server_certificate.foo.id}"
	}
	resource "alicloud_slb_domain_extension" "default" {
  		load_balancer_id      = "${alicloud_slb_load_balancer.instance.id}"
  		frontend_port         = "${alicloud_slb_listener.https.frontend_port}"
  		domain                = "www.test.com"
  		server_certificate_id = "${alicloud_slb_server_certificate.foo.id}"
 	}
 	data "alicloud_slb_domain_extensions" "default" {
		%s
 	}`, rand, strings.Join(pairs, "\n "))
}
