package aws

import (
	"net/http"
	"os"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
)

var (
	client *ec2.EC2
)

func initEC2(region string) {
	creds := credentials.NewChainCredentials(
		[]credentials.Provider{
			&credentials.EnvProvider{},               // check environment
			&credentials.SharedCredentialsProvider{}, // check home dir
		},
	)

	if region == "" { // user didn't set region
		region = os.Getenv("AWS_DEFAULT_REGION") // aws cli checks this
		if region == "" {
			region = os.Getenv("AWS_REGION") // aws sdk checks this
		}
	}

	client = ec2.New(session.New(&aws.Config{
		Credentials: creds,
		Region:      &region,
		//CredentialsChainVerboseErrors: aws.Bool(true),
		HTTPClient: &http.Client{Timeout: 30 * time.Second},
	}))

}

type awsConfig struct {
	Ami           string
	RouteTable    string
	Region        string
	SecurityGroup string
	Vpc           string
	Subnet        string
	SshKey        string
}
