package tart

import (
	"context"
	"fmt"
	"github.com/hashicorp/packer-plugin-sdk/multistep"
	packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
	"strconv"
	"time"
)

type stepCreateVM struct{}

func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*Config)
	ui := state.Get("ui").(packersdk.Ui)

	ui.Say("Creating virtual machine...")

	isLinux := len(config.FromISO) > 0

	createArguments := []string{"create"}
	if config.FromIPSW != "" {
		createArguments = append(createArguments, "--from-ipsw", config.FromIPSW)
	} else if isLinux {
		createArguments = append(createArguments, "--linux")
	}

	if config.DiskSizeGb > 0 {
		createArguments = append(createArguments, "--disk-size", strconv.Itoa(int(config.DiskSizeGb)))
	}

	if config.DiskFormat != "" && config.DiskFormat != "raw" {
		createArguments = append(createArguments, "--disk-format", config.DiskFormat)
	}

	createArguments = append(createArguments, config.VMName)

	if _, err := TartExec(ctx, ui, createArguments...); err != nil {
		err := fmt.Errorf("Failed to create a VM: %s", err)
		state.Put("error", err)
		return multistep.ActionHalt
	}

	state.Put("vm_name", config.VMName)

	if config.CreateGraceTime != 0 {
		message := fmt.Sprintf("Waiting %v to let the Virtualization.Framework's installation process "+
			"to finish correctly...", config.CreateGraceTime)
		ui.Say(message)
		time.Sleep(config.CreateGraceTime)
	}

	return multistep.ActionContinue
}

func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
	// nothing to clean up
}
