package alicloud

import (
	"fmt"
	"strings"
	"testing"

	"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudGaAdditionalCertificatesDataSource(t *testing.T) {
	rand := acctest.RandInt()
	checkoutSupportedRegions(t, true, connectivity.GaSupportRegions)
	allConf := dataSourceTestAccConfig{
		existConfig: testAccCheckAlicloudGaAdditionalCertificatesDataSourceName(rand, map[string]string{
			"ids": `["${alicloud_ga_additional_certificate.default.id}"]`,
		}),
		fakeConfig: testAccCheckAlicloudGaAdditionalCertificatesDataSourceName(rand, map[string]string{
			"ids": `["${alicloud_ga_additional_certificate.default.id}_fake"]`,
		}),
	}
	var existAlicloudGaAdditionalCertificatesDataSourceNameMapFunc = func(rand int) map[string]string {
		return map[string]string{
			"ids.#":                         "1",
			"certificates.#":                "1",
			"certificates.0.accelerator_id": CHECKSET,
			"certificates.0.certificate_id": CHECKSET,
			"certificates.0.id":             CHECKSET,
			"certificates.0.domain":         CHECKSET,
			"certificates.0.listener_id":    CHECKSET,
		}
	}
	var fakeAlicloudGaAdditionalCertificatesDataSourceNameMapFunc = func(rand int) map[string]string {
		return map[string]string{
			"ids.#":          "0",
			"certificates.#": "0",
		}
	}

	var alicloudGaAdditionalCertificatesCheckInfo = dataSourceAttr{
		resourceId:   "data.alicloud_ga_additional_certificates.default",
		existMapFunc: existAlicloudGaAdditionalCertificatesDataSourceNameMapFunc,
		fakeMapFunc:  fakeAlicloudGaAdditionalCertificatesDataSourceNameMapFunc,
	}

	preCheck := func() {
		testAccPreCheck(t)
	}
	alicloudGaAdditionalCertificatesCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, allConf)
}
func testAccCheckAlicloudGaAdditionalCertificatesDataSourceName(rand int, attrMap map[string]string) string {
	var pairs []string
	for k, v := range attrMap {
		pairs = append(pairs, k+" = "+v)
	}

	config := fmt.Sprintf(`

variable "name" {	
	default = "tf-testAccAdditionalCertificate-%d"
}

data "alicloud_ga_accelerators" "default" {
  status = "active"
}

resource "alicloud_ga_bandwidth_package" "default" {
   	bandwidth              =  100
  	type                   = "Basic"
  	bandwidth_type         = "Basic"
	payment_type           = "PayAsYouGo"
  	billing_type           = "PayBy95"
	ratio       = 30
	bandwidth_package_name = var.name
    auto_pay               = true
    auto_use_coupon        = true
}

resource "alicloud_ga_bandwidth_package_attachment" "default" {
	// Please run resource ga_accelerator test case to ensure this account has at least one accelerator before run this case.
	accelerator_id = data.alicloud_ga_accelerators.default.ids.0
	bandwidth_package_id = alicloud_ga_bandwidth_package.default.id
}

resource "alicloud_ssl_certificates_service_certificate" "default" {
  count            = 2
  certificate_name = join("-", [var.name, count.index])
  cert             = <<EOF
-----BEGIN CERTIFICATE-----
MIIDRjCCAq+gAwIBAgIJAJn3ox4K13PoMA0GCSqGSIb3DQEBBQUAMHYxCzAJBgNV
BAYTAkNOMQswCQYDVQQIEwJCSjELMAkGA1UEBxMCQkoxDDAKBgNVBAoTA0FMSTEP
MA0GA1UECxMGQUxJWVVOMQ0wCwYDVQQDEwR0ZXN0MR8wHQYJKoZIhvcNAQkBFhB0
ZXN0QGhvdG1haWwuY29tMB4XDTE0MTEyNDA2MDQyNVoXDTI0MTEyMTA2MDQyNVow
djELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAkJKMQswCQYDVQQHEwJCSjEMMAoGA1UE
ChMDQUxJMQ8wDQYDVQQLEwZBTElZVU4xDTALBgNVBAMTBHRlc3QxHzAdBgkqhkiG
9w0BCQEWEHRlc3RAaG90bWFpbC5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
AoGBAM7SS3e9+Nj0HKAsRuIDNSsS3UK6b+62YQb2uuhKrp1HMrOx61WSDR2qkAnB
coG00Uz38EE+9DLYNUVQBK7aSgLP5M1Ak4wr4GqGyCgjejzzh3DshUzLCCy2rook
KOyRTlPX+Q5l7rE1fcSNzgepcae5i2sE1XXXzLRIDIvQxcspAgMBAAGjgdswgdgw
HQ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z6FEnBaLOVD6hf6YQIsPQJAX/CZPoW6dzwgvimo1/GcY6eleiWE
qygqjWhsh71e/3bz7yuEAnj5yE3t7Zshcp+dXR3xxGo0eSuLfLFxHgGxwQJAAxf8
9DzQ5NkPkTCJi0sqbl8/03IUKTgT6hcbpWdDXa7m8J3wRr3o5nUB+TPQ5nzAbthM
zWX931YQeACcwhxvHQJBAN5mTzzJD4w4Ma6YTaNHyXakdYfyAWrOkPIWZxfhMfXe
DrlNdiysTI4Dd1dLeErVpjsckAaOW/JDG5PCSwkaMxk=
-----END RSA PRIVATE KEY-----
EOF
}

locals {
  domain               = "test"
  certificate_id       = join("-", [alicloud_ssl_certificates_service_certificate.default.1.id, "cn-hangzhou"])
}

resource "alicloud_ga_listener" "default" {
  depends_on     = [alicloud_ga_bandwidth_package_attachment.default]
  accelerator_id = alicloud_ga_bandwidth_package_attachment.default.accelerator_id
  name           = var.name
  protocol       = "HTTPS"
  port_ranges {
    from_port = 8080
    to_port   = 8080
  }
  certificates {
    id = join("-", [alicloud_ssl_certificates_service_certificate.default.0.id, "%s"])
  }
}

resource "alicloud_ga_additional_certificate" "default" {
  certificate_id = local.certificate_id
  domain         = local.domain
  accelerator_id = alicloud_ga_bandwidth_package_attachment.default.accelerator_id
  listener_id    = alicloud_ga_listener.default.id
}

data "alicloud_ga_additional_certificates" "default" {	
	accelerator_id = alicloud_ga_additional_certificate.default.accelerator_id
	listener_id    = alicloud_ga_listener.default.id
	%s
}
`, rand, defaultRegionToTest, strings.Join(pairs, " \n "))
	return config
}
