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"
)

func resourceAlicloudDatabaseGatewayGateway() *schema.Resource {
	return &schema.Resource{
		Create: resourceAlicloudDatabaseGatewayGatewayCreate,
		Read:   resourceAlicloudDatabaseGatewayGatewayRead,
		Update: resourceAlicloudDatabaseGatewayGatewayUpdate,
		Delete: resourceAlicloudDatabaseGatewayGatewayDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},
		Schema: map[string]*schema.Schema{
			"gateway_desc": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"gateway_name": {
				Type:     schema.TypeString,
				Required: true,
			},
			"status": {
				Type:     schema.TypeString,
				Computed: true,
			},
		},
	}
}

func resourceAlicloudDatabaseGatewayGatewayCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	var response map[string]interface{}
	action := "CreateGateway"
	request := make(map[string]interface{})
	var err error
	if v, ok := d.GetOk("gateway_desc"); ok {
		request["GatewayDesc"] = v
	}
	request["GatewayName"] = d.Get("gateway_name")
	request["RegionId"] = client.RegionId
	wait := incrementalWait(3*time.Second, 3*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
		response, err = client.RpcPost("dg", "2019-03-27", action, nil, request, false)
		if err != nil {
			if NeedRetry(err) {
				wait()
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		return nil
	})
	addDebug(action, response, request)
	if err != nil {
		return WrapErrorf(err, DefaultErrorMsg, "alicloud_database_gateway_gateway", action, AlibabaCloudSdkGoERROR)
	}
	if fmt.Sprint(response["Success"]) == "false" {
		return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
	}

	d.SetId(fmt.Sprint(response["Data"]))

	return resourceAlicloudDatabaseGatewayGatewayRead(d, meta)
}
func resourceAlicloudDatabaseGatewayGatewayRead(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	dgService := DgService{client}
	object, err := dgService.DescribeDatabaseGatewayGateway(d.Id())
	if err != nil {
		if NotFoundError(err) {
			log.Printf("[DEBUG] Resource alicloud_database_gateway_gateway dgService.DescribeDatabaseGatewayGateway Failed!!! %s", err)
			d.SetId("")
			return nil
		}
		return WrapError(err)
	}
	d.Set("gateway_desc", object["gatewayDesc"])
	d.Set("gateway_name", object["gatewayName"])
	d.Set("status", object["status"])
	return nil
}
func resourceAlicloudDatabaseGatewayGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	var err error
	var response map[string]interface{}
	update := false
	request := map[string]interface{}{
		"GatewayId": d.Id(),
	}
	if d.HasChange("gateway_desc") {
		update = true
	}
	if v, ok := d.GetOk("gateway_desc"); ok {
		request["GatewayDesc"] = v
	}
	if d.HasChange("gateway_name") {
		update = true
	}
	request["GatewayName"] = d.Get("gateway_name")
	if update {
		action := "ModifyGateway"
		wait := incrementalWait(3*time.Second, 3*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
			response, err = client.RpcPost("dg", "2019-03-27", action, nil, request, false)
			if err != nil {
				if NeedRetry(err) {
					wait()
					return resource.RetryableError(err)
				}
				return resource.NonRetryableError(err)
			}
			return nil
		})
		addDebug(action, response, request)
		if err != nil {
			return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
		}
		if fmt.Sprint(response["Success"]) == "false" {
			return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
		}
	}
	return resourceAlicloudDatabaseGatewayGatewayRead(d, meta)
}
func resourceAlicloudDatabaseGatewayGatewayDelete(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	action := "DeleteGateway"
	var response map[string]interface{}
	var err error
	request := map[string]interface{}{
		"GatewayId": d.Id(),
	}

	wait := incrementalWait(3*time.Second, 3*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
		response, err = client.RpcPost("dg", "2019-03-27", action, nil, request, false)
		if err != nil {
			if NeedRetry(err) {
				wait()
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		return nil
	})
	addDebug(action, response, request)
	if err != nil {
		return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
	}
	if fmt.Sprint(response["Success"]) == "false" {
		return WrapError(fmt.Errorf("%s failed, response: %v", action, response))
	}
	return nil
}
