package alicloud

import (
	"fmt"
	"log"
	"time"

	"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

	"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 resourceAlicloudEcsNetworkInterfacePermission() *schema.Resource {
	return &schema.Resource{
		Create: resourceAlicloudEcsNetworkInterfacePermissionCreate,
		Read:   resourceAlicloudEcsNetworkInterfacePermissionRead,
		Delete: resourceAlicloudEcsNetworkInterfacePermissionDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},
		Timeouts: &schema.ResourceTimeout{
			Create: schema.DefaultTimeout(1 * time.Minute),
			Delete: schema.DefaultTimeout(1 * time.Minute),
		},
		Schema: map[string]*schema.Schema{
			"account_id": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"network_interface_id": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"permission": {
				Type:         schema.TypeString,
				Required:     true,
				ForceNew:     true,
				ValidateFunc: validation.StringInSlice([]string{"InstanceAttach"}, false),
			},
			"status": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"force": {
				Type:     schema.TypeBool,
				ForceNew: true,
				Optional: true,
			},
		},
	}
}

func resourceAlicloudEcsNetworkInterfacePermissionCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	var response map[string]interface{}
	action := "CreateNetworkInterfacePermission"
	request := make(map[string]interface{})
	var err error
	request["AccountId"] = d.Get("account_id")
	request["NetworkInterfaceId"] = d.Get("network_interface_id")
	request["Permission"] = d.Get("permission")
	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("Ecs", "2014-05-26", 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_ecs_network_interface_permission", action, AlibabaCloudSdkGoERROR)
	}
	responseNetworkInterfacePermission := response["NetworkInterfacePermission"].(map[string]interface{})
	d.SetId(fmt.Sprint(responseNetworkInterfacePermission["NetworkInterfacePermissionId"]))

	ecsService := EcsService{client}
	stateConf := BuildStateConf([]string{}, []string{"Granted"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, ecsService.EcsNetworkInterfacePermissionStateRefreshFunc(d.Id(), []string{}))
	if _, err := stateConf.WaitForState(); err != nil {
		return WrapErrorf(err, IdMsg, d.Id())
	}

	return resourceAlicloudEcsNetworkInterfacePermissionRead(d, meta)
}
func resourceAlicloudEcsNetworkInterfacePermissionRead(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	ecsService := EcsService{client}
	object, err := ecsService.DescribeEcsNetworkInterfacePermission(d.Id())
	if err != nil {
		if NotFoundError(err) {
			log.Printf("[DEBUG] Resource alicloud_ecs_network_interface_permission ecsService.DescribeEcsNetworkInterfacePermission Failed!!! %s", err)
			d.SetId("")
			return nil
		}
		return WrapError(err)
	}
	d.Set("account_id", fmt.Sprint(object["AccountId"]))
	d.Set("network_interface_id", object["NetworkInterfaceId"])
	d.Set("permission", object["Permission"])
	d.Set("status", object["PermissionState"])
	return nil
}
func resourceAlicloudEcsNetworkInterfacePermissionDelete(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	action := "DeleteNetworkInterfacePermission"
	var response map[string]interface{}
	var err error
	request := map[string]interface{}{
		"NetworkInterfacePermissionId": d.Id(),
	}

	if v, ok := d.GetOkExists("force"); ok {
		request["Force"] = v
	} else {
		request["Force"] = true
	}

	request["RegionId"] = client.RegionId
	wait := incrementalWait(3*time.Second, 3*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
		response, err = client.RpcPost("Ecs", "2014-05-26", 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)
	}
	return nil
}
