package alicloud

import (
	"fmt"
	"log"
	"time"

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

func resourceAlicloudOnsGroup() *schema.Resource {
	return &schema.Resource{
		Create: resourceAlicloudOnsGroupCreate,
		Read:   resourceAlicloudOnsGroupRead,
		Update: resourceAlicloudOnsGroupUpdate,
		Delete: resourceAlicloudOnsGroupDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},
		Timeouts: &schema.ResourceTimeout{
			Create: schema.DefaultTimeout(4 * time.Minute),
			Delete: schema.DefaultTimeout(4 * time.Minute),
		},
		Schema: map[string]*schema.Schema{
			"group_name": {
				Type:          schema.TypeString,
				Optional:      true,
				Computed:      true,
				ForceNew:      true,
				ValidateFunc:  validateOnsGroupId,
				ConflictsWith: []string{"group_id"},
			},
			"group_id": {
				Type:          schema.TypeString,
				Optional:      true,
				Computed:      true,
				ForceNew:      true,
				ValidateFunc:  validateOnsGroupId,
				Deprecated:    "Field 'group_id' has been deprecated from version 1.98.0. Use 'group_name' instead.",
				ConflictsWith: []string{"group_name"},
			},
			"group_type": {
				Type:         schema.TypeString,
				Optional:     true,
				ForceNew:     true,
				ValidateFunc: validation.StringInSlice([]string{"http", "tcp"}, false),
				Default:      "tcp",
			},
			"instance_id": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"read_enable": {
				Type:     schema.TypeBool,
				Optional: true,
			},
			"remark": {
				Type:         schema.TypeString,
				Optional:     true,
				ForceNew:     true,
				ValidateFunc: validation.StringLenBetween(1, 256),
			},
			"tags": tagsSchema(),
		},
	}
}

func resourceAlicloudOnsGroupCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	var response map[string]interface{}
	action := "OnsGroupCreate"
	request := make(map[string]interface{})
	var err error
	if v, ok := d.GetOk("group_name"); ok {
		request["GroupId"] = v
	} else if v, ok := d.GetOk("group_id"); ok {
		request["GroupId"] = v
	} else {
		return WrapError(Error(`[ERROR] Argument "group_id" or "group_name" must be set one!`))
	}

	if v, ok := d.GetOk("group_type"); ok {
		request["GroupType"] = v
	}

	request["InstanceId"] = d.Get("instance_id")
	if v, ok := d.GetOk("remark"); ok {
		request["Remark"] = v
	}

	wait := incrementalWait(3*time.Second, 3*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
		response, err = client.RpcPost("Ons", "2019-02-14", action, nil, request, false)
		if err != nil {
			if IsExpectedErrors(err, []string{"Throttling.User"}) || NeedRetry(err) {
				wait()
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		addDebug(action, response, request)
		return nil
	})
	if err != nil {
		return WrapErrorf(err, DefaultErrorMsg, "alicloud_ons_group", action, AlibabaCloudSdkGoERROR)
	}

	d.SetId(fmt.Sprint(request["InstanceId"], ":", request["GroupId"]))

	return resourceAlicloudOnsGroupUpdate(d, meta)
}
func resourceAlicloudOnsGroupRead(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	onsService := OnsService{client}
	object, err := onsService.DescribeOnsGroup(d.Id())
	if err != nil {
		if NotFoundError(err) {
			log.Printf("[DEBUG] Resource alicloud_ons_group onsService.DescribeOnsGroup Failed!!! %s", err)
			d.SetId("")
			return nil
		}
		return WrapError(err)
	}
	parts, err := ParseResourceId(d.Id(), 2)
	if err != nil {
		return WrapError(err)
	}
	d.Set("group_name", parts[1])
	d.Set("group_id", parts[1])
	d.Set("instance_id", parts[0])
	d.Set("group_type", object["GroupType"])
	d.Set("remark", object["Remark"])
	d.Set("tags", tagsToMap(object["Tags"].(map[string]interface{})["Tag"]))
	return nil
}
func resourceAlicloudOnsGroupUpdate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	onsService := OnsService{client}
	var err error
	var response map[string]interface{}
	parts, err := ParseResourceId(d.Id(), 2)
	if err != nil {
		return WrapError(err)
	}
	d.Partial(true)

	if d.HasChange("tags") {
		if err := onsService.SetResourceTags(d, "GROUP"); err != nil {
			return WrapError(err)
		}
		d.SetPartial("tags")
	}
	if d.HasChange("read_enable") {
		request := map[string]interface{}{
			"GroupId":    parts[1],
			"InstanceId": parts[0],
		}
		request["ReadEnable"] = d.Get("read_enable")
		action := "OnsGroupConsumerUpdate"
		wait := incrementalWait(3*time.Second, 3*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
			response, err = client.RpcPost("Ons", "2019-02-14", action, nil, request, false)
			if err != nil {
				if NeedRetry(err) {
					wait()
					return resource.RetryableError(err)
				}
				return resource.NonRetryableError(err)
			}
			addDebug(action, response, request)
			return nil
		})
		if err != nil {
			return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
		}
		d.SetPartial("read_enable")
	}
	d.Partial(false)
	return resourceAlicloudOnsGroupRead(d, meta)
}
func resourceAlicloudOnsGroupDelete(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	parts, err := ParseResourceId(d.Id(), 2)
	if err != nil {
		return WrapError(err)
	}
	action := "OnsGroupDelete"
	var response map[string]interface{}
	request := map[string]interface{}{
		"GroupId":    parts[1],
		"InstanceId": parts[0],
	}

	wait := incrementalWait(3*time.Second, 3*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
		response, err = client.RpcPost("Ons", "2019-02-14", action, nil, request, false)
		if err != nil {
			if IsExpectedErrors(err, []string{"Throttling.User"}) || NeedRetry(err) {
				wait()
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		addDebug(action, response, request)
		return nil
	})
	if err != nil {
		if IsExpectedErrors(err, []string{"AUTH_RESOURCE_OWNER_ERROR"}) {
			return nil
		}
		return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
	}
	return nil
}
